Class AgentModelResponse

java.lang.Object
dev.langchain4j.agentic.internal.AgentModelResponse
All Implemented Interfaces:
LangChain4jManaged

public final class AgentModelResponse extends Object implements LangChain4jManaged
The response of the model called by one invocation of an AI or decision agent, as reported to the agent's listeners after the invocation. The invoker of the agent creates one instance per invocation and puts it, next to the AgenticScope, in the LangChain4jManaged parameters of the invocation, so that the handler of the agent finds it in the invocation context of the AI service events it receives, on whatever thread they arrive, and the invoker reads it once the agent method has returned.

The model call of an agent returning a TokenStream or a future completes on another thread, usually after the invoker has already reported the invocation, so such a response is not reported; the state of this class is guarded by its lock, so that a model call completing while the invoker reads it is never seen half-written.

  • Constructor Details

    • AgentModelResponse

      public AgentModelResponse()
  • Method Details

    • claim

      public void claim(Object handler)
      Claims this response for the handler running the agent method of the invocation. An agent invoked directly from inside another agent on the same thread, for instance by one of its tools, finds the response of the enclosing invocation already owned, so its model calls are ignored instead of being summed into it.
    • isOwnedBy

      public boolean isOwnedBy(Object handler)
    • onChatResponse

      public void onChatResponse(AiServiceResponseReceivedEvent event)
      Records one model call of the invocation: an agent calling tools makes several, and its reported response is the last one with the token usage of all of them, and the model name only when all the calls agree on it.
    • onDecisionResponse

      public void onDecisionResponse(DecisionResponse decisionResponse)
    • chatRequest

      public ChatRequest chatRequest()
    • chatResponse

      public ChatResponse chatResponse()
    • lastUserMessage

      public UserMessage lastUserMessage()
    • decisionResponse

      public DecisionResponse decisionResponse()