agentleFS
LeostrukaLeostruka/devin-bundle/skills/context-hygiene/SKILL.md

context-hygiene

Skill0 starsChanged 4 months agoMITView on GitHub

Use when context is growing large or the agent forgets earlier instructions, when deciding clear vs compact, when processing documents/logs >50k tokens, when monitoring token cost or limiting subagent fan-out, when choosing a reasoning effort level, or when reducing token/compute/MCP costs. Covers lost-in-the-middle, context folding (RLM), cost guards, and difficulty-matched effort.

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

6 rules. About 0 are common, 4 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    If dense access to early detail is needed, fold (below) instead.

    line 27
  2. Very specific:

    Keep rules files small — they load into *every* conversation.

    line 32
  3. Very specific:

    Don't paste huge documents — write to file, then read offset/limit or grep.

    line 37
  4. Distinctive:

    Prefer subagents for parallel exploration — each has its own window.

    line 39
  5. Distinctive:

    Don't fold preemptively, don't dump whole files into context, don't use for simple retrieval (grep is cheaper).

    line 58
  6. Distinctive:

    Add caching; shorten prompts.

    line 74

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Context Hygiene

Context window = input + output tokens the model sees at once. Every system prompt, rules file, MCP tool definition, message, and tool result counts against a hard provider-set limit. Output quality degrades past ~100k active tokens ("dumb zone") regardless of total window — lost-in-the-middle means middle detail has weak impact (arXiv:2307.03172). Bigger window ≠ better retrieval.

Default stance: with SWE-2 (262K) most sessions fit — use context directly. Apply the techniques below only when actually needed.

Clear vs Compact

ActionWhat it doesWhen
clearWipes historyDefault. Task done or unrelated to prior chat.
compactSummarizes historySame task continues; losing thread would hurt. Lossy — preserves intent, not facts.

Compaction leaves "sediment" — treat it as escape hatch, not hygiene default. If dense access to early detail is needed, fold (below) instead.

Lean Context Rules

  1. Clear chats between unrelated tasks.
  2. Keep rules files small — they load into every conversation. Modularize into skills; see writing-skills.
  3. Be paranoid about MCP servers — each injects every tool definition into the system prompt. Audit with mcp-governance before adding; keep <10-15 tools/server.
  4. Don't paste huge documents — write to file, then read offset/limit or grep.
  5. Prefer subagents for parallel exploration — each has its own window; only synthesis returns (50-100x savings).
  6. Watch the budget — context-budget.py (SessionStart hook).
  7. Guard against overflow — unbounded loops are a cost and correctness risk.

Context Folding (RLM-style, for >50k-token artifacts)

Adapted from Recursive Language Models (arXiv:2512.24601): treat context as a variable in an environment — peek, grep, partition, sub-query — instead of reading it all.

Workflow: offload to file → peek (read offset/limit) → grep to locate → partition (split -l 500) → sub-query chunks with researcher subagents → verify returns (Rule 12 spot-checks) → synthesize.

Depth rule (critical): depth=1 only. Subagents must NOT spawn subagents — depth=2 causes overthinking, 95x slower (arXiv:2603.02615).

Don't fold preemptively, don't dump whole files into context, don't use for simple retrieval (grep is cheaper).

Cost Guard

LimitAction
1-3 subagentsDefault max; >3 requires explicit user approval (enforced by validate-tool-args.py).
Loops/retriesBound every loop; each iteration multiplies cost.
Token maxingDetect effort disproportionate to task difficulty.

Cost Optimization Protocol

  1. Measure current cost (tokens/request, model tier, cache hit rate).
  2. Audit context usage (mcp-governance for MCP tool-definition cost).
  3. Right-size effort (see below); paid models only when explicitly approved.
  4. Add caching; shorten prompts; prefer file snippets over full reads.
  5. Re-measure; report tokens/cost before and after with commands used.

Code-formatting minification — scoped caution

Stripping non-semantic whitespace cuts input tokens ~24.5% (arXiv:2508.13666, ICSE'26 — Java 33.7% avg; tool: Deblank). Verified but narrow: measured on FIM completion only. Do NOT minify code in agentic/editing flows —

  • SWE-bench agents: input −42% but resolution 50%→38% (arXiv:2606.01326).
  • Whitespace-preserving rewrites flip predictions up to 60% (TokDrift, arXiv:2510.14972).
  • Python: indentation is syntax → ~6.5% gain; minified files break edit-matching, linters, and review.
  • Output stays formatted regardless (−2.9%); "emit unformatted" prompting broke Gemini 67%→11% and is model-fragile.

Valid as a per-project rule only when ALL hold: model reads but never edits the code (analysis/completion/RAG input); brace-delimited language (not Python/YAML); a formatter restores output (Deblank pattern — prettier/CI round-trip, disk unchanged); payload is one-shot, not an editing session.

Effort Calibration

Effort is scarce — match to task difficulty. Maps to data/bundle-models.json:

Levelmodel_uidWhen
Mediumswe-2-mediumSimple tasks, spot fixes, isolated scripts, mechanical refactors. Single test/build verifies.
Highswe-2-highMulti-file tasks with clear dependencies. Default.
Maxswe-2-maxOpen-ended, long-horizon, architecture, novel debugging, or failure at lower effort.

Rules:

  1. Improve the spec before raising effort — information quality substitutes for reasoning budget (arXiv:2608.01347).
  2. Start minimal, expand on failure (E3: Estimate, Execute, Expand — arXiv:2607.13034; 85% cost cut at equal success on simple edits).
  3. Never start at Max for a task untried at Medium. But don't starve hard tasks — higher effort lifted first-try success 28%→89% on a hard build (arXiv:2607.02436).
  4. Prompt wording allocates work — "multiple approaches" multiplies reasoning 2.4-7.4× with no success gain; "max certainty" creates verification loops 18× median cost. State WHAT + acceptance criteria.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.