What Is Context Compaction?
Context compaction replaces a long conversation or work history with a smaller representation of the state needed for future work. Its purpose is to free space in the context window while continuing the same task.
This is not necessarily lossless compression like a ZIP archive. It often combines summarization, removal of old tool output, and extraction of important state, so details can be lost.
What Gets Smaller
A long agent task mixes information that remains active with information whose job is finished.
Long history
├─ Goal, constraints, and decisions ────────┐
├─ Current file state and open issues ──────┼→ compacted task state
├─ Next action ─────────────────────────────┘
├─ Full logs for resolved errors ────────────→ remove or retain a short finding
└─ Long exploration of rejected options ─────→ remove or retain only the decisionThe compacted task state and recent messages then become the context for continued work. The OpenAI Responses API supports both automatic compaction through context management and explicit compaction through a dedicated endpoint.[1]
Task State Worth Preserving
Good compaction does more than shorten prose. It retains the state needed for the next decision.
| Information to prioritize | Why it matters |
|---|---|
| Current goal and completion conditions | Prevents the task from losing its destination |
| Binding constraints and permissions | Preserves prohibitions and safety conditions |
| Accepted decisions and rejected options | Prevents a return to obsolete approaches |
| Edited files and external state | Keeps the work’s current position aligned |
| Verification results and open problems | Prevents repeated failures |
| Next action | Makes the immediate continuation clear |
Full search results, complete logs, already-applied drafts, and small talk that did not affect a decision are candidates for shortening. Information needed for later audit or reproduction should still be stored outside the conversation even if it leaves the active context.
Claude Code Example
As Claude Code approaches its context limit, it first clears older tool output and then summarizes the conversation to compact it automatically. The /compact command starts compaction manually and can include instructions about what to emphasize.[2][3]
Fine-grained instructions from early in the session may be omitted from the summary. Anthropic therefore recommends recording persistent rules in a durable instruction file such as CLAUDE.md instead of relying on conversation history alone.[3]
Codex Example
Codex provides a setting for the token threshold that triggers automatic history compaction. Its configuration reference describes scopes based on either total token count or the message body after a stable prefix.[4]
For agents built with the OpenAI API, compacted output may be an opaque item intended to be passed directly to a later request rather than a human-readable summary.[1] A design should therefore treat a human handoff note and model-readable compacted state as separate artifacts.
How to Think About ChatGPT
Public ChatGPT project guidance describes keeping related chats, files, instructions, and sources together in a project, while separating chats by distinct outcome.[5] Memory can make useful information available across conversations.[6]
These features help organize long-running work, but they are not necessarily the same implementation as the exposed compaction mechanisms in an API or Claude Code. When splitting long work across ChatGPT conversations, create an explicit handoff summary for the next chat:
Goal:
Settled decisions:
Binding constraints:
Open questions:
Sources to consult:
Next action:How Related Mechanisms Differ
| Mechanism | Main operation | Reduces context size? | Main risk |
|---|---|---|---|
| Context compaction | Summarize, organize, or replace history | Yes | Loss of a needed detail |
| Simple truncation | Delete old history | Yes | Important information disappears too |
| Prompt caching | Reuse computation for the same input prefix | No | Cache misses after input changes |
| Memory | Supply useful information to later conversations | Not necessarily | Stale or irrelevant memory enters context |
| RAG | Retrieve selected parts of external sources | Depends on selection | Missed retrieval or incorrect evidence |
Prompt caching and compaction can be used together. However, if compaction rewrites the conversation prefix, an earlier cache may no longer be reusable.[7] Do not keep obsolete history merely to preserve cache efficiency; choose a compaction point based on task quality and context capacity.
Limits of Compaction
Compaction cannot perfectly predict what will matter later. The following details are especially easy to lose:
- The exact sequence leading to an error
- Subtle wording or user intent
- An exception condition mentioned only once
- Precise values inside tool output
- The comparison process behind a selected approach
For important decisions, verify against original sources, source code, meeting records, or audit logs rather than trusting compacted state alone. If the same misunderstanding continues after compaction, or if the task’s goal has changed, starting a fresh conversation can be safer than carrying the old state forward.
Summary
- Context compaction reduces a long history to the task state needed for future work
- It can use summarization, removal of old tool output, and extraction of important state
- Claude Code and Codex provide automatic compaction mechanisms, and the OpenAI API can pass compacted state into later requests
- ChatGPT projects and memory help organize work but are not the same as an exposed compaction API
- Compaction can lose information, so persistent rules and verification records also belong outside the conversation
References
- OpenAI, Compaction
- Anthropic, Best practices for Claude Code
- Anthropic, How Claude Code works
- OpenAI, Configuration Reference
- OpenAI, Projects and chats
- OpenAI, Memories
- OpenAI, Prompt caching
Freshness note: Automatic-compaction triggers, commands, settings, and API formats can change. Check each product’s current specification before use.