babylon.rag.context_window.manager
Context window manager for token-aware content prioritization.
This module implements dynamic context window management for RAG systems, ensuring AI model context limits are respected while maximizing information density. Key features:
Token counting and capacity tracking
Priority-based content eviction (relevance, recency, hybrid strategies)
Automatic optimization at configurable thresholds
Metrics integration for monitoring and tuning
The manager is designed for high-throughput scenarios where context must be continuously optimized as new content arrives and old content ages.
Example
>>> from babylon.rag.context_window import ContextWindowManager
>>> manager = ContextWindowManager()
>>> manager.add_content("key1", "Some text content", importance=0.8)
>>> manager.current_usage
4 # tokens
Classes
|
Manages the token usage and content prioritization in the RAG context window. |
- class babylon.rag.context_window.manager.ContextWindowManager(config=None, metrics_collector=None, lifecycle_manager=None)[source]
Bases:
objectManages the token usage and content prioritization in the RAG context window.
The ContextWindowManager ensures that the total token usage stays within the limits of the AI model while prioritizing the most relevant content. It implements:
Token counting and tracking
Content prioritization based on relevance
Automatic optimization when approaching limits
Integration with metrics collection
- Parameters:
config (ContextWindowConfig | None)
metrics_collector (MetricsCollector | None)
lifecycle_manager (Any)
- config
Configuration for the context window
- metrics_collector
Collector for performance metrics
- lifecycle_manager
Optional manager for object lifecycles
- __init__(config=None, metrics_collector=None, lifecycle_manager=None)[source]
Initialize the context window manager.
- Parameters:
config (
ContextWindowConfig|None) – Configuration for token limits and optimization thresholdsmetrics_collector (
MetricsCollector|None) – Collector for performance metricslifecycle_manager (
Any) – Optional manager for object lifecycles
- add_content(content_id, content, token_count, importance=0.5)[source]
Add content to the context window.
- Parameters:
- Return type:
- Returns:
True if content was added successfully, False if optimization was required
- Raises:
ContentInsertionError – If content could not be added
CapacityExceededError – If capacity is exceeded and optimization fails