Skip to content

chore(claude): cut Claude Code token spend and check the LSP servers at session start - #622

Merged
JuroOravec merged 1 commit into
mainfrom
chore/token-usage
Nov 13, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
chore/token-usage

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

What this does

Cuts what Claude Code spends on this repository, based on a measured pass over seven weeks of local transcripts (143,618 model requests, priced at Anthropic list prices; about $12,100). Each change below acts on one measured cost.

Change Measured cost it targets Estimated saving
Compact at 400,000 tokens (CLAUDE_CODE_AUTO_COMPACT_WINDOW in .claude/settings.json, applies to subagents too), and one fresh agent per pull request through the new saved implementer agent Agents with 150+ turns were 47% of spend and grew to ~967k tokens of context 13-21% (transcript replay; upper bound, leaves out work lost in compaction)
implementer keeps its prompt cache for 1 hour (experimental.cacheTtl) ~1,260 times a subagent waited over 5 minutes and rewrote its whole cache together with the row above, 21-25%
docs-cold-reader on Sonnet; prose passes and cold reads routed to Sonnet Sonnet was 2.6% of spend despite the routing rule 2-4%
rust-analyzer and pyright LSP plugins enabled, plus a rule to find code by symbol and read line ranges File reads were ~49% of tool output 2-5% (estimate)
CLAUDE.md 59 KB -> 50 KB: delivery-loop rules, the Mechanism 7 list, and the changelog rules moved to docs/agent/delivery-loop.md, docs/security/where-values-reach-the-browser.md, and docs/best-practices/writing-docs.md, with pointers that say when to read each CLAUDE.md is loaded into every request (~5% of spend) ~1%

The method and findings are recorded in docs/agent/RATIONALE.md ("Token cost of long-running agents"), and the setup in docs/codebase.md ("Setting up Claude Code").

The LSP servers now fail loudly

The rust-analyzer plugin was already installed on the maintainer's machine, but the language server it starts was missing, so the LSP tool never worked and nobody noticed. A new SessionStart hook, .claude/hooks/check-lsp-servers.py, checks both servers when a session starts. When one is missing, does not start, or (for pyright) differs from the package.json pin, it shows the user a warning with the fixing command and tells Claude that the LSP tool will fail for that language. It is silent when both work and takes about 0.1 s.

Contributors install the two servers once (docs/codebase.md has the commands). I kept rust-analyzer out of rust-toolchain.toml components on purpose: that would add a download to every Rust CI job for a tool only local agents use.

Other edits

  • The delegation intro now matches Claude Code's behavior. General-purpose and saved agents load CLAUDE.md at start, and Explore and Plan agents do not. So briefs tell only Explore, Plan and Workflow agents to read the whole file. Mechanism 6, docs-cold-reader and the fable-mode skill follow suit.
  • The "Keep the user informed" rule moved to "Working mode", because it applies to every task.
  • References to the moved sections are updated: the PR template, docs/codebase.md, and RATIONALE.md.

Review

  • Measurements: an independent agent reviewed the analysis and caught two counting bugs. Output was undercounted 2.5x, and requests copied into fork and resumed transcripts were counted twice. All figures above are corrected.
  • The change: an adversarial technical review and a docs cold read checked it. They found no lost rules and no broken links. Their findings on the trigger pointers, contradictions (the implementer vs Sonnet routing, re-reading CLAUDE.md), and prose are folded in.
  • The hook: a separate review checked it. Its fixes are folded in: Windows .cmd shims, no rustup toolchain download during session start, the pyright next to the language server, range pins, and running without CLAUDE_PROJECT_DIR.
  • Verified locally:
    • The hook stays silent when both servers work.
    • It warns correctly when they are missing, under the macOS Python 3.9.
    • It warns correctly when the pinned toolchain is missing, and on a pin mismatch.
    • A real claude -p session delivered the warning and the added context.
    • A fresh session found compile_template through the LSP tool.
    • scripts/validate.py passes.
  • Not yet observed: a subagent compacting at 400k. The docs say the variable applies to subagents. To confirm, look for compact_boundary with preTokens near 400,000 in a new subagent transcript.

Checklist

  • Tests added or updated for the change, and python scripts/check.py passes locally. No automated test for the hook: .claude/hooks/ has no test harness and runs only on contributors' machines, so I verified it by hand in the cases above. The change touches no product code. I ran scripts/validate.py, not the full gate.
  • CHANGELOG.md updated if the change is user-facing. Not user-facing.
  • Docs updated if behavior or the public API changed
  • For grammar / AST / compiler / binding changes, the cross-language contract stays in step. Not applicable.

🤖 Generated with Claude Code

…at session start

A measured pass over seven weeks of local Claude Code transcripts (143,618
requests) showed where the tokens go: long-lived agents with 150 or more
turns were 47% of spend, subagent caches expired while agents waited, and
file reads filled most of the context. This change acts on those findings:

- Compact at 400,000 tokens (CLAUDE_CODE_AUTO_COMPACT_WINDOW) in the main
  session and every subagent, and give each pull request or issue its own
  fresh agent through a new saved `implementer` agent.
- Keep the implementer's prompt cache for one hour, since it waits on builds
  and CI; other subagents keep five minutes.
- Run docs-cold-reader on Sonnet and route prose passes there.
- Enable the rust-analyzer and pyright LSP plugins, add a SessionStart hook
  that warns when either language server is missing, broken, or off the
  pyright pin, and tell agents to find code by symbol and read line ranges.
- Move the delivery-loop rules, the Mechanism 7 list of places where a
  value reaches the browser, and the changelog rules out of CLAUDE.md into
  docs/agent/delivery-loop.md, docs/security/where-values-reach-the-browser.md
  and docs/best-practices/writing-docs.md, with pointers that say when to
  read them (CLAUDE.md 59 KB -> 50 KB).
- Brief only Explore, Plan and Workflow agents to read CLAUDE.md; other
  agents already load it at start.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit c6bf31e into main Nov 13, 2026
14 of 15 checks passed
@JuroOravec
JuroOravec deleted the chore/token-usage branch November 13, 2026 11:21
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