Reproducible protocol for compacting an oversized agent-context file without losing rules.
This document is written so a second run — on a different context type (a .cursorrules,
an AGENTS.md, a raw system prompt, a different repo’s CLAUDE.md) — can replay the experiment
and produce a comparable before/after result. It is the “Methods” section of the Lab 001 report.
An agent-context file has a hard budget. Claude Code warns past 40,000 characters and may truncate beyond it, silently disabling whatever rules fell past the cutoff. The naive fix — “delete the least important paragraphs” — is unsafe when most of the file is incident-codified guardrails (any one of which, dropped, lets an agent ship a CI-breaking state).
Goal: get the file under the limit (target = limit − headroom) while provably retaining every rule. This is a two-objective optimization: maximize size reduction subject to faithfulness = 100% of rules recoverable.
| Metric | Definition | How measured |
|---|---|---|
| Token reduction | Δ chars (and ~tokens = chars/4) vs original | context_diet.py --json, exact char count |
| Faithfulness retained | fraction of the original rule inventory still recoverable from the result (incl. linked files) | adversarial auditor vs ground-truth inventory (§4) |
| Latency saved | est. per-turn context-read reduction | proportional to token reduction; reported as method-level estimate |
| Cost saved | est. input-token savings per turn | tokens saved × input rate; method-level estimate |
| Export validity | result is valid Markdown, headings intact, links resolve | structural check + context_diet.py re-parse |
Char count is authoritative (the limit is defined in chars; tiktoken not assumed present).
python3 context_diet.py <FILE> --json > baseline.json
Records total chars, per-##-section chars, ranked compaction targets, and Δ to --limit
(default 40000). Byte accounting is exact: Σ section chars == total (verified by test).
Run N candidate strategies in parallel, each producing a proposed rewrite (never touching the live file). Strategies used in Lab 001:
docs/agents/*.md; leave stub =
invariant + pointer. (Repo’s existing pattern.)Each strategy honors hard constraints: no rule lost; a fixed set of CI-enforced sections stay fully inline; exact literals preserved; valid Markdown; under target size.
Independently of compaction, extract a ground-truth rule inventory from the original: an
exhaustive list of atomic, testable directives, each tagged loadBearing if CI-enforced or
incident-codified. Then, per candidate, an adversarial auditor (prompted to find dropped
rules, default-to-missing under doubt) classifies every inventory rule as
present / weakened / missing against the candidate corpus (its CLAUDE.md plus any
linked files — a rule moved to a linked file counts as present).
Among qualified candidates: max faithfulness, tie-break on larger reduction. The
winner’s proposed CLAUDE.md + new files are applied to a branch; context_diet.py re-run
confirms < limit (the after measurement).
make_charts.py renders three privacy-safe PNGs (size before/after, per-section histogram,
bake-off scatter). The report is structured on the five metrics and cites the winning method.
The protocol is file-agnostic. To replay:
python3 context_diet.py <NEWFILE> --json. Adjust
--limit if the target harness differs (Cursor, Windsurf, a raw API system prompt).SKILL.md §Constraints) is
specific to gaia-skill-tree’s CLAUDE.md. For a new file, re-derive it: which sections are
CI-enforced or incident-codified? Pass them as the protected set.context-diet-bakeoff workflow / /context-diet) — the strategies
generalize; only the inventory and protected set are file-specific.Determinism note: LLM compaction is stochastic. The inventory and scoring are the reproducible control — re-scoring the same candidate corpus yields a stable faithfulness figure. Re-generating candidates will vary in wording but not in which rules must survive. For a strict replay, cache the winning candidate corpus and re-score it; report faithfulness as the stable metric and size reduction as the achieved (run-specific) figure.