Class LlmAgent

All Implemented Interfaces:
Node

public class LlmAgent extends BaseAgent
The LLM-based agent.
  • Constructor Details

  • Method Details

    • builder

      public static LlmAgent.Builder builder()
      Returns a LlmAgent.Builder for LlmAgent.
    • determineLlmFlow

      protected BaseLlmFlow determineLlmFlow()
    • runAsyncImpl

      protected io.reactivex.rxjava3.core.Flowable<Event> runAsyncImpl(InvocationContext invocationContext)
      Description copied from class: BaseAgent
      Agent-specific asynchronous logic.
      Specified by:
      runAsyncImpl in class BaseAgent
      Parameters:
      invocationContext - Current invocation context.
      Returns:
      stream of agent-generated events.
    • runNode

      @Experimental public io.reactivex.rxjava3.core.Flowable<Event> runNode(Context context, @Nullable Object nodeInput)
      Runs this agent as a workflow node: a AgentMode.SINGLE_TURN agent takes the node input as its user turn and gives the first final reply, its own or its inline target's, as output; a AgentMode.TASK agent works until finish_task succeeds, failing with StateSchemaException if the node's state schema rejects its outputKey() write; and a AgentMode.CHAT agent runs the tasks it delegates and stays open (waitForOutput()). The flow of a chat or task agent stops at a handoff, and the workflow runs the target as a node, or fails the run if the handoff is invalid. Any other agent runs as BaseAgent.runNode(Context, Object) runs it.
      Specified by:
      runNode in interface Node
      Overrides:
      runNode in class BaseAgent
    • waitForOutput

      @Experimental public boolean waitForOutput()
      Returns true for a AgentMode.CHAT or AgentMode.TASK agent, whose node stays open across turns until it produces an output. An agent without a mode is a chat agent when another agent in its tree sets a mode.
      Specified by:
      waitForOutput in interface Node
      Overrides:
      waitForOutput in class BaseNode
    • runLiveImpl

      protected io.reactivex.rxjava3.core.Flowable<Event> runLiveImpl(InvocationContext invocationContext)
      Description copied from class: BaseAgent
      Agent-specific synchronous logic.
      Specified by:
      runLiveImpl in class BaseAgent
      Parameters:
      invocationContext - Current invocation context.
      Returns:
      stream of agent-generated events.
    • canonicalInstruction

      public io.reactivex.rxjava3.core.Single<Map.Entry<String,Boolean>> canonicalInstruction(ReadonlyContext context)
      Constructs the text instruction for this agent based on the instruction field. Also returns a boolean indicating that state injection should be bypassed when the instruction is constructed with an Instruction.Provider.

      This method is only for use by Agent Development Kit.

      Parameters:
      context - The context to retrieve the session state.
      Returns:
      The resolved instruction as a Single wrapped Map.Entry. The key is the instruction string and the value is a boolean indicating if state injection should be bypassed.
    • canonicalGlobalInstruction

      public io.reactivex.rxjava3.core.Single<Map.Entry<String,Boolean>> canonicalGlobalInstruction(ReadonlyContext context)
      Constructs the text global instruction for this agent based on the globalInstruction field. Also returns a boolean indicating that state injection should be bypassed when the instruction is constructed with an Instruction.Provider.

      This method is only for use by Agent Development Kit.

      Parameters:
      context - The context to retrieve the session state.
      Returns:
      The resolved global instruction as a Single wrapped Map.Entry. The key is the instruction string and the value is a boolean indicating if state injection should be bypassed.
    • canonicalTools

      public io.reactivex.rxjava3.core.Flowable<BaseTool> canonicalTools()
      Constructs the list of tools for this agent based on the tools() field.
      Returns:
      The resolved list of tools as a Single wrapped list of BaseTool.
    • canonicalTools

      public io.reactivex.rxjava3.core.Flowable<BaseTool> canonicalTools(@Nullable ReadonlyContext context)
      Constructs the list of tools for this agent based on the tools() field.
      Parameters:
      context - The context to retrieve the session state.
      Returns:
      The resolved list of tools as a Single wrapped list of BaseTool.
    • instruction

      public Instruction instruction()
    • globalInstruction

      public Instruction globalInstruction()
    • model

      public Optional<Model> model()
    • planning

      public boolean planning()
    • maxSteps

      public Optional<Integer> maxSteps()
    • generateContentConfig

      public Optional<com.google.genai.types.GenerateContentConfig> generateContentConfig()
    • includeContents

      public LlmAgent.IncludeContents includeContents()
      Returns the configured history setting, LlmAgent.IncludeContents.DEFAULT when unset. A SINGLE_TURN workflow node overrides an unset one; see includeContentsExplicit().
    • includeContentsExplicit

      @Experimental public boolean includeContentsExplicit()
      Returns whether LlmAgent.Builder.includeContents(IncludeContents) was set, which a SINGLE_TURN workflow node then uses instead of running without history.

      This method is only for use by Agent Development Kit.

    • mode

      Returns how this agent converses when it runs as a workflow node, or empty without a mode.
    • hasMode

      @Experimental public boolean hasMode(AgentMode expected)
      Returns whether this agent's own mode is expected.

      This method is only for use by Agent Development Kit.

    • isChatNode

      @Experimental public boolean isChatNode()
      Returns whether this agent runs as a AgentMode.CHAT workflow node, either set or because another agent in its tree sets a mode.

      This method is only for use by Agent Development Kit.

    • runsAsTask

      @Experimental public static boolean runsAsTask(BaseAgent agent, InvocationContext context)
      Returns whether agent runs as a AgentMode.TASK workflow node in context, so it ends its task with finish_task. A task agent run any other way runs as an agent without a mode does.

      This method is only for use by Agent Development Kit.

    • isFinishTaskTool

      @Experimental public static boolean isFinishTaskTool(Object tool)
      Returns whether tool is the finish_task tool a task agent gets.

      This method is only for use by Agent Development Kit.

    • runsAsTool

      @Experimental public static boolean runsAsTool(BaseAgent agent)
      Returns whether agent is a AgentMode.SINGLE_TURN or AgentMode.TASK agent, which its parent calls as a tool rather than transfers to.

      This method is only for use by Agent Development Kit.

    • finishesTask

      @Experimental public static boolean finishesTask(Event event)
      Returns whether event ends a task with its own finish_task call that succeeded: one the task agent answered, not the user or an agent it transferred to.

      This method is only for use by Agent Development Kit.

    • endsTaskWithError

      @Experimental public static boolean endsTaskWithError(Event event)
      Returns whether event reports an execution error of a task's run, such as a failure, rather than an error code a model gave as its finish or block reason.

      This method is only for use by Agent Development Kit.

    • delegatesTask

      @Experimental public static boolean delegatesTask(BaseTool tool)
      Returns whether tool delegates a task to a AgentMode.TASK sub-agent, whose response the delegating agent gives once the task ends.

      This method is only for use by Agent Development Kit.

    • tools

      public io.reactivex.rxjava3.core.Single<List<BaseTool>> tools()
    • toolsUnion

      public List<Object> toolsUnion()
      Returns the configured tools and toolsets, then finish_task for a AgentMode.TASK agent, then one tool per AgentMode.TASK or AgentMode.SINGLE_TURN sub-agent.
    • toolsets

      public List<BaseToolset> toolsets()
    • disallowTransferToParent

      public boolean disallowTransferToParent()
    • disallowTransferToPeers

      public boolean disallowTransferToPeers()
    • transferTargets

      @Experimental public com.google.common.collect.ImmutableList<BaseAgent> transferTargets()
      Returns the agents this one may transfer to: its sub-agents, plus its parent and peers unless disallowed, in the order the model sees them. Transfer to a parent or peer needs an LlmAgent parent, and an agent its parent calls as a tool is no transfer target.

      This method is only for use by Agent Development Kit.

    • transferTargets

      @Experimental public com.google.common.collect.ImmutableList<BaseAgent> transferTargets(InvocationContext context)
      Returns the agents this one may transfer to in context. Outside a workflow a single-turn agent cannot run as a tool, so it stays a transfer target, as before modes.

      This method is only for use by Agent Development Kit.

    • beforeModelCallback

      public List<? extends Callbacks.BeforeModelCallback> beforeModelCallback()
    • afterModelCallback

      public List<? extends Callbacks.AfterModelCallback> afterModelCallback()
    • beforeToolCallback

      public List<? extends Callbacks.BeforeToolCallback> beforeToolCallback()
    • afterToolCallback

      public List<? extends Callbacks.AfterToolCallback> afterToolCallback()
    • onModelErrorCallback

      public List<? extends Callbacks.OnModelErrorCallback> onModelErrorCallback()
    • onToolErrorCallback

      public List<? extends Callbacks.OnToolErrorCallback> onToolErrorCallback()
    • canonicalBeforeModelCallbacks

      public List<? extends Callbacks.BeforeModelCallback> canonicalBeforeModelCallbacks()
      The resolved beforeModelCallback field as a list.

      This method is only for use by Agent Development Kit.

    • canonicalAfterModelCallbacks

      public List<? extends Callbacks.AfterModelCallback> canonicalAfterModelCallbacks()
      The resolved afterModelCallback field as a list.

      This method is only for use by Agent Development Kit.

    • canonicalOnModelErrorCallbacks

      public List<? extends Callbacks.OnModelErrorCallback> canonicalOnModelErrorCallbacks()
      The resolved onModelErrorCallback field as a list.

      This method is only for use by Agent Development Kit.

    • canonicalBeforeToolCallbacks

      public List<? extends Callbacks.BeforeToolCallback> canonicalBeforeToolCallbacks()
      The resolved beforeToolCallback field as a list.

      This method is only for use by Agent Development Kit.

    • canonicalAfterToolCallbacks

      public List<? extends Callbacks.AfterToolCallback> canonicalAfterToolCallbacks()
      The resolved afterToolCallback field as a list.

      This method is only for use by Agent Development Kit.

    • canonicalOnToolErrorCallbacks

      public List<? extends Callbacks.OnToolErrorCallback> canonicalOnToolErrorCallbacks()
      The resolved onToolErrorCallback field as a list.

      This method is only for use by Agent Development Kit.

    • inputSchema

      public Optional<com.google.genai.types.Schema> inputSchema()
      Description copied from interface: Node
      Returns the schema the node's input must match. An edge into the node fails at build when its source's output schema lists a key or type this one rejects, so an open producer such as a map builds, and a FunctionNode on a Content port checks the edge against the Content schema. A node that requires all predecessors applies it to each predecessor's output, not the joined map. A run checks a node's schemas only for type, nullable, enum, anyOf, properties, required and items; it ignores minimum, maximum, pattern, lengths and format.
      Specified by:
      inputSchema in interface Node
      Overrides:
      inputSchema in class BaseNode
    • outputSchema

      public Optional<com.google.genai.types.Schema> outputSchema()
      Description copied from interface: Node
      Returns the schema that the node's output value must match. The content of a message-as-output event is not checked against this schema. A run checks a record, POJO or enum input or output in its JSON form and hands on that form: a map for a record, the name for an enum.
      Specified by:
      outputSchema in interface Node
      Overrides:
      outputSchema in class BaseNode
    • rerunOnResume

      @Experimental public boolean rerunOnResume()
      Returns true, so that a workflow resuming this agent as a node runs it again and its model sees the answers to its long-running calls.
      Specified by:
      rerunOnResume in interface Node
      Overrides:
      rerunOnResume in class BaseNode
    • executor

      public Optional<Executor> executor()
    • outputKey

      public Optional<String> outputKey()
    • codeExecutor

      public Optional<BaseCodeExecutor> codeExecutor()
    • resolvedModel

      public Model resolvedModel()
    • fromConfig

      public static LlmAgent fromConfig(LlmAgentConfig config, String configAbsPath) throws ConfigAgentUtils.ConfigurationException
      Creates an LlmAgent from configuration with full subagent support.
      Parameters:
      config - the agent configuration
      configAbsPath - The absolute path to the agent config file. This is needed for resolving relative paths for e.g. tools and subagents.
      Returns:
      the configured LlmAgent
      Throws:
      ConfigAgentUtils.ConfigurationException - if the configuration is invalid
    • close

      public io.reactivex.rxjava3.core.Completable close()
      Description copied from class: BaseAgent
      Closes all sub-agents.
      Overrides:
      close in class BaseAgent
      Returns:
      a Completable that completes when all sub-agents are closed.