feat(context): implement token budget management (Phase 2)

Add TokenCalculator with LLM Gateway integration for accurate token
counting with in-memory caching and fallback character-based estimation.
Implement TokenBudget for tracking allocations per context type with
budget enforcement, and BudgetAllocator for creating budgets based on
model context window sizes.

- TokenCalculator: MCP integration, caching, model-specific ratios
- TokenBudget: allocation tracking, can_fit/allocate/deallocate/reset
- BudgetAllocator: model context sizes, budget creation and adjustment
- 35 comprehensive tests covering all budget functionality

Part of #61 - Context Management Engine

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-04 02:13:23 +01:00
parent 22ecb5e989
commit dfa75e682e
5 changed files with 1277 additions and 0 deletions

View File

@@ -14,11 +14,18 @@ Usage:
ConversationContext,
TaskContext,
ToolContext,
TokenBudget,
BudgetAllocator,
TokenCalculator,
)
# Get settings
settings = get_context_settings()
# Create budget for a model
allocator = BudgetAllocator(settings)
budget = allocator.create_budget_for_model("claude-3-sonnet")
# Create context instances
system_ctx = SystemContext.create_persona(
name="Code Assistant",
@@ -27,6 +34,13 @@ Usage:
)
"""
# Budget Management
from .budget import (
BudgetAllocator,
TokenBudget,
TokenCalculator,
)
# Configuration
from .config import (
ContextSettings,
@@ -67,6 +81,10 @@ from .types import (
)
__all__ = [
# Budget Management
"BudgetAllocator",
"TokenBudget",
"TokenCalculator",
# Configuration
"ContextSettings",
"get_context_settings",