Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

17. Context Management

Context caching and compression for better performance.

Blog Post: https://arjunprabhulal.com/adk-context-management/

Table of Contents

  1. Overview
  2. Prerequisites
  3. Setup Steps
  4. Context Strategies
  5. Running the Demo
  6. Next Steps

Overview

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

Prerequisites

Setup Steps

  1. Navigate to this module:
cd 17-context-management
  1. Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Install dependencies:
pip install -r ../requirements.txt
  1. Set up environment variables in context_agent/.env:
GOOGLE_API_KEY=your-api-key-here

Context Strategies

Context Caching

Cache frequently used context to reduce costs:

from google.adk.agents.config import ContextCacheConfig

cache_config = ContextCacheConfig(
    max_entries=100,
    ttl_seconds=3600
)

Context Compression

Summarize long conversations:

from google.adk.agents.config import EventsCompactionConfig

compaction_config = EventsCompactionConfig(
    max_events=50,
    compaction_strategy="summarize"
)

Running the Demo

cd context_agent
python agent.py

The demo shows:

  • Caching configuration
  • Compression strategies
  • Performance optimization

Next Steps

Continue to 18. Callbacks