Skip to content

context-health

Recognizes and prevents context window degradation — poisoning, distraction, confusion, and clash failure modes. Use when context seems stale, when agent behavior degrades mid-session, or when building context-aware systems.

ModelSource
sonnetpack: context-engineering
Full Reference

┏━ 🧠 context-health ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ your friendly armadillo is here to serve you ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

Diagnoses and prevents context window degradation. Context is a finite, depletable resource — once poisoned or exhausted, quality collapses silently.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

See reference/failure-modes.md for full examples and symptoms.

ModeRoot CausePrimary Signal
PoisoningBad output fed back as ground truthHallucinations compound across turns
DistractionHigh-salience irrelevant contentAgent fixates on wrong files/concepts
ConfusionSemantically similar but unrelated infoSubtle cross-domain bleed
ClashAccumulated contradictionsFlip-flopping, inconsistent decisions

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Run this checklist when behavior feels off mid-session:

Poisoning signals:

  • Agent cites earlier output as external fact
  • Confidence increases while accuracy decreases
  • Error messages from previous steps appear as valid data

Distraction signals:

  • Agent keeps referencing a file that isn’t relevant to current task
  • Large pasted block (log, schema, diff) dominates all subsequent reasoning
  • Responses reference context from 10+ turns ago unprompted

Confusion signals:

  • Domain terminology bleeds across subsystems (e.g., “user” meaning two different things)
  • Agent applies patterns from one layer to another (e.g., DB schema rules to API design)
  • Similar variable names cause wrong-file edits

Clash signals:

  • Contradictory decisions made within the same session
  • Agent re-debates settled questions
  • Implementation diverges from earlier agreed architecture

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

See reference/prevention-patterns.md for implementation detail.

Load context incrementally — only what the current task requires. Never dump entire files unless they are directly acted upon.

Wrong: Read all 12 files → then ask question
Right: Ask question → read only files needed to answer it

Assign explicit token budgets per context zone:

ZoneBudgetPolicy
System prompt~2k tokensFixed — skills, rules, identity
Working memory~4k tokensRotating — current task only
Evidence~8k tokensScoped — file excerpts, not full files
HistoryRemainderCompressed via artifacts

When approaching limits, offload to artifacts before the window fills. Waiting until 95% means the offload itself gets truncated.

Before context fills:

  1. Write decisions to .claude/progress/ or a task-specific artifact
  2. Summarize completed work into a compact handoff note
  3. Start fresh context with the handoff note as input — not the full history

Tool outputs that are high-volume and low-signal (test run logs, full diffs, raw API responses) should be summarized before being returned to the main context.

Wrong: Return full 800-line test output to context
Right: Extract: pass/fail counts + failing test names + first error per failure

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

When degradation is detected, take the appropriate recovery action:

Detected ModeImmediate ActionThen
PoisoningStop. State what was hallucinated and which turn introduced itRe-run from last verified checkpoint
DistractionExplicitly name the distractor and exclude it from scopeRestate the actual task goal
ConfusionDefine the conflicting terms with explicit namespacingAudit recent edits for cross-contamination
ClashSurface all contradictory decisions explicitlyRe-decide from first principles, write to artifact

Nuclear option: /compact the session, then resume with a clean handoff artifact. Don’t try to correct a heavily degraded context in-place — the corrections themselves become noise.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Integration with verification-before-completion

Section titled “Integration with verification-before-completion”

Before claiming any work is complete, run the context probe gate:

  1. Poisoning probe — Can you trace every factual claim to an external source, not a previous model output?
  2. Distraction probe — Does the implementation address the original task, not a distractor?
  3. Confusion probe — Are all domain terms used consistently across modified files?
  4. Clash probe — Do all decisions in this session form a coherent, non-contradictory whole?

If any probe fails, invoke systematic-debugging before committing. A context health failure is a category of bug — treat it as one.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

TriggerAction
Agent gives confidently wrong answerRun poisoning probe immediately
Session > 40 turnsRun full checklist, consider compaction
Agent ignores explicit instructionDistraction check — identify competing context
Two subsystems get cross-wiredConfusion recovery
Previous decision gets contradictedClash recovery + artifact write
Building a multi-agent systemApply prevention patterns from the start
FileContents
reference/failure-modes.mdDetailed mode descriptions, examples, symptoms
reference/compression-awareness.mdWhat survives compaction, artifact trail strategies
reference/prevention-patterns.mdProgressive disclosure, token budgeting, observation masking