feat: v0.21.0 — token budget tracking and context window early warning#37
Merged
Conversation
Adds token_budget.py with: - CONTEXT_LIMITS dict covering Anthropic, OpenAI, Google, Meta models - analyse_token_budget() — per-request accumulation table with % of limit - TokenBudgetWatcher — stateful watcher used by watch mode, fires once when cumulative input tokens cross a configurable threshold (default 90%) watch.py integrates TokenBudgetWatcher; threshold configurable via max_context_pct in .agent-watch.json or --max-context-pct CLI flag. CLI: agent-strace token-budget [session-id] [--warning-threshold 0.9] Closes #27 Co-authored-by: Ona <no-reply@ona.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #27
What
Adds
token_budget.pyto track cumulative input token usage against the model's context window limit and warn before exhaustion.Context limits
Built-in limits for Anthropic (claude-*), OpenAI (gpt-4o, o1, o3), Google (gemini-1.5/2.0), and Meta (llama-3.1) models. Prefix matching handles versioned model names like
claude-sonnet-4-20251022.analyse_token_budget()Per-request accumulation table:
TokenBudgetWatcherStateful watcher integrated into
watch.py. Fires once when cumulative input tokens cross the threshold. Configurable viamax_context_pctin.agent-watch.jsonor--max-context-pctCLI flag.CLI
Tests
tests/test_token_budget.py— 13 tests.