feat: auto-detect 1M context window and scale guard thresholds#20
Open
ynaamane wants to merge 1 commit intoRuya-AI:mainfrom
Open
feat: auto-detect 1M context window and scale guard thresholds#20ynaamane wants to merge 1 commit intoRuya-AI:mainfrom
ynaamane wants to merge 1 commit intoRuya-AI:mainfrom
Conversation
Claude Code appends [1m] to model IDs when using extended 1M context. Cozempic was hardcoding all models to 200K, causing premature pruning (guard firing at 150K tokens instead of 750K) and incorrect context % in diagnostics. Changes: - Add [1m]-suffixed entries to MODEL_CONTEXT_WINDOWS (1M for all models) - Update detect_context_window() with bracket-aware prefix matching - Update SessionStart hook to extract context_window_size from Claude Code stdin JSON and pass --context-window dynamically to guard daemon - Display detected context window in guard banner - Scale overflow danger thresholds based on detected context window - Add 15 new tests for 1M detection, threshold scaling, and edge cases Backward compatible: 200K models keep exact same behavior. Env var COZEMPIC_CONTEXT_WINDOW override still takes priority.
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
Claude Code now appends
[1m]to model IDs when using extended 1M context (e.g.claude-opus-4-6[1m]). Cozempic v1.0.0 hardcodes all models to 200K inMODEL_CONTEXT_WINDOWS, causing:cozempic current --diagnoseshows 50% context used when it's actually 10%Changes
tokens.py: Add[1m]-suffixed entries toMODEL_CONTEXT_WINDOWS(1M context). Updatedetect_context_window()with bracket-aware prefix matching for versioned model IDs (e.g.claude-opus-4-6-20260301[1m])init.py: UpdateSessionStarthook template to extractcontext_window.context_window_sizefrom Claude Code stdin JSON and pass--context-windowdynamically to guard daemonguard.py: Always detect context window upfront (used for display + overflow scaling). Display detected context window in guard banner. Scale overflowdanger_threshold_mbanddanger_threshold_tokensbased on context windowtests/test_model_detection.py: Add 15 new tests covering 1M exact match, versioned prefix match, 200K/1M non-confusion, env override priority, threshold scaling, and context % calculationsBackward compatibility
COZEMPIC_CONTEXT_WINDOWenv var override still takes highest priority--context-window) fall back to model-based detection from session JSONLcozempic initto get the updated templateBefore/After
Test plan
cozempic current --diagnosewith 1M model shows correct context windowContext: 1.0Mon 1M sessionsecho '{"session_id":"test","context_window":{"context_window_size":1000000}}' | bash -c '<hook_command>'