# Compaction Strategies

> Implement hook-based history compaction in fast-agent (rolling window, context threshold truncation, tool-result clearing, summary prompt compaction). Use when adding or extending compaction hooks and wiring them into agent cards. Use when this capability is needed.

- Skill: `tomevault-io/compaction-strategies` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add tomevault-io/compaction-strategies`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tomevault-io/compaction-strategies/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- Author: tomevault-io (https://skillmd.com/u/tomevault-io)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/tomevault-io/compaction-strategies

---


# Compaction strategies (hooks)

Implement and wire compaction hooks via `after_turn_complete`.

## Quick start

- Copy `scripts/compaction_hooks.py` into your project hooks module.
- Pick a strategy and wire it via `tool_hooks.after_turn_complete`.
- Tune defaults in the hook or wrap it in a custom function (hook specs do not accept params).

### Agent card wiring

```yaml
tool_hooks:
  after_turn_complete: hooks.py:rolling_window
```

## Strategy menu

- **Rolling window** → `rolling_window`
- **Truncate over threshold** → `truncate_over_threshold`
- **Clear results (soft)** → `clear_results_soft`
- **Clear results (hard)** → `clear_results_hard`
- **Compaction prompt** → `compaction_prompt`

See [references/compaction.md](references/compaction.md) for the design summary.

## Hook requirements

- Guard with `if not ctx.is_turn_complete: return`.
- Use `ctx.usage` when available to read context usage.
- Use `ctx.load_message_history(...)` to replace history.
- Use `show_hook_message(...)` to display a compaction notice.

## Testing

Run a hook against saved history:

```bash
python scripts/hook_smoke_test.py \
  --hook path/to/hooks.py:rolling_window \
  --history ./history.json \
  --hook-type after_turn_complete \
  --output ./history-modified.json
```

---
> Converted and distributed by [TomeVault](https://tomevault.io/claim/fast-agent-ai) — claim your Tome and manage your conversions.
<!-- tomevault:4.0:skill_md:2026-04-13 -->

