InvocationContext is the runtime state passed through every agent in the call tree. It carries the session binding, a mutable shared state dict, run config, and an optional event callback for real-time event pushing.
Fields
Core Identity
State & Session
Control Flow
Services & Callbacks
Event Signing
Derived contexts
Sub-agents receive a derived context with their ownagent_name and branch for isolation, while sharing the same state reference and event_callback:
SequentialAgent, ParallelAgent, LoopAgent) and AgentTool call derive() automatically. AgentTool additionally calls clear_session() to give the child a fresh context without the parent’s conversation history.
clear_session()
Returns a copy of the context with an empty session - same IDs but no conversation history. Used byAgentTool to give child agents a clean slate:
event_callback
event_callback is an optional callable that tools can use to push events to the parent agent’s event stream in real-time. LlmAgent sets this automatically before tool execution using an asyncio.Queue, so AgentTool (and any custom tool) can emit events while running:
derive(), so nested sub-agents also push events up to the root.
Methods
get_events(*, current_branch=False, current_invocation=False)
Return session events with optional filtering. When current_branch=True, only events matching this context’s branch are returned. When current_invocation=True, only events from this invocation ID are returned.
get_previous_final_responses()
Return final response events from previous invocations (all branches). Useful for giving agents context about what happened in prior turns.
set_agent_state(agent_name, *, agent_state=None, end_of_agent=False)
Set or clear execution state for a named agent. Used by composite agents to track child progress. When end_of_agent=True, marks the agent as finished and clears its state.
reset_sub_agent_states(agent_name)
Recursively reset the state of all sub-agents of the named agent. Called by LoopAgent between iterations so child agents start fresh each cycle.
should_pause_invocation(event)
Return True if the invocation should pause after this event. Triggered when is_resumable=True and the event contains tool calls with IDs in long_running_tool_ids.