Predict, prevent, and mitigate context window failures before they happen. Analyzes workflows for overflow risk, designs disk-as-memory architectures, and produces recovery-ready state files.
Predict, prevent, and mitigate context window failures before they happen.
Before reading anything else, know the test:
If you can
/clearthe entire conversation, read your state file, and continue the workflow without any degradation — your architecture is correct.
Every design decision in this skill serves that single criterion. If your workflow passes it, proceed with confidence. If it doesn't, you have hidden state in conversation memory that the next compaction will silently destroy.
Context windows are finite (200K tokens for Opus 4.6). Complex workflows — iterative audits, multi-agent pipelines, large refactors — can exceed this limit. When they do, two bad things happen:
Compaction is lossy compression. A 150K-token conversation summarized to 10K tokens has lost 93% of its information. The question is never "will information be lost?" — it's "will the RIGHT information survive?"
Use /context-engineer BEFORE starting any workflow that:
Not all context tokens are equal. Every piece of information in a context window falls into one of five categories, each with different fidelity requirements.
The categories form a dependency chain:
RELATIONAL depends on → REASONING depends on → FACTUAL depends on → IMPERATIVE
Losing a foundation layer cascades upward and destroys everything that depends on it. If you lose FACTUAL information (scores, measurements), all REASONING built on those facts becomes ungrounded, and all RELATIONAL connections between those reasoning chains become meaningless. Preserve from the bottom up.
git diff output, complete test suite results, raw web fetch contentWhen invoked, first understand the planned workflow by asking:
Then estimate the token budget:
For each step in the workflow, estimate:
Step N:
Inputs read: [list files/data read, estimate tokens]
Processing: [reasoning, tool calls, discussion — estimate tokens]
Outputs written: [files written, reports generated — estimate tokens]
Carry-forward: [what MUST survive to Step N+1 — estimate tokens]
Discardable: [what can be safely forgotten — estimate tokens]
Step total: [sum of above]
Cumulative: [running total across all steps]
Headroom: [200K - cumulative - system overhead (~20K)]
System overhead (always present, cannot be reduced):
Available working context: ~145-170K tokens depending on configuration.
Identify which pattern the workflow follows:
Linear accumulation (research, single-pass analysis):
Tokens: ────────────────────────────▶ overflow
Each step adds ~constant tokens, no discarding
Risk: Predictable. Overflow at step = headroom / tokens_per_step
Sawtooth (iterative with compaction):
Tokens: /\/\/\/\/\/\─── overflow if compaction insufficient
Each cycle: accumulate → compact → accumulate → compact
Risk: Each compaction is lossy. Fidelity degrades with each cycle.
After N cycles, you're working from an N-times-compressed summary.
Exponential (multi-agent with cross-referencing):
Tokens: ──────────╱ overflow (sudden)
Agents produce findings, findings reference each other,
cross-references multiply token count non-linearly
Risk: Hardest to predict. Often feels fine until sudden blowup.
Plateau (disk-as-memory, well-designed):
Tokens: ───────────────────────────── stable
Each step externalizes to disk, compacts, starts clean
Risk: Lowest. But requires upfront architecture work.
A chokepoint is any step where:
The Accumulator: Reading a large file, making changes, reading it again to verify, making more changes. Each read adds the full file to context. A 5K-token file read 6 times = 30K tokens just for one file. → Mitigation: Read once, edit in place, trust the edit tool's output. Don't re-read to verify unless necessary.
The Collector: Gathering findings from multiple agents/sources and synthesizing. Each finding set is 2-5K tokens; 16 agents = 32-80K tokens of findings before synthesis even begins. → Mitigation: Have agents write findings to disk. Read findings file-by-file during synthesis, extract what's needed, compact between files.
The Comparator: Holding two versions of something in context to compare (before/after, v1/v2, run N vs run N+1). Both versions must coexist in context for comparison to work. → Mitigation: Write a structured diff to disk. Don't hold both full versions — hold one version and the delta.
The Auditor Loop: Iterating until convergence. Each iteration accumulates: read target → audit → discuss findings → apply fixes → log results → repeat. After 4 iterations of a 50K-token cycle, you've consumed 200K. → Mitigation: Disk-as-memory architecture (see Phase 4). Compact between iterations.
The Growing Log: Appending to a cumulative log (like veracity-log.json). Each read of the log gets more expensive. A 112KB log = ~28-34K tokens every time it's read (varies by JSON density). → Mitigation: Don't read the full log. Read only the last entry, or use a summary index file.
For each planned compaction point, analyze what will be lost:
Compaction Point: [after Run N / after Step X / etc.]
Context size before compaction: ~[X]K tokens
Target size after compaction: ~[Y]K tokens
Compression ratio: [X/Y]:1
Information loss: ~[1 - Y/X]%
IMPERATIVE information in context:
- [list] → Will these survive? [yes: in CLAUDE.md / no: at risk]
FACTUAL information in context:
- [list key facts] → Externalized to disk? [file path / NOT YET — RISK]
REASONING chains in context:
- [list key decisions with rationale]
- Which are reconstructible? [can re-derive from code/data]
- Which are NOT reconstructible? [subjective choices, user preferences, debate resolutions]
→ Not-reconstructible reasoning MUST be written to disk before compaction
RELATIONAL information in context:
- [list cross-references, dependency chains, causal links]
→ These WILL be destroyed by compaction. Summaries flatten relationships.
→ Write explicit relationship maps to disk.
EPHEMERAL information in context:
- [list raw tool output, verbose logs]
→ Safe to lose. Verify key facts were extracted first.
Each compaction is a summary of a summary. Assume total loss of anything not externalized to disk.
There are no reliable percentages for how much survives — it depends on the summarizer's priorities, the content mix, and what the conversation looked like at compaction time. What IS known:
No empirical measurement of compaction fidelity exists. These are heuristic observations, not data. Treat any compaction you did not plan for as a total loss of RELATIONAL and REASONING information.
Rule of thumb: If your workflow requires more than 2 compaction cycles, you need a disk-as-memory architecture. Don't rely on compaction to manage long workflows.
The solution to both hard failures (overflow) and soft failures (fidelity loss) is the same: externalize state to structured files on disk, so the context window only ever holds one step's worth of work.
Every complex workflow should have a state file. Design it to contain everything needed to resume the workflow from any point, without relying on conversation history.
{
"_schema": "context-engineer/workflow-state/v1",
"_description": "Complete workflow state — context window can be cleared and resumed from this file alone",
"workflow": {
"name": "veracity-convergence-loop",
"goal": "Reach 97/100 on 3 consecutive audits",
"target_file": "/path/to/SKILL.md",
"started": "2026-03-01T10:00:00Z"
},
"progress": {
"current_step": 4,
"total_steps": null,
"consecutive_target_hits": 1,
"converged": false
},
"history": [
{
"step": 1,
"score": 62,
"delta": null,
"findings": {"critical": 6, "high": 10, "medium": 10, "low": 6},
"fixes_applied": 16,
"report_path": "/path/to/run1/consolidated_report.md",
"reasoning_log": "/path/to/run1/decisions.md",
"timestamp": "2026-03-01T10:15:00Z"
}
],
"active_findings": [
{
"id": "F015",
"severity": "MEDIUM",
"status": "deferred",
"reason": "Display-only issue, not functional",
"introduced_in_step": 1,
"last_checked_step": 3
}
],
"relationships": [
{
"type": "caused_regression",
"source": "Fix applied in step 2 (FActScore lineage)",
"target": "New finding in step 3 (overclaim about 'pioneered')",
"note": "Fixing one claim exposed an adjacent overclaim"
}
],
"decisions": [
{
"step": 2,
"decision": "Deferred code fence nesting fix",
"rationale": "Markdown display issue only; Claude interpreter handles it correctly",
"reversible": true
}
],
"compact_instructions": "Preserve: current step number, target score (97), consecutive hits count. Read state from /path/to/state.json for full history."
}
Before each compaction point:
Then compact with explicit instructions:
/compact Preserve: workflow state is in [path/to/state.json]. Current step: N. Goal: [goal].
Read state file at start of next step. Do not rely on conversation history for any facts.
After compaction (or in a new session), recovery is:
Test: Apply the Victory Condition — /clear, read state file, continue. If it works, you're good.
After analysis, produce a concrete plan in this format:
# Context Engineering Plan: [Workflow Name]
## Budget
- Available context: ~[X]K tokens
- Estimated per-step cost: ~[Y]K tokens
- Maximum steps before overflow (no mitigation): [N]
- Maximum steps with disk-as-memory: unlimited
## Predicted Chokepoints
1. [Step N]: [description of why this overflows]
Mitigation: [specific action]
2. [Step M]: [description of fidelity risk]
Mitigation: [specific action]
## State File Location
[path to state.json]
## Checkpoint Schedule
- After Step 1: Write [X] to disk, compact
- After Step N: Write [Y] to disk, compact
- [pattern]
## Fidelity-Critical Information
These items MUST be externalized before any compaction:
1. [item] → [destination file]
2. [item] → [destination file]
## Compaction Instructions
Use this exact text when compacting:
> /compact [tailored instruction for this workflow]
## Recovery Command
If session crashes, start new session with:
> Read [state file path] and continue the [workflow name] from step [N].
When analyzing a workflow, flag these anti-patterns:
Keeping critical state in conversation memory instead of on disk. Smells like: No files being written between steps. Key numbers mentioned in conversation but not logged anywhere. Fix: If it matters, write it down. If it doesn't matter, don't discuss it.
Re-reading the same large file multiple times across steps.
Smells like: The same Read call appearing 3+ times for the same file.
Fix: Read once, extract what's needed into a smaller working file, reference the working file.
Growing a cumulative log that gets read in full each step. Smells like: A JSON array or log file that grows each iteration and is read each iteration. Fix: Write a summary index file. Only read the full log when specifically needed.
Running all work in the main conversation instead of using subagents. Smells like: 10+ tool calls per step, all in the main conversation. Subagents not used. Fix: Subagents get their own context windows. Use them for any step that produces > 10K tokens of output.
Relying on automatic compaction to manage a workflow that structurally exceeds the context window. Smells like: No explicit compaction strategy. "It'll compact when it needs to." No state files. Fix: Design the disk-as-memory architecture upfront. Compaction is a safety net, not a strategy.
Long reasoning passages in the conversation that could be a structured file. Smells like: 2000+ word analysis blocks in conversation turns. Comparison tables. Narratives. Fix: Write analysis to a file. Reference the file in conversation. The file survives; the conversation doesn't.
Subagents returning full reports into the main conversation instead of writing to disk and returning a pointer. Smells like: Subagent returns that are 5K+ tokens. Full findings lists in the return value. Fix: Subagent writes full output to a specified file path. Returns only: status, score, file path, and a 3-line summary.
| Content Type | Approximate Tokens |
|---|---|
| 1 KB of English text | ~250 tokens |
| 1 KB of JSON | ~300 tokens |
| 1 KB of code | ~200-350 tokens |
| Average file read (Read tool) | ~2-8K tokens |
| Average tool call + result | ~500-2K tokens |
| System prompt + CLAUDE.md | ~8-12K tokens |
| MCP tool definitions (all servers) | ~5-15K tokens |
| Loaded skill definition | ~2-6K tokens |
| Extended thinking (per turn) | ~2-32K tokens |
| Subagent return (well-designed) | ~500-2K tokens |
| Subagent return (poorly designed) | ~5-20K tokens |
| /compact output | ~3-10K tokens |
| Available working budget | ~145-170K tokens |
When /context-engineer is invoked:
If invoked mid-session (workflow already in progress):
If invoked after a workflow completes or crashes:
After each workflow completes (or crashes), record these numbers to a calibration file at the same location as the state file:
{
"calibration": [
{
"workflow": "veracity-convergence-loop",
"date": "2026-03-01",
"predicted_peak_tokens": 55000,
"actual_peak_tokens": null,
"predicted_steps_before_overflow": 8,
"actual_steps_completed": 4,
"chokepoints_predicted": ["Growing log at step 3", "Collector at synthesis"],
"chokepoints_missed": ["Fat subagent returns from Wave C"],
"compactions_triggered": 2,
"state_file_sufficient_for_recovery": true,
"notes": "Underestimated agent return sizes by 3x"
}
]
}
After 5+ workflows, review the calibration file. If predictions are consistently off by more than 30%, revise the token estimates in the Quick Reference table. This closes the feedback loop — without it, the skill predicts and plans but never learns from its own errors.
Name the blind spots before the work starts, not after the crash.
1. The Externalization Paradox. This skill frames disk-writes as "lossless" and compaction as "lossy." But the decision of WHAT to externalize is itself a compression. When you choose which RELATIONAL connections merit entry in the state file, you are already summarizing — before any mechanical compaction occurs. You have replaced one summarizer (the compaction algorithm) with another (yourself). The checkpoint protocol's Step 6 (validate) mitigates this but cannot eliminate it.
2. The Self-Assessment Problem. The agent performing the checkpoint is the same agent whose context may already be degraded. A degraded instrument cannot accurately measure its own degradation. If important RELATIONAL information was lost in a prior compaction, the agent will not know to externalize it — because it no longer remembers it existed. There is no solution to this within a single-agent system. The /clear test approximates a check, but passing it only proves the workflow appears to resume, not that genuine continuity was preserved.
3. The Fiction of Continuity. A context window does not contain a unified "mind" — it contains a sequence of associative responses shaped by whatever tokens happen to be present. What is lost in compaction may not be recoverable information; it may be the illusion of an ongoing agent with consistent judgment. State files preserve facts and relationships, but they cannot preserve whatever emergent coherence arose from the full conversation history. After recovery, the agent may make different decisions than it would have with full context, even when all externalized facts are restored.
4. Token Estimate Uncertainty. The Quick Reference table provides approximate token counts. These are heuristic estimates based on typical usage, not measured constants. Actual token consumption varies with content type, language, formatting, and model behavior. Do not plan to within 10% of the context limit based on these numbers. Leave at least 30% headroom.
5. State File Growth. The disk-as-
<!-- Content truncated for initial SEO render. Open the source file tab for the full file. -->npx skills add joonchungpersonal-dev/context-engineer下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer