Files
khosra/docs/context-economy.md
T
Claude Opus 5andbdeshi 7796b0d919 make context a budget the harness enforces
Adopts ideas/token-conservation.md, parked 2026-07-28, plus the disciplines
the human added: read the compressed form first, discover by mechanism,
shrink output at the source, never pay twice for the same bytes.

docs/context-economy.md owns all of it and leads with a floor, because
every cheap failure mode is also a token saving — skipping the owning doc,
guessing a signature, reporting from a diff, thinning a test — and each has
already cost this repo a defect. Frugality is for presentation and
discovery, never for the artifact or the evidence.

Mechanical, not remembered:

- scripts/surface.sh generates docs/surface.md — every top-level
  declaration with its line, 261 lines standing for 3757 of source. The
  pre-commit hook regenerates and stages it, so it cannot be stale, and
  verify.sh compares independently for a clone that never set
  core.hooksPath. The hook refuses a commit with unstaged .go changes,
  since what it generated describes the working tree, not the commit.
- verify.sh --quiet: 48 lines of gate output become 1. The hook uses it.
- CLAUDE_LOC_MAX=150, the only budget billed per turn rather than per read.

Both new gates were watched failing before being kept: a doctored
surface.md, and CLAUDE_LOC_MAX temporarily set to 5.

state.md's inventory loses its LOC column. It had already drifted on six
files (content.go 381→450, render.go 447→454, web.go 206→217, check
216→223, watch 129→137, chrome 105→110) which is what a number written in
two places does; the generated file owns sizes now, the table owns purpose.
The subagent question is recorded there as the one open decision, with the
case for and against written out in the idea file.
2026-08-01 02:16:11 +06:00

5.0 KiB

Context economy

How the agent spends context. The limit on this project is not typing speed, it is how much work fits in a session before the window fills — so bytes that buy nothing are features not built.

This doc has a floor and the floor wins. Every technique below is forbidden from buying: skipping the doc that owns a rule (CLAUDE.md §1), guessing an API instead of reading it, reporting success from a diff instead of a run (SKILL.md §4), keeping a test that was never seen to fail, or thinning generated code, content, tests, comments or commit messages. Those are all cheaper in tokens and every one of them has cost this project a defect. Frugality applies to presentation and discovery, never to the artifact or the evidence. When a saving and the floor disagree, pay.

1. Fewer turns beats fewer bytes

The whole context is re-sent on every tool call, so a six-call sequence costs roughly six times the window. Batch independent calls into one message. Where a sequence is known in advance or will be run again, write the script instead: one artifact that sets up, runs, probes and prints its conclusion — the live-reload check that measured the watcher is the shape to copy, and verify.sh is the same technique applied to the gates.

2. Read the compressed form first

In order of cost, stop at the first that answers the question:

Question Cheapest answer
where does X live, what is in this package docs/surface.md (generated; make surface)
what does this package offer callers go doc ./internal/<pkg>
what does this behave like its _test.go — a table states in 20 lines what 200 implement
why is it like this decisions.md, state.md — never the code
what changed and when git log --oneline -- <path>
how big is this before I open it wc -l, or the manifest line in surface.md

Then read the range, not the file: grep -n to locate, Read with offset/limit. A whole-file read is for the doc that owns a rule you are about to assert, and for a file you are about to rewrite.

3. Let the machine find it

Discovery by mechanism beats discovery by reading.

  • Break it and read the errors. Changing a signature: go build ./... returns the exact caller list, complete, in a form a grep can miss.
  • scripts/verify.sh --list answers "does this gate exist" without running or reading anything.
  • grep -c for "does this exist", -l for "which files", -n for "where". Context flags only when the surrounding lines are the answer.
  • go test -run TestName over a full suite while iterating; the full suite once, at the end.

4. Shrink output at the source

Filtering inside the call is free. Filtering after it lands is impossible.

  • ./scripts/verify.sh --quiet — two lines on green instead of forty-three. The pre-commit hook uses it. Use the loud form when a gate fails or you are auditing the gates themselves.
  • Quiet and fail-fast flags by default: -q, --porcelain, --short, --oneline, -failfast, --stat before -p, and 2>/dev/null for known noise.
  • Pipe through tail/grep -v/awk in the same call. Ten relevant lines, not four hundred.
  • Long or noisy runs write to the scratchpad and get queried; never cat a lock file, a binary, or generated output.
  • Sample rather than enumerate: two bundles out of thirty answer whether the shape holds.

5. Never pay for the same bytes twice

  • No re-reading after Edit or Write — they fail loudly if they did not apply.
  • A background command's output is read once, after it completes. Not while polling, and not quoted back afterwards.
  • No restating the plan, then doing it, then summarising it. State once, report deltas.
  • Write a fact down where it belongs the first time it is established (state.md, an ADR, a resume note in the queue file) so the next session reads a line instead of re-deriving it. A handoff note is cheaper than a compaction summary and loses less.
  • Report file:line, never the code — the human has the file, and the reference is clickable.

6. Prose is the cheapest thing to cut and the easiest to overcut

Target density, not brevity: the same findings, numbers and caveats in fewer words. Cut preamble, recap, restatement, option surveys, and hedging. Do not cut a finding, a measurement, a stated assumption, or a caveat — a report that omits the caveat is not shorter, it is wrong.

What is enforced, and what is not

Three of these are mechanical. scripts/hooks/pre-commit regenerates docs/surface.md and stages it, so no commit can carry a stale one, and verify.sh compares independently for the clone that never set core.hooksPath. CLAUDE_LOC_MAX bounds the one file billed on every turn. --quiet exists to be used, and the hook uses it.

Everything else here is discipline — no script can see a redundant read, an unnecessary whole-file Read, or a report that padded instead of informing. That asymmetry is the reason this doc is short enough to be re-read, and the reason its floor is stated first.