Quick Start
Enable compaction on a session-bound agent
Bind a
session_id and turn on context compaction. When the window fills, the summary is written to the session automatically — no extra code.How It Works
The compactor produces a summary during the run; the store anchors that summary to the current end of the transcript. On resume the store hands back the summary followed by the retained tail. The checkpoint recordsmessage_index — the length of the transcript at compaction time. Anything appended after that index is the tail that follows the summary on resume. If the head is later trimmed, the anchor shifts by the same amount so the tail stays aligned.
What Gets Persisted
A single checkpoint is stored onSessionData.last_compaction. It carries the summary plus the anchor and observability counters.
| Field | Type | Default | Meaning |
|---|---|---|---|
summary | str | (required) | The condensed history the compactor produced |
message_index | int | 0 | Transcript length at compaction time; anything after this index is the tail |
role | str | "system" | Role used when replaying the summary as an LLM message |
tokens_before | int | 0 | Token count before compaction (observability) |
tokens_after | int | 0 | Token count after compaction (observability) |
timestamp | float | time.time() | When the checkpoint was written |
metadata | Dict[str, Any] | {} | Free-form extension slot |
SessionData also gains two helpers:
| Method | What it does |
|---|---|
trim_messages(max_messages) | Trims the transcript head, shifting the checkpoint anchor so the tail stays aligned |
get_working_history(max_messages=None) | Reconstructs [summary_message, *tail]; falls back to get_chat_history when no checkpoint exists. The summary is always kept at the head — only the tail is truncated to max_messages |
Advanced — Direct Store Access
Persist and read a checkpoint yourself via the store.CompactionCheckpoint is a top-level export.
append_compaction_checkpoint(session_id, summary, *, role="system", tokens_before=0, tokens_after=0, metadata=None) returns False and is a no-op for a blank/whitespace summary, so an empty {"role": "system", "content": ""} is never replayed. See Session Store for the full method reference.
Backward Compatibility
Sessions without a checkpoint resume from their raw messages exactly as before —get_working_history falls back to get_chat_history. Third-party stores that implement only the old protocol keep working; the agent checks for get_working_history / append_compaction_checkpoint with hasattr before using them.
Best Practices
Bind a session_id
Bind a session_id
The checkpoint is persisted to the bound session. Without a
session_id, compaction still runs in-memory but nothing is saved for a cheap resume.Enable compaction on long-lived agents
Enable compaction on long-lived agents
Turn on
execution=ExecutionConfig(context_compaction=True) for agents that run for hours or across many sessions — that’s where checkpoint-backed resume pays off.Don't hand-edit the checkpoint anchor
Don't hand-edit the checkpoint anchor
message_index is kept aligned by the store whenever the head is trimmed. Editing it by hand will misalign the retained tail on resume.set_chat_history intentionally invalidates the checkpoint
set_chat_history intentionally invalidates the checkpoint
Replacing the whole transcript with
set_chat_history() clears last_compaction — the old anchor no longer applies. clear_session() clears it too.Related
Session Persistence
Automatic session save and restore
Session Store
Store methods and checkpoint API
Context Compaction
How summaries are produced in-run

