---
name: context-lean
description: Prune context before each LLM call. Use in any multi-step workflow that accumulates context across iterations. Less but more relevant context produces better outputs. Prevents context bloat — the single biggest silent quality degrader in long autonomous runs.
user-invocable: false
model-invocable: true
side-effects: none
permission-profile: normal
triggers:
  - "*"
---

# Context Lean

## Iron Law

```
CONTEXT IS A BUDGET, NOT A DUMP.
EVERY TOKEN IN CONTEXT MUST EARN ITS PLACE.
```

Adding more context is not safer than adding less. Irrelevant context degrades output quality by diluting signal. When in doubt, leave it out.

## Recognize Your Own Rationalizations

- "More context can't hurt — it gives the model more to work with." → Wrong. Noise degrades recall. The model attends to everything; irrelevant context steals attention from relevant context.
- "I'll include the whole file to be safe." → Include only the functions you're actually modifying. The rest is noise.
- "I need to include the history so the model understands the situation." → Include the summary, not the transcript. Summaries are signal; raw transcripts are noise.
- "The token limit isn't hit yet, so it's fine." → Token limits are not quality thresholds. Quality degrades well before the limit.

## When to Run

Before any LLM call in a multi-step workflow. Especially:
- Before each autonomous iteration
- Before a planning call that synthesizes many inputs
- After completing a phase (prune phase artifacts before the next phase)
- When the context window is more than 50% full

## Skill Chain

Inline skill. Run as a pre-call gate before each significant LLM invocation.

```
← prev:  any skill, before its LLM call
→ next:  return to the invoking skill with pruned context
```

## Pruning Protocol

Apply in order. Stop when the context is lean.

### Step 1 — Remove completed work

Anything that was needed to get to the current state but is not needed to proceed:
- Completed task details (keep the summary, drop the steps)
- Resolved errors (keep the fix, drop the stack trace)
- Superseded plans (keep the current plan, drop the draft)

### Step 2 — Summarize transcripts

Raw conversation history is always worse than a summary. For any context block older than the current phase:
1. Write a 3-5 sentence summary: what was decided, what was built, what failed
2. Replace the transcript block with the summary
3. Keep only the last 2-3 turns verbatim (for continuity)

### Step 3 — Scope file content

Never include entire files when you only need parts of them:
- Include only the functions/methods being modified
- Include only the test cases for the current behaviour
- Include only the error output relevant to the current failure

If a file must be included whole (e.g., a small config), it must be ≤ 50 lines or explicitly justified.

### Step 4 — Audit includes

For every block of context, ask: **if this were removed, would the model's output be worse?** If the answer is "maybe not," remove it.

Keep:
- The current task/goal (always)
- The specific code being modified (always)
- The error message or test failure driving the current step (always)
- The contract/spec for the current slice (always)
- Recent decisions that constrain the current step

Remove:
- Earlier phases' full output (summarize)
- Files not touched in the current step
- Passing test output (keep only failures)
- Dependency documentation (link, don't include)
- Comment threads and discussion (summarize conclusions)

### Step 5 — Verify budget

After pruning:
- Context should fit in < 30% of the token budget for simple tasks, < 60% for complex ones
- If still over budget after pruning, the task is too large for one call — split it

## Context Composition Rules

| Source | Include | Format |
|--------|---------|--------|
| Current task | Always | Full |
| Current file being edited | Only changed functions | Snippet |
| Current error / test failure | Always | Full |
| Previous phase output | Summary only | 3-5 sentences |
| Related file (not being edited) | Only the contract/signature | Snippet |
| Conversation history | Last 2-3 turns + summary of rest | Mixed |
| Documentation | Never inline | Reference by path |

## Completion Criteria

Context is lean when:
- [ ] No completed phase artifacts in full (only summaries)
- [ ] No entire files included when snippets suffice
- [ ] Every included block answers "yes" to the audit question
- [ ] Token budget is within target
