ADK Context Manager
Philosophy & Architecture
InvocationContext provides the "bundle" of information (session state, events, services) for a single request-response cycle (invocation).
Context Flavors
- •
InvocationContext: Comprehensive internal container; used directly in_run_async_impl. - •
CallbackContext: Shared across agent and model callbacks. - •
ToolContext: Subclass ofCallbackContextfor tool callbacks; addsfunction_call_id. - •
ReadonlyContext: Safe access for toolsets/services that don't modify state.
State Management
- •Access:
context.state(dict-like object). - •Modification:
context.state['key'] = 'value'. Changes are tracked asstate_deltain events. - •Prefixes:
- •
user:*: User-level preferences (persistent). - •
app:*: Application-wide settings (persistent). - •
temp:*: Short-lived invocation-level data. - •(No prefix): Session-specific data.
- •
Identity & Tracking
- •
invocation_id: Unique identifier for the current run. - •
agent.name: Identify the currently running agent. - •Read
references/context.mdfor full attribute definitions.
Success Criteria
- •Correct state scope usage (e.g.,
user:for preferences). - •Proper access to services (e.g.,
context.artifact_service). - •Effective use of
InvocationContextfor early termination (ctx.end_invocation = True).