Skip to content

feat(sessions): Efficient Large Context Handling for Agent Development Kit #1247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Adewale-1
Copy link

Fixes #1246

Problem

With Gemini's introduction of massive context windows (1M-2M tokens), existing approaches to context management in ADK have become inefficient:

  1. Serialization Bottleneck: Full context serialization/deserialization with each state update creates significant performance overhead.
  2. Memory Inefficiency: Duplicate contexts across agents waste memory.
  3. Missing Caching Mechanisms: No integration with Gemini's context caching capabilities.
  4. Scale Limitations: Memory usage grows linearly with agent count, limiting multi-agent applications.

Solution

This PR introduces a reference-based approach to context management with three new components:

  1. ContextReferenceStore: A lightweight store that manages large contexts efficiently
  2. ContextMetadata: Tracks information about stored contexts
  3. LargeContextState: Extends ADK's State class with reference-based context handling

Performance Improvements

Testing with 500K tokens (approximately 1.92 MB of data):

Metric Traditional State Reference-Based Improvement
Serialization Time 0.0332 seconds 0.0001 seconds 415.40x faster
Deserialization Time 0.0162 seconds 0.0019 seconds 8.35x faster
Serialized Size 1.92 MB 0.00 MB 15,944x smaller

With 50 agents sharing 100K tokens of context:

Approach Memory Usage
Traditional 19.44 MB
Reference-Based 0.40 MB
Reduction Factor 49.07x smaller

Changes

  • Added context_reference_store.py with ContextReferenceStore and ContextMetadata classes
  • Added large_context_state.py with LargeContextState class
  • Added unit tests for the implementation
  • Added documentation and examples

Testing

  • Comprehensive unit tests for all new components
  • Performance benchmarks comparing to traditional approach
  • Integration tests with ADK agents

How to Run Tests

To run the unit tests for this implementation:

# Run all tests
python -m pytest tests/unittests/sessions/test_large_context_state.py tests/unittests/utils/test_langgraph_utils.py -v

# Run specific test class
python -m pytest tests/unittests/sessions/test_large_context_state.py::TestContextReferenceStore -v

# Run with coverage
python -m pytest tests/unittests/sessions/test_large_context_state.py tests/unittests/utils/test_langgraph_utils.py --cov=google.adk.sessions --cov=google.adk.utils

For performance benchmarks:

# Run the benchmark script
python src/google/adk/examples/large_context_example.py --benchmark

Compatibility

This implementation is fully backward compatible with existing ADK code:

  • Does not modify any existing classes or functions
  • Provides optional enhancements via new classes

Documentation

Full documentation is provided in:

  • Code docstrings
  • Example usage in tests

Usage Examples

Basic Usage

from google.adk.sessions.large_context_state import LargeContextState
from google.adk.agents import LlmAgent

# Create an agent with large context support
agent = LlmAgent(
    name="DocumentAnalyzer",
    model="gemini-1.5-pro",
    description="Analyzes large documents",
    state_cls=LargeContextState  # Use our enhanced state class
)

# Add large context to the state
with agent.session() as session:
    state = session.state
    context_ref = state.add_large_context(large_document_text)

    # Use reference in prompt
    response = agent.generate_content(
        f"Analyze the document referenced by {context_ref}",
        state=state
    )

Multi-Agent Context Sharing

from google.adk.sessions.context_reference_store import ContextReferenceStore
from google.adk.sessions.large_context_state import LargeContextState
from google.adk.agents import LlmAgent, SequentialAgent

# Create a shared context store
context_store = ContextReferenceStore()

# Create multiple agents that share the same context store
agent1 = LlmAgent(
    name="Researcher",
    model="gemini-1.5-pro",
    state_cls=lambda: LargeContextState(context_store=context_store)
)

agent2 = LlmAgent(
    name="Summarizer",
    model="gemini-1.5-flash",
    state_cls=lambda: LargeContextState(context_store=context_store)
)

# Sequential agent with shared context
sequential = SequentialAgent(
    name="DocumentProcessor",
    sub_agents=[agent1, agent2],
    state_cls=lambda: LargeContextState(context_store=context_store)
)

# The context is shared across all agents efficiently

Future Work

Potential future enhancements:

  1. Persistence options for the context store (Redis, disk, cloud storage)
  2. Advanced caching strategies (TTL-based eviction, LRU policies)
  3. Partial context loading capabilities
  4. Framework adapters for LangGraph, LangChain, etc.

Checklist

  • Code follows ADK style guidelines
  • Tests added for all new functionality
  • Documentation added/updated
  • Performance benchmarks included
  • Backward compatibility maintained
  • Examples of usage provided

@Adewale-1 Adewale-1 changed the title Efficient Large Context Handling for Agent Development Kit feat(sessions): Efficient Large Context Handling for Agent Development Kit Jun 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Efficient Large Context Window Handling for Gemini Models
1 participant