feat: context-budget estimator (system + MCP + skills + memory)#37
Merged
0bserver07 merged 1 commit intomainfrom May 1, 2026
Merged
feat: context-budget estimator (system + MCP + skills + memory)#370bserver07 merged 1 commit intomainfrom
0bserver07 merged 1 commit intomainfrom
Conversation
Surface the per-session "context tax" — the tokens every Claude Code turn pays before the user types: system prompt + registered MCP servers + available skills + agent definitions + memory files. New service + CLI command + HTTP route, with the bloat finding wired into the optimize report at >20k tokens. The heuristic is intentionally simple (len(text) // 4 per file; per-MCP base+tools) and documented in every output payload, so users know the numbers are rough — built for spotting bloat, not billing. Defensive: missing files contribute zero-token slices rather than raising. 28 new tests; total suite 910 passed, 2 skipped. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
099ee87 to
70e11e1
Compare
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
stackunderflow.services.context_budgetmodule: estimates the per-session "context tax" by walking visible config files (project + globalCLAUDE.md,~/.claude.jsonmcpServers,~/.claude/skills/*/SKILL.md, project + global.claude/agents/*.md). Returns aContextBudgetwithslices: list[ContextSlice],total_tokens,cost_per_session_usd, andestimated_monthly_cost_usd.stackunderflow context-budgetCLI command with--project DIR,--global, and--format text|jsonflags; newGET /api/context-budget?project=<slug>HTTP route returning the same shape.stackunderflow.reports.optimizevia a siblingfind_context_budget_findings(conn, *, threshold=20_000)that emitskind="context_budget_bloat"severity="medium"findings for the global overhead and any over-threshold project, with the top 5 contributing slices included.heuristicfield, CHANGELOG,docs/cli-reference.md):len(text) // 4per file;200 + 50/toolper MCP server (or a flat 200 fallback when tool counts aren't statically known). Useful for spotting bloat, not billing.Sample output (this repo)
Test plan
pytest tests/ -q— 910 passed, 2 skipped (was 882; +28 new tests)ruff checkclean on all touched files~/.claude/config (output above)find_context_budget_findingsemits a finding when budget > threshold and stays silent when under🤖 Generated with Claude Code