Context caching and compression for better performance.
Blog Post: https://arjunprabhulal.com/adk-context-management/
Context management optimizes how agents handle conversation history and system prompts:
- Context Caching - Reduce API calls by caching static content
- Context Compression - Summarize long conversations to stay within token limits
- Python 3.10+
- Gemini API key from AI Studio
- Navigate to this module:
cd 17-context-management- Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate- Install dependencies:
pip install -r ../requirements.txt- Set up environment variables in
context_agent/.env:
GOOGLE_API_KEY=your-api-key-here
Cache frequently used context to reduce costs:
from google.adk.agents.config import ContextCacheConfig
cache_config = ContextCacheConfig(
max_entries=100,
ttl_seconds=3600
)Summarize long conversations:
from google.adk.agents.config import EventsCompactionConfig
compaction_config = EventsCompactionConfig(
max_events=50,
compaction_strategy="summarize"
)cd context_agent
python agent.pyThe demo shows:
- Caching configuration
- Compression strategies
- Performance optimization
Continue to 18. Callbacks