Class InvocationContext

java.lang.Object
com.google.adk.agents.InvocationContext

public class InvocationContext extends Object
The context for an agent invocation.
  • Constructor Details

  • Method Details

    • builder

      public static InvocationContext.Builder builder()
      Returns a new InvocationContext.Builder for creating InvocationContext instances.
    • toBuilder

      public InvocationContext.Builder toBuilder()
      Returns a InvocationContext.Builder initialized with the values of this instance.
    • sessionService

      public BaseSessionService sessionService()
      Returns the session service for managing session state.
    • artifactService

      public BaseArtifactService artifactService()
      Returns the artifact service for persisting artifacts.
    • memoryService

      public BaseMemoryService memoryService()
      Returns the memory service for accessing agent memory.
    • pluginManager

      public Plugin pluginManager()
      Returns the plugin manager for accessing tools and plugins.
    • activeStreamingTools

      public Map<String, ActiveStreamingTool> activeStreamingTools()
      Returns a map of tool call IDs to active streaming tools for the current invocation.
    • liveRequestQueue

      public Optional<LiveRequestQueue> liveRequestQueue()
      Returns the queue for managing live requests, if available for this invocation.
    • invocationId

      public String invocationId()
      Returns the unique ID for this invocation.
    • branch

      public void branch(@Nullable String branch)
      Sets the [branch] ID for the current invocation. A branch represents a fork in the conversation history.
    • branch

      public Optional<String> branch()
      Returns the branch ID for the current invocation, if one is set. A branch represents a fork in the conversation history.
    • agent

      public BaseAgent agent()
      Returns the agent being invoked.
    • node

      @Experimental public Optional<Node> node()
      Returns the workflow node this invocation runs, or empty when it runs an agent. When it runs a node, agent() returns a NodeViewAgent that stands in for it.
    • agentMode

      @Experimental public Optional<AgentMode> agentMode(BaseAgent agent)
      Returns the mode agent runs in as a workflow node in this context, or empty when it runs without one.

      This method is only for use by Agent Development Kit.

    • stepsTaken

      @Experimental public int stepsTaken(BaseAgent agent)
      Returns the model steps agent has taken in its workflow node run, which its delegation rounds share, or 0 outside such a run.

      This method is only for use by Agent Development Kit.

    • nodePath

      @Experimental public Optional<String> nodePath()
      Returns the path of the workflow node the agent runs as, or empty outside a workflow. Sub-agents of the node share this path.

      This method is only for use by Agent Development Kit.

    • isolationScope

      @Experimental public Optional<String> isolationScope()
      Returns the isolation scope of the workflow node that the agent runs as, or empty when the node is unscoped or outside a workflow.

      This method is only for use by Agent Development Kit.

    • isOtherNodeInput

      @Experimental public boolean isOtherNodeInput(Event event)
      Returns whether event is a user turn that a workflow node other than this context's added as its input, which the agent here does not see. Outside a node, every node's input is another node's.

      This method is only for use by Agent Development Kit.

    • session

      public Session session()
      Returns the session associated with this invocation.
    • eventsOnCurrentBranch

      public com.google.common.collect.ImmutableList<Event> eventsOnCurrentBranch()
      Returns a snapshot of the session's events, keeping only those on the branch this invocation is running on, without other workflow nodes' inputs.

      The rule is author-asymmetric on purpose, so a confirmation the user answered on a sub-branch stays visible while a descendant agent's own events do not.

    • userContent

      public Optional<com.google.genai.types.Content> userContent()
      Returns the user content that triggered this invocation, if any.
    • runConfig

      public RunConfig runConfig()
      Returns the configuration for the current agent run.
    • callbackContextData

      public Map<String,Object> callbackContextData()
      Returns a map for storing temporary context data that can be shared between different parts of the invocation (e.g., before/on/after model callbacks).
    • endInvocation

      public boolean endInvocation()
      Returns whether this invocation should be ended, e.g., due to reaching a terminal state or error.
    • setEndInvocation

      public void setEndInvocation(boolean endInvocation)
      Sets whether this invocation should be ended.
    • appName

      public String appName()
      Returns the application name associated with the session.
    • userId

      public String userId()
      Returns the user ID associated with the session.
    • scheduler

      public io.reactivex.rxjava3.core.Scheduler scheduler()
      Returns the Scheduler on which this invocation runs the work ADK hands to worker threads: the sub-agents of a ParallelAgent, tool calls in ToolExecutionMode.PARALLEL_SUBSCRIBE mode, the nodes of a workflow graph and the items of a parallelNode, workflow node retries and attempts with a timeout, the hop that starts the live send loop, and the work that resumes after a wait for the Runner to save events. A workflow FunctionNode decodes its input and runs its confirmation predicate and body on it, unless FunctionNode.Builder#scheduler sets another; then only the hand-back after them runs on it. An agent-level setting ( ParallelAgent.Builder#scheduler, LlmAgent.Builder#executor) takes precedence over this scheduler, which defaults to Schedulers.io(). A bounded scheduler limits how many thread-blocking graph nodes run at once, and it deadlocks once every thread holds a node blocked on runNode(...).blockingGet() for a child, such as a nested workflow or a FunctionNode, that also needs a thread.
    • instantSource

      public InstantSource instantSource()
      Returns the InstantSource for this invocation.
    • uuidProvider

      public UuidProvider uuidProvider()
      Returns the UuidProvider for this invocation.
    • now

      public Instant now()
      Returns the current time from this invocation's InstantSource.
    • newUuid

      public String newUuid()
      Returns a new unique identifier from this invocation's UuidProvider.
    • newInvocationContextId

      public static String newInvocationContextId()
      Generates a new unique ID for an invocation context.
    • newInvocationContextId

      public static String newInvocationContextId(UuidProvider uuidProvider)
      Generates a new unique ID for an invocation context using the given UuidProvider.
    • incrementLlmCallsCount

      public void incrementLlmCallsCount() throws LlmCallsLimitExceededException
      Increments the count of LLM calls made during this invocation and throws an exception if the limit defined in RunConfig is exceeded. In a workflow node run it also counts a model step of the node's agent, which its delegation rounds share.
      Throws:
      LlmCallsLimitExceededException - if the call limit is exceeded
    • eventsCompactionConfig

      public Optional<EventsCompactionConfig> eventsCompactionConfig()
      Returns the events compaction configuration for the current agent run.
    • contextCacheConfig

      public Optional<ContextCacheConfig> contextCacheConfig()
      Returns the context cache configuration for the current agent run.
    • isResumable

      @Experimental public boolean isResumable()
      Returns whether the current invocation is resumable.
    • isLegacyResumability

      @Deprecated public boolean isLegacyResumability()
      Deprecated.
      Reports the deprecated plain-text continuation shim and goes away with it; use isResumable().
      Returns whether the invocation runs the legacy resumption flow: the behavior isResumable() had before durable checkpoints existed. It is a separate flow, not a weaker isResumable(), so a caller wanting either has to ask for both.
    • agentStates

      @Experimental public Map<String, Map<String,Object>> agentStates()
      Returns an unmodifiable view of the per-agent resumability checkpoint states for this invocation, keyed by agent name. The backing map is shared by reference across derived contexts within the invocation; mutate it only through setAgentState(String, Map, boolean).
    • endOfAgents

      @Experimental public Map<String,Boolean> endOfAgents()
      Returns an unmodifiable view of the per-agent end-of-agent flags for this invocation, keyed by agent name.
    • populateInvocationAgentStates

      @Experimental public void populateInvocationAgentStates()
      Rehydrates agentStates() and endOfAgents() from the current invocation's history when this invocation is resumable. For each event carrying agent-state information, sets the authoring agent's checkpoint; for a non-workflow author that already produced content, seeds an empty state so it is treated as mid-run.
    • events

      @Experimental public com.google.common.collect.ImmutableList<Event> events(boolean currentInvocation, boolean currentBranch)
      Returns the current session's events, optionally filtered to the current invocation and/or the current branch. Reads the in-memory Session.events() list, which BaseSessionService.appendEvent(Session, Event) keeps in sync. A null-branch event is visible on any branch, and other workflow nodes' inputs never are.
      Parameters:
      currentInvocation - whether to filter to events from this invocation.
      currentBranch - whether to filter to events on this branch (or with no branch).
    • shouldPauseInvocation

      @Experimental public boolean shouldPauseInvocation(Event event)
      Returns whether to pause the invocation right after this event: it is resumable and the event carries a long-running function call, including a synthetic adk_request_confirmation HITL request. Pausing, unlike ending, leaves the invocation resumable.
    • equals

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

      public int hashCode()
      Overrides:
      hashCode in class Object