Context 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
| Field | Type | Description |
|---|---|---|
invocation_id | str | Unique ID for this invocation (auto-generated) |
session_id | str | The session this invocation belongs to |
user_id | str | The user who initiated the session |
app_name | str | The application running the agent |
agent_name | str | The name of the currently executing agent |
branch | str | Dot-separated path for nested execution (e.g. "root.child") |
state | dict | Mutable key-value store shared across the call tree |
session | Session | Session with conversation history and persisted state |
run_config | dict | LangChain RunnableConfig for callbacks/tracing |
memory_service | Any | Optional memory service for long-term recall |
event_callback | Callable | Optional callback for pushing events to the parent stream |
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.