Recipe: Context Compaction

This recipe shows how to compact long conversations before they hit context limits.

Compaction means:

  1. summarize prior discussion,
  2. reset old context, then
  3. continue from a short handoff summary.

How this works

  1. Hook runs after each assistant pass.
  2. If token usage is below LIMIT, nothing happens.
  3. If over LIMIT, stdin conversation is piped to a separate lectic -S summarizer call.
  4. Hook outputs LECTIC:reset to drop prior context and LECTIC:final to stop an extra follow-up pass.
  5. Next turn starts from the compacted summary.

Variation: use the active assistant to summarize first

If you want compaction to use the same assistant (including its reasoning behavior), use an in-band reset flow with :reset[].

macros:
  - name: compact
    expansion: |
      exec:#!/usr/bin/env bash
      cat <<'EOF'
      :reset[]

      Before reset applies, summarize our discussion with:
      - current goal
      - decisions and constraints
      - important files/commands
      - remaining tasks
      EOF

Usage: :compact[]

This is not as automatic, but it gives the model one response to produce the handoff summary before context is truncated.