feat(sessions): Efficient Large Context Handling for Agent Development Kit #1247
+1,395
−46
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1246
Problem
With Gemini's introduction of massive context windows (1M-2M tokens), existing approaches to context management in ADK have become inefficient:
Solution
This PR introduces a reference-based approach to context management with three new components:
ContextReferenceStore
: A lightweight store that manages large contexts efficientlyContextMetadata
: Tracks information about stored contextsLargeContextState
: Extends ADK'sState
class with reference-based context handlingPerformance Improvements
Testing with 500K tokens (approximately 1.92 MB of data):
With 50 agents sharing 100K tokens of context:
Changes
context_reference_store.py
withContextReferenceStore
andContextMetadata
classeslarge_context_state.py
withLargeContextState
classTesting
How to Run Tests
To run the unit tests for this implementation:
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:
Documentation
Full documentation is provided in:
Usage Examples
Basic Usage
Multi-Agent Context Sharing
Future Work
Potential future enhancements:
Checklist