# InvocationContext

data class InvocationContext(val session: Session, val runConfig: RunConfig? = null, val agent: BaseAgent, val branch: String? = null, val invocationId: String = "e-" + Uuid.random(), val artifactService: ArtifactService? = null, val memoryService: MemoryService? = null, val sessionService: SessionService? = null, val resumabilityConfig: ResumabilityConfig? = null, val userContent: Content? = null, val agentStates: MutableMap<String, TypedData> = concurrentMutableMapOf(), val endOfAgents: MutableMap<String, Boolean> = concurrentMutableMapOf(), val extraTools: MutableMap<String, BaseTool> = concurrentMutableMapOf(), var isEndOfInvocation: Boolean = false, var isPaused: Boolean = false, val pluginManager: PluginManager = PluginManager())

An invocation context represents the data of a single invocation of an agent.

An invocation:

1. Starts with a user message and ends with a final response.

2. Can contain one or multiple agent calls.

3. Is handled by runner.run_async().

An invocation runs an agent until it does not request to transfer to another agent.

An agent call:

1. Is handled by agent.run().

2. Ends when agent.run() ends.

An LLM agent call is an agent with a BaseLLMFlow. An LLM agent call can contain one or multiple steps.

An LLM agent runs steps in a loop until:

1. A final response is generated.

2. The agent transfers to another agent.

3. The end_invocation is set to true by any callbacks or tools.

A step:

1. Calls the LLM only once and yields its response.

2. Calls the tools and yields their responses if requested.

The summarization of the function response is considered another step, since it is another llm call. A step ends when it's done calling llm and tools, or if the end_invocation is set to true at any time.

        ┌─────────────────────── invocation ──────────────────────────┐
        ┌──────────── llm_agent_call_1 ────────────┐ ┌─ agent_call_2 ─┐
        ┌──── step_1 ────────┐ ┌───── step_2 ──────┐
        [call_llm] [call_tool] [call_llm] [transfer]

Content copied to clipboard

## Constructors

InvocationContext
Link copied to clipboard
constructor(session: Session, runConfig: RunConfig? = null, agent: BaseAgent, branch: String? = null, invocationId: String = "e-" + Uuid.random(), artifactService: ArtifactService? = null, memoryService: MemoryService? = null, sessionService: SessionService? = null, resumabilityConfig: ResumabilityConfig? = null, userContent: Content? = null, agentStates: MutableMap<String, TypedData> = concurrentMutableMapOf(), endOfAgents: MutableMap<String, Boolean> = concurrentMutableMapOf(), extraTools: MutableMap<String, BaseTool> = concurrentMutableMapOf(), isEndOfInvocation: Boolean = false, isPaused: Boolean = false, pluginManager: PluginManager = PluginManager())

## Properties

agent
Link copied to clipboard
val agent: BaseAgent

The current agent of this invocation context. Readonly.

agentStates
Link copied to clipboard
val agentStates: MutableMap<String, TypedData>

The state of the agent for this invocation.

artifactService
Link copied to clipboard
val artifactService: ArtifactService? = null
branch
Link copied to clipboard
val branch: String? = null

The branch of the invocation context.

endOfAgents
Link copied to clipboard
val endOfAgents: MutableMap<String, Boolean>

The end of agent status for each agent in this invocation.

extraTools
Link copied to clipboard
val extraTools: MutableMap<String, BaseTool>

Extra tools injected dynamically during invocation (e.g., by SequentialAgent).

invocationId
Link copied to clipboard
val invocationId: String

The id of this invocation context. Readonly.

isEndOfInvocation
Link copied to clipboard
@Volatile
var isEndOfInvocation: Boolean

Whether to end this invocation.

isPaused
Link copied to clipboard
@Volatile
var isPaused: Boolean

Whether this invocation is paused.

isResumable
Link copied to clipboard
val isResumable: Boolean

Returns whether the current invocation is resumable.

memoryService
Link copied to clipboard
val memoryService: MemoryService? = null
pluginManager
Link copied to clipboard
val pluginManager: PluginManager

The manager for keeping track of plugins in this invocation.

resumabilityConfig
Link copied to clipboard
val resumabilityConfig: ResumabilityConfig? = null

Optional resumability configuration for this invocation.

runConfig
Link copied to clipboard
val runConfig: RunConfig? = null

Configurations for live agents under this invocation.

session
Link copied to clipboard
val session: Session

The current session of this invocation context. Readonly.

sessionService
Link copied to clipboard
val sessionService: SessionService? = null
userContent
Link copied to clipboard
val userContent: Content? = null

The user content that started this invocation. Readonly.

## Functions

branch
Link copied to clipboard
fun branch(childAgent: BaseAgent): InvocationContext

Creates a new InvocationContext for a child agent, derived from this context. Appends the given agent's name to the branch path.

executeSingleFunctionCall
Link copied to clipboard
suspend fun executeSingleFunctionCall(functionCall: FunctionCall, tools: Map<String, BaseTool>, toolConfirmation: ToolConfirmation? = null): Event?

Executes a single function call synchronously and builds a corresponding response event.

findMatchingFunctionCall
Link copied to clipboard
suspend fun findMatchingFunctionCall(functionResponseEvent: Event): Event?

Finds the function call event in the current invocation that matches the function response id.

getEvents
Link copied to clipboard
suspend fun getEvents(currentInvocation: Boolean = false, currentBranch: Boolean = false): List<Event>

Returns the events from the current session.

handleFunctionCalls
Link copied to clipboard
suspend fun handleFunctionCalls(functionCalls: List<FunctionCall>, tools: Map<String, BaseTool>, filters: Set<String> = emptySet(), toolConfirmations: Map<String, ToolConfirmation>? = null): Event?

Processes a list of function calls by executing them efficiently and safely.

populateInvocationAgentStates
Link copied to clipboard
suspend fun populateInvocationAgentStates()

Populates agent states for the current invocation if it is resumable.

resetSubAgentStates
Link copied to clipboard
fun resetSubAgentStates(agentName: String)

Resets the state of all sub-agents of the given agent recursively.

setAgentState
Link copied to clipboard
fun setAgentState(agentName: String, agentState: TypedData? = null, endOfAgent: Boolean = false)

Set state of an agent explicitly. Does not implicitly initialize.

shouldPauseInvocation
Link copied to clipboard
fun shouldPauseInvocation(event: Event): Boolean

Returns whether to pause the invocation right after this event.