strategy-decision-tree.md

reference

← Back to skill

Content hash: dcd2aad57efd0ad9bbd6eb8586cb3fa18f6090c622b0efb8569c5d77522b5896
# Context Management Strategy Decision Tree

## Pick the least lossy strategy that fits

```
Question: Is the overflowing content knowledge or conversation?

  KNOWLEDGE (large, queryable) ──→ RAG / retrieval (per-turn)
                                    (correct answer; doesn't replace conversational memory)

  CONVERSATION (grows unbounded)
      │
      ├─ Short sessions (< window) ──→ Truncation (keep last N tokens)
      │                                Zero LLM cost, simplest
      │
      ├─ Long multi-step agents ──→ Sliding-window eviction
      │                             (drop oldest chunks at threshold, repair tool-call pairs)
      │
      ├─ Must retain context across turns ──→ Summarization / compaction
      │                                        (LLM summary of old + raw recent tail)
      │
      └─ Multi-session, must remember user ──→ Two-layer agent memory
                                               (short-term context + long-term vector store)
```

## Trigger thresholds

| Strategy | Trigger | Why |
|----------|---------|-----|
| Truncation | Always | Simplest |
| Sliding window | 50% of window | Leave headroom for the next few turns |
| Compaction | 25–70% of window | Earlier trigger = smaller, more frequent compactions |
| RAG | N/A (per-turn) | Retrieve only what's needed |

## Compaction summary must preserve

```
✓ Session intent / goal
✓ Artifacts created (files, DB changes, etc.)
✓ Key decisions + rationale
✓ Open questions / next steps
✗ Transient observations (tool output noise)
✗ Verbatim dialogue (keep a recent tail of raw messages)
```

## Repairing tool-call pairs after eviction

When evicting, never split a tool call from its result:
```python
# Before eviction: [..., {"role": "assistant", "tool_calls": [...]}, {"role": "tool", ...}]
# After: keep tool_calls + tool results as an atomic unit
# If you must drop, drop the whole pair, never just one side
```

## Durable record requirement

- The summary is **lossy** — always persist the original transcript to disk/DB
- The canonical record is the disk copy, not the in-context summary
- Reconstruct on demand if the agent needs detail a summary dropped