Default to token-based guard thresholds; make system overhead configurable#10
Open
conal wants to merge 1 commit intoRuya-AI:mainfrom
Open
Default to token-based guard thresholds; make system overhead configurable#10conal wants to merge 1 commit intoRuya-AI:mainfrom
conal wants to merge 1 commit intoRuya-AI:mainfrom
Conversation
…rable Guard now computes token thresholds automatically (75%/45% of context window) when --threshold-tokens is not passed. Previously, token checking was entirely skipped without explicit flags, so the guard only watched file size (50MB default). Sessions with high token density but small file size (e.g. meaning-rich conversations without build output) could hit context limits long before file size thresholds. Also makes SYSTEM_OVERHEAD_TOKENS configurable via --system-overhead-tokens flag or COZEMPIC_SYSTEM_OVERHEAD_TOKENS env var. The default (21K) is conservative; sessions with heavy rules files, MCP servers, and tool schemas can have 30K-40K+ tokens of system overhead, causing the heuristic to underestimate context usage. Fixes Ruya-AI#8, fixes Ruya-AI#9. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Mar 9, 2026
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.
Summary
--threshold-tokens. Previously, token checking was entirely skipped without this flag, so the guard only watched file size (50MB default).SYSTEM_OVERHEAD_TOKENSis now configurable via--system-overhead-tokensflag orCOZEMPIC_SYSTEM_OVERHEAD_TOKENSenv var. The default (21K) underestimates overhead for sessions with heavy rules files, MCP servers, and tool schemas (which can reach 30K-40K+).Motivation
Sessions with high token density but small file size (meaning-rich conversations without build output) could hit context limits long before the 50MB file size threshold. In our case, Cozempic's guard reported 68% context usage while the client reported 85%, and compaction triggered before the guard intervened.
Root causes:
--threshold-tokens), not the defaultChanges
tokens.py: Addedget_system_overhead_tokens()(env var override),default_token_thresholds()(75%/45% of context window). Replaced hardcodedSYSTEM_OVERHEAD_TOKENSin heuristic and calibration paths.guard.py:start_guard()now computes default token thresholds from the session's context window when none are passed.cli.py: Added--system-overhead-tokensglobal flag. Updated help text for--threshold-tokensand--soft-threshold-tokens.Test plan
test_tokens.pytests passdefault_token_thresholds()returns (150000, 90000) for 200K contextCOZEMPIC_SYSTEM_OVERHEAD_TOKENSenv var override worksFixes #8, fixes #9.
🤖 Generated with Claude Code