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

context-hygiene

Skill0 starsChanged 3 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.

5 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. 0 are common, 4 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 5 edited versions we read keep these.

  • 4

    Distinctive

    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 62
  3. Very specific:

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

    line 67
  4. Distinctive:

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

    line 69
  5. Distinctive:

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

    line 88
  6. Distinctive:

    Add caching; shorten prompts.

    line 119

“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “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.

Prune vs Fold vs Compact vs Summarize

ActionMechanismWhen
PrunePer-item keep/truncate/drop via extensions/laya-compactor (laya compact-item-v1, suggestion/abstain only)Tool noise fills the window; facts must survive verbatim.
FoldOffload artifacts to files; peek/grep/sub-queryDense access to early detail needed; >50k-token artifacts.
Compactcompact summaryEscape hatch when pruning cannot reach target size.
SummarizeManual tl;drAlmost never inside a session; for handoffs write a doc instead.

Pruning decision policy lives here: pin system + first message + last user task + newest preserve_recent messages; abstain means keep (fail-safe); drop applies pair-wise so no tool_result is orphaned; survivors stay byte-identical. Escalate to compact summary only when pruning cannot reach compact_to.

Smart Window params (adaptive effective window, not fixed recency):

ParamValueBasis
compact_at0.70 x window (SWE-2 262k -> ~183k)headroom for next tool batch
compact_to0.40 x window (~105k)hysteresis, avoids thrash
preserve_recent6 messages + first messageupstream port default
keep_threshold0.5, calibrate on real transcriptslaya confidence is entropy-based
truncate_head300 charsupstream port default
max_state~25k est. tokensfits laya context
reduction_floor<0.25 -> abort, keep originalavoids pointless churn

assist mode requires activation_errors empty plus a calibrated threshold; run shadow first, measure, then promote.

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).

Reversible folds (scripts/fold_store.py, inspired by billion-context): when you fold a range the session may need again, persist it with an id — the summary stays in context, the content survives on disk.

python skills/context-hygiene/scripts/fold_store.py save <id> \
    --summary "one line" --content-file huge_log.txt   # fold away
fold_store.py unfold <id>      # restore full text when needed
fold_store.py grep <keyword>   # find which fold holds a detail
fold_store.py list             # ids, summaries, est. tokens

Fold ids are durable across sessions (.devin/scratch/folds/); delete stale folds at task end to keep the store small.

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.