Class DefaultInvocationContext

java.lang.Object
dev.langchain4j.invocation.DefaultInvocationContext
All Implemented Interfaces:
InvocationContext

public class DefaultInvocationContext extends Object implements InvocationContext
The InvocationContext built by LangChain4j.

Besides its values, it holds whether the invocation has already reported its end (see InvocationEnd). Copies made with InvocationContext.toBuilder() that keep the same invocationId() share this state, so the invocation reports its end only once, whichever copy is used. A copy with another invocation id, or a context built from scratch, starts as not ended.

Since:
1.6.0
  • Constructor Details

  • Method Details

    • invocationId

      public UUID invocationId()
      Description copied from interface: InvocationContext
      Unique identifier for an entire AI Service invocation. AI Services created with AiServices treat each subscription to a publisher returned by an AI Service method as a separate invocation, with its own identifier.
      Specified by:
      invocationId in interface InvocationContext
    • parentInvocationId

      public UUID parentInvocationId()
      Description copied from interface: InvocationContext
      The InvocationContext.invocationId() of the parent invocation: the AI Service invocation whose own work started this invocation. This is the case when one of these parts of an AI Service calls another AI Service:
      • a tool, a tool error handler or the strategy for hallucinated tool names
      • a tool provider
      • an input or output guardrail
      • a component of the retrieval augmentor (for example a query transformer or a content retriever)
      • the system or user message provider, or the system message transformer
      • the chat memory (including its ChatMemoryStore) or the chat memory provider
      • the moderation model
      • the chatRequestTransformer
      • the chat model itself (for example your own ChatModel implementation)

      It is null when the AI Service is called from your own code (for example from a handler of a TokenStream or from a listener, also from those of an AI Service that itself has a parent), when there is no parent, or when this InvocationContext implementation does not track it.

      The parent is also known when LangChain4j itself runs that part on another thread (for example tools executed concurrently, or a blocking content retriever offloaded to an executor). But if your own code in that part hands the call to another thread (for example with CompletableFuture.supplyAsync(..., executor)), it is null. In rare cases, when futures are shared between invocations, it can even name another invocation. So treat it as best effort.

      It is filled by the AI Service implementation that LangChain4j provides. Frameworks that replace that implementation (for example through AiServicesFactory) return null until they add support for it, also for AI Services they create through AiServices.builder(...).

      It is set when the AI Service method is called: later runs of the same TokenStream keep it.

      Agents run by an agentic workflow (sequence, parallel, loop, conditional) or by a supervisor currently have no parent. An agent called from a tool of another agent has one.

      Specified by:
      parentInvocationId in interface InvocationContext
      Returns:
      the id of the parent invocation, or null if there is none or it is not known
    • interfaceName

      public String interfaceName()
      Description copied from interface: InvocationContext
      The fully-qualified name of the AI Service interface where the invocation was initiated from
      Specified by:
      interfaceName in interface InvocationContext
      See Also:
    • methodName

      public String methodName()
      Description copied from interface: InvocationContext
      The method name on InvocationContext.interfaceName() where the invocation was initiated from
      Specified by:
      methodName in interface InvocationContext
    • methodReturnType

      public Type methodReturnType()
      Description copied from interface: InvocationContext
      The declared return type of the AI Service method, including its type arguments (for example List<String> or TokenStream).

      Frameworks that create the context with their own AI Service implementation return null until they add support for it.

      Specified by:
      methodReturnType in interface InvocationContext
      Returns:
      the return type of the AI Service method, or null if it is not known
    • methodArguments

      public List<Object> methodArguments()
      Description copied from interface: InvocationContext
      The arguments passed into the AI Service method
      Specified by:
      methodArguments in interface InvocationContext
    • userMessage

      public UserMessage userMessage()
      Description copied from interface: InvocationContext
      The UserMessage to be sent to the LLM. This is the message after all transformations (RAG augmentation, content injection, input guardrails, output format instructions).
      Specified by:
      userMessage in interface InvocationContext
    • originalUserMessage

      public UserMessage originalUserMessage()
      Description copied from interface: InvocationContext
      The UserMessage created from the AI Service method and its prompt template, before any transformation (RAG augmentation, content injection, input guardrails, output format instructions). It is what the user asked, without the retrieved content, for example to decide which tools or models are relevant to the question.
      Specified by:
      originalUserMessage in interface InvocationContext
    • chatMemoryId

      public Object chatMemoryId()
      Description copied from interface: InvocationContext
      The chat memory id parameter of the method
      Specified by:
      chatMemoryId in interface InvocationContext
    • defaultRequestParameters

      public ChatRequestParameters defaultRequestParameters()
      Description copied from interface: InvocationContext
      Returns the default ChatRequestParameters of the chat model configured on the AI service.
      Specified by:
      defaultRequestParameters in interface InvocationContext
    • modelProvider

      public ModelProvider modelProvider()
      Description copied from interface: InvocationContext
      Returns the ModelProvider of the chat model configured on the AI service.
      Specified by:
      modelProvider in interface InvocationContext
    • invocationParameters

      public InvocationParameters invocationParameters()
      Description copied from interface: InvocationContext
      The invocation parameters
      Specified by:
      invocationParameters in interface InvocationContext
    • managedParameters

      public Map<Class<? extends LangChain4jManaged>, LangChain4jManaged> managedParameters()
      Description copied from interface: InvocationContext
      LangChain4j managed parameters
      Specified by:
      managedParameters in interface InvocationContext
    • timestamp

      public Instant timestamp()
      Description copied from interface: InvocationContext
      Retrieves the point in time when the invocation occurred.
      Specified by:
      timestamp in interface InvocationContext
    • equals

      public boolean equals(Object object)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object