Context (ADK)
https://google.github.io/adk-docs/context/
#Agent_Development_Kit
What are Context
In the Agent Development Kit (ADK), "context" refers to the crucial bundle of information available to your agent and its tools during specific operations.
contextにより可能になる
Maintaining State
Passing Data
Accessing Services
Identity and Tracking
Tool-Specific Actions
1. Framework creates the main context for this specific run
InvocationContext
_run_async_implに渡る
2. Framework calls the agent's run method, passing the context implicitly
The Different types of Context
InvocationContext
InvocationContext acts as the comprehensive internal container,
Where Used: Received as the ctx argument directly within an agent's core implementation methods (_run_async_impl, _run_live_impl).
google.adk.agents.InvocationContext
ReadonlyContext
CallbackContext
ToolContext
Common Tasks Using Context
Accessing Information
Reading Session State
context.state.get()
Getting Current Identifiers
Accessing the Initial User Input
context.user_content.parts[0].text
Managing State
Passing Data Between Tools
context.state(辞書)に代入(context.state[key] = value)して、それをget
Updating User Preferences
State Prefixes
SessionServiceによるらしい(積ん読)
Working with Artifacts
context.save_artifact()
stateのtemp:doc_artifact_nameキーでファイル名を受け渡し
context.load_artifact()
Listing Artifacts: Discover what files are available.
Handling Tool Authentication
stateの"user:my_api_credential"キー
Leveraging Memory
context.search_memory()
Advanced: Direct InvocationContext Usage
Key Takeaways & Best Practices
Use the Right Context: Always use the most specific context object provided (ToolContext in tools/tool-callbacks, CallbackContext in agent/model-callbacks, ReadonlyContext where applicable).
Use the full InvocationContext (ctx) directly in _run_async_impl / _run_live_impl only when necessary.
State for Data Flow: context.state is the primary way to share data, remember preferences, and manage conversational memory within an invocation.
Use prefixes (app:, user:, temp:) thoughtfully when using persistent storage.
Artifacts for Files: Use context.save_artifact and context.load_artifact for managing file references (like paths or URIs) or larger data blobs.
Tracked Changes: Modifications to state or artifacts made via context methods are automatically linked to the current step's EventActions and handled by the SessionService.
積ん読
Start Simple: Focus on state and basic artifact usage first.
Explore authentication, memory, and advanced InvocationContext fields (like those for live streaming) as your needs become more complex.