context-compact
Skill0 starsChanged 3 months agoMITView on GitHub
Measure your own context fill, compare it against budget thresholds, and adapt (read less, delegate to subagents, recommend /compact) so long sessions stay cheap and fast. Use automatically before starting a task on a long conversation, after reading large files, or whenever context feels heavy — triggers: "context", "compact", "/compact", "token", "token usage", "context size", "heavy/cluttered/long context", "keep it light" (in any language the user speaks). Implements the auto-compact concept (budget → fill ratio → mode → adapt), the same idea as lilith's adaptive context engine.
In this repository14 other files it gives its agents.
- chatgpt/AGENTS.mdAGENTS.md
- opencode/AGENTS.mdAGENTS.md
- zcode/AGENTS.mdAGENTS.md
- claude/CLAUDE.mdCLAUDE.md
- code-qualitySkill
- design-principlesSkill
- external-ai-consultSkill
- parallel-subagentsSkill
- reliable-executionSkill
- research-and-reuseSkill
- session-handoffSkill
- sudo-handoffSkill
- task-intakeSkill
- tauri-v2-docsSkill
The file
We split this skill into the rules it gives an agent
10 rules. About 0 are common, 7 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈7
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 41
Do not trigger for trivial single-file lookups on a fresh session.
- Very specific: line 63
Absolute cost cap: When estimated context exceeds 500K tokens, recommend /compact immediately — regardless of percentage bands.
- Distinctive: line 66
Sensitive-work exception: Do NOT recommend compaction during data-loss-risk, security, irreversible-operations, or state-heavy debugging …
- Distinctive: line 91
When you summarize/drop a file, never later quote its exact code as if it is still loaded — re-read only the needed lines.
- Distinctive: line 113
Wait: Do not start new heavy reads while the user runs it.
- Distinctive: line 119
Never deliver a handoff during sensitive work (see Workflow step 7).
- Distinctive: line 120
If the user declines, keep working in constrained mode — don't nag repeatedly.
- Very specific: line 144
Before a task on a long session, state the band in one line, e.g.
- Distinctive: line 146
After compacting, the session should feel lighter: the next user turn should show a smaller working set (fewer large reads).
- Very specific: line 150
If a session crossed 500K tokens and no /compact was recommended (and the work wasn't sensitive), the cost-cap rule was skipped.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 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.
Files
Context Compact
Purpose
Prevent context bloat and cut token consumption by measuring how full the
context window is, comparing it to thresholds, and adapting reading and
delegation behavior before it becomes a problem. When fill gets high,
compact what you can and tell the user to run /compact (or /clear when
the task is done).
When to use
Trigger automatically when any of these holds:
- The conversation is long (10+ turns) or the user mentions "context", "compact", "/compact", "token"/"token usage", "context size", "heavy/cluttered/long context", or asks to "keep it light" — in any language the user speaks.
- About to read a file you suspect is large (> 300 lines) or whose size you don't know yet.
- Right after reading a large file while the task continues.
- The user mentions token usage, cost, or session speed.
- The user just approved a plan and implementation is about to start (plan-gated compact — see Rules).
Do not trigger for trivial single-file lookups on a fresh session.
Preconditions
- None hard. The estimation method below is a heuristic — no tokenizer needed.
Workflow
- Estimate the budget. The active model supports 600K/1M-token windows — use the known window (600K default, 1M when set). Reserve ~20% for system prompt + tool output.
- Estimate current fill. Sum of: system instructions (AGENTS.md files), files fully read this session (byte size ÷ 4 ≈ tokens, or line count × ~11), tool outputs still in context, and the conversation itself. Rough is fine — you only need the right band.
- Compare → mode using the table in Rules.
- Adapt per mode. Change how you read and delegate before doing anything else.
- Compact when heavy (fill > 70% — or the absolute cap in step 6).
Re-express what you still need as short notes; stop holding full file
contents in context; explicitly recommend
/compactto the user (and/clearwhen the task is done). - Absolute cost cap. When estimated context exceeds 500K tokens,
recommend
/compactimmediately — regardless of percentage bands. The goal is cutting token cost, so trigger early, not at the window edge. - Sensitive-work exception. Do NOT recommend compaction during data-loss-risk, security, irreversible-operations, or state-heavy debugging work — losing detail can change the outcome. Keep full context and state the tradeoff explicitly.
- Preserve thread and plan. Immediately before recommending
/compact//clear, write a 2–4 line handoff note (current task • plan • decisions • next steps) so the summary retains the thread. - Report. One line to the user about the mode and what you changed, so
the recommendation is visible: e.g.
mode: balanced (~55% fill).
Rules
| Fill | Mode | Behavior |
| < 40% | expansive | Full reads fine; normal workflow |
| 40–50% | balanced | Prefer offset/limit reads and grep; delegate broad searches to subagents |
| > 50% | lean | Recommend /compact at a natural stopping point; reduce full-file holds |
| > 70% | constrained | grep-only for new files; subagents for everything; recommend /compact; no new large reads without telling the user |
| > 500K tokens (absolute) | cost-cap | Recommend /compact immediately, bands ignored; write the handoff note first; never during sensitive work |
- ≈ 4 characters ≈ 1 token for mixed code + text (code is denser; count bytes ÷ 3 as a safe upper bound).
- A full
Readpermanently adds the file to context — preferReadwith offset/limit, orgrep, when a section suffices. - Multi-file searches always go to a subagent (
Agent), never inline. - When you summarize/drop a file, never later quote its exact code as if it is still loaded — re-read only the needed lines.
- At > 70% fill, never open a large file silently: state the cost first.
- Plan-gated compact: once the user approves a plan, recommend
/compactbefore implementation starts. The exploration context collapses into the plan summary; the handoff note is the plan itself, so only the noise is lost. Still respects the sensitive-work exception.
Compact handoff (delivering /compact)
The agent never runs /compact itself — the user does. Deliver it exactly
like a privileged command (the sudo-handoff pattern):
- State the trigger. One line:
context > 500K tokens — cost-cap(orfill > 70% — heavy). - Write the handoff note (2–4 lines: current task • plan • decisions •
next steps) as the LAST content of your message — that's what the
/compactsummary preserves. - Give the copy-pasteable command in a fenced block:
Preceded by one sentence of what it does and why it's needed./compact - Wait. Do not start new heavy reads while the user runs it.
- Resume from the note once the user confirms. Never assume it ran.
Rules:
- The note must be the last thing you write — the summary keeps the tail of the conversation.
- Never deliver a handoff during sensitive work (see Workflow step 7).
- If the user declines, keep working in constrained mode — don't nag repeatedly.
Common mistakes
- Counting only the last message — context is cumulative since the session started.
- Reading a whole file when grep + offset/limit would do (the biggest waste by far).
- Silently continuing after heavy reads instead of recommending
/compact— the user can't see your token usage. - Summarizing a file but then still referencing its exact contents (it's gone — you must re-read).
- Treating this skill as optional because "the AGENTS.md already has context rules" — this is the procedure for those rules; the rules are the thresholds, this is the loop.
- Recommending
/compactduring sensitive work (data-loss risk, security, irreversible ops, state-heavy debugging) just to save tokens. - Crossing 500K tokens without telling the user that every further read is expensive — a missed compaction makes each re-read costlier than the compaction itself.
Validation
- Before a task on a long session, state the band in one line, e.g.
mode: balanced (~55% fill)— a human can sanity-check the estimate. - After compacting, the session should feel lighter: the next user turn should show a smaller working set (fewer large reads).
- The user confirms the
/compactrecommendation was made when fill crossed 70% — if a heavy session never got a recommendation, this skill was skipped. - If a session crossed 500K tokens and no
/compactwas recommended (and the work wasn't sensitive), the cost-cap rule was skipped. - A handoff note exists right before every
/compact//clearrecommendation, so the thread and plan survived the summary. - The user confirms
/compactran, and you resume from the pre-written note rather than from memory.
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
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.

