Skip to content

feat: context-budget estimator (system + MCP + skills + memory)#37

Merged
0bserver07 merged 1 commit intomainfrom
feat/context-budget
May 1, 2026
Merged

feat: context-budget estimator (system + MCP + skills + memory)#37
0bserver07 merged 1 commit intomainfrom
feat/context-budget

Conversation

@0bserver07
Copy link
Copy Markdown
Owner

Summary

  • New stackunderflow.services.context_budget module: estimates the per-session "context tax" by walking visible config files (project + global CLAUDE.md, ~/.claude.json mcpServers, ~/.claude/skills/*/SKILL.md, project + global .claude/agents/*.md). Returns a ContextBudget with slices: list[ContextSlice], total_tokens, cost_per_session_usd, and estimated_monthly_cost_usd.
  • New stackunderflow context-budget CLI command with --project DIR, --global, and --format text|json flags; new GET /api/context-budget?project=<slug> HTTP route returning the same shape.
  • Wired into stackunderflow.reports.optimize via a sibling find_context_budget_findings(conn, *, threshold=20_000) that emits kind="context_budget_bloat" severity="medium" findings for the global overhead and any over-threshold project, with the top 5 contributing slices included.
  • Heuristic is documented prominently (CLI text, JSON heuristic field, CHANGELOG, docs/cli-reference.md): len(text) // 4 per file; 200 + 50/tool per MCP server (or a flat 200 fallback when tool counts aren't statically known). Useful for spotting bloat, not billing.
  • Defensive throughout: any missing file / malformed JSON / unreadable directory contributes a zero-token slice instead of raising.

Sample output (this repo)

$ stackunderflow context-budget
Context budget (per-session estimate)
  heuristic: len(text) // 4; per-MCP-server 200 + 50/tool

  system_prompt                   3,000 tok   (fixed)
  memory:project_CLAUDE.md            0 tok   .../StackUnderflow/CLAUDE.md
  memory:global_CLAUDE.md           401 tok   /Users/yadkonrad/.claude/CLAUDE.md
  mcp:apple-calendar                400 tok   /Users/yadkonrad/.claude.json
  mcp:drawio-mcp                    400 tok   /Users/yadkonrad/.claude.json
  mcp:macos-automator               400 tok   /Users/yadkonrad/.claude.json
  mcp:macos-control                 400 tok   /Users/yadkonrad/.claude.json
  mcp:macos-ui-automation           400 tok   /Users/yadkonrad/.claude.json
  mcp:tavily                        400 tok   /Users/yadkonrad/.claude.json
  skill:anti-slop-guide           5,856 tok   .../skills/anti-slop-guide/SKILL.md
  skill:capture-hinge-profile     1,751 tok   .../skills/capture-hinge-profile/SKILL.md
  skill:draft-message             2,038 tok   .../skills/draft-message/SKILL.md
  skill:process-debrief           2,733 tok   .../skills/process-debrief/SKILL.md
  skill:time-and-context-check      292 tok   .../skills/time-and-context-check/SKILL.md
  skill:yad-finances                928 tok   .../skills/yad-finances/SKILL.md

  total: 19,399 tokens
  cost per session: $0.0582
  estimated monthly cost: $5.82

Test plan

  • pytest tests/ -q — 910 passed, 2 skipped (was 882; +28 new tests)
  • ruff check clean on all touched files
  • CLI smoke against actual ~/.claude/ config (output above)
  • HTTP route returns 200 for known slug, 404 for unknown slug, falls back to global shape when project path is missing on disk
  • find_context_budget_findings emits a finding when budget > threshold and stays silent when under

🤖 Generated with Claude Code

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>
@0bserver07 0bserver07 force-pushed the feat/context-budget branch from 099ee87 to 70e11e1 Compare May 1, 2026 04:17
@0bserver07 0bserver07 merged commit 65763ed into main May 1, 2026
7 checks passed
@0bserver07 0bserver07 deleted the feat/context-budget branch May 1, 2026 04:17
@0bserver07 0bserver07 mentioned this pull request May 1, 2026
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant