Overview
TheInvocationContext class represents the data and state of a single agent invocation. It tracks the execution lifecycle, manages services, handles cost tracking, and provides context propagation for multi-agent workflows.
Location: @iqai/adk package, exported from packages/adk/src/agents/invocation-context.ts:124
Key Concepts
Invocation Hierarchy
An invocation:- Starts with a user message and ends with a final response
- Can contain one or multiple agent calls
- Is handled by
runner.runAsync() - Runs an agent until it doesn’t request transfer to another agent
- Is handled by
agent.run() - Ends when
agent.run()completes
- Contains one or multiple steps
- Runs steps in a loop until:
- A final response is generated
- The agent transfers to another agent
endInvocationis set to true
- Calls the LLM once and yields its response
- Calls tools if requested and yields their responses
- Ends when done calling LLM and tools, or if
endInvocationis set to true
Constructor
Configuration options for the invocation context
Session service for managing conversation state
The current agent for this context
The current session instance
Plugin manager for lifecycle hooks
Unique invocation ID (auto-generated if not provided)
Branch identifier for multi-agent workflows (format: “agent_1.agent_2.agent_3”)
The user content that started this invocation
Memory service for long-term storage
Artifact service for file management
Whether to end this invocation
Queue for receiving live requests
Running streaming tools of this invocation
Cache for audio/video transcription data
Runtime configuration for this invocation
Context caching configuration
Transfer context for multi-agent workflows
Telemetry span counters (llm, tool, agent)
packages/adk/src/agents/invocation-context.ts:221
Properties
Readonly Properties
Unique identifier for this invocation.
Session service instance.
Memory service instance if configured.
Artifact service instance if configured.
Plugin manager for lifecycle hooks.
Branch identifier showing the agent hierarchy path (e.g., “agent_1.agent_2.agent_3”). Used when sub-agents shouldn’t see peer agents’ conversation history.
Current session instance containing conversation history and state.
The user content that started this invocation.
Context caching configuration. When present, enables reuse of cached LLM context across agent calls.
Mutable Properties
The current agent for this context.
Set to true in callbacks or tools to terminate this invocation.
Queue to receive live audio/video requests.
Currently running streaming tools.
Cache for audio/video transcription data.
Runtime configuration for this invocation.
Transfer context tracking agent transfer chain for multi-agent workflows.
Computed Properties
appName (getter)
Gets the application name from the session. Returns:string
packages/adk/src/agents/invocation-context.ts:266
userId (getter)
Gets the user ID from the session. Returns:string
packages/adk/src/agents/invocation-context.ts:273
Methods
incrementLlmCallCount()
Tracks the number of LLM calls made and enforces the limit if configured. Throws:LlmCallsLimitExceededError if the limit is exceeded.
packages/adk/src/agents/invocation-context.ts:282
nextLlmSpanIndex()
Returns the next LLM span index for telemetry. Returns:number
packages/adk/src/agents/invocation-context.ts:291
nextToolSpanIndex()
Returns the next tool span index for telemetry. Returns:number
packages/adk/src/agents/invocation-context.ts:298
nextAgentSpanIndex()
Returns the next agent span index for telemetry. Returns:number
packages/adk/src/agents/invocation-context.ts:305
getSpanCounters()
Exposes shared span counters for child contexts. Returns:SpanCounters
packages/adk/src/agents/invocation-context.ts:312
createChildContext()
Creates a child invocation context for a sub-agent. Inherits most properties from parent while updating agent-specific fields.The sub-agent for the child context
InvocationContext
packages/adk/src/agents/invocation-context.ts:319
Transfer Context
TheTransferContext interface tracks multi-agent workflows:
Cost Management
The internalInvocationCostManager tracks invocation costs:
Utility Functions
newInvocationContextId()
Generates a new invocation context ID. Returns:string
packages/adk/src/agents/invocation-context.ts:80
Exceptions
LlmCallsLimitExceededError
Thrown when the number of LLM calls exceeds the configured limit.packages/adk/src/agents/invocation-context.ts:37
Usage Examples
Type Definitions
See Also
- BaseAgent - Base class that uses InvocationContext
- AgentBuilder - Creates agents that run with InvocationContext
- LlmAgent - LLM agent that uses InvocationContext