Skip to content
LinkedInX

What Is Context Compaction?

Target audience: Those who want to continue long AI sessions or distinguish summaries and history deletion from memory
Prerequisites: Basic understanding of The Context Window and Its Limits and What Is Prompt Caching?

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 decision

The 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 prioritizeWhy it matters
Current goal and completion conditionsPrevents the task from losing its destination
Binding constraints and permissionsPreserves prohibitions and safety conditions
Accepted decisions and rejected optionsPrevents a return to obsolete approaches
Edited files and external stateKeeps the work’s current position aligned
Verification results and open problemsPrevents repeated failures
Next actionMakes the immediate continuation clear
This table scrolls horizontally. Keyboard users can focus the table and use the left and right arrow keys.

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:
MechanismMain operationReduces context size?Main risk
Context compactionSummarize, organize, or replace historyYesLoss of a needed detail
Simple truncationDelete old historyYesImportant information disappears too
Prompt cachingReuse computation for the same input prefixNoCache misses after input changes
MemorySupply useful information to later conversationsNot necessarilyStale or irrelevant memory enters context
RAGRetrieve selected parts of external sourcesDepends on selectionMissed retrieval or incorrect evidence
This table scrolls horizontally. Keyboard users can focus the table and use the left and right arrow keys.

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

  1. OpenAI, Compaction
  2. Anthropic, Best practices for Claude Code
  3. Anthropic, How Claude Code works
  4. OpenAI, Configuration Reference
  5. OpenAI, Projects and chats
  6. OpenAI, Memories
  7. OpenAI, Prompt caching

Freshness note: Automatic-compaction triggers, commands, settings, and API formats can change. Check each product’s current specification before use.

Quiz