{"slug": "compaction", "title": "Compaction", "summary": "OpenAI introduces compaction for long-running AI interactions, reducing context size while preserving state. The feature is available via server-side compaction in Responses API or a standalone compact endpoint, helping balance quality, cost, and latency.", "body_md": "## Overview\n\nTo support long-running interactions, you can use compaction to reduce context size while preserving state needed for subsequent turns.\n\nCompaction helps you balance quality, cost, and latency as conversations grow.\n\n## Server-side compaction\n\nYou can enable server-side compaction in a Responses create request\n(`POST /responses`\n\nor `client.responses.create`\n\n) by setting\n`context_management`\n\nwith `compact_threshold`\n\n.\n\n- When the rendered token count crosses the configured threshold, the server runs server-side compaction.\n- No separate\n`/responses/compact`\n\ncall is required in this mode. - The response stream includes the encrypted compaction item.\n- ZDR note: server-side compaction is ZDR-friendly when you set\n`store=false`\n\non your Responses create requests.\n\nThe returned compaction item carries forward key prior state and reasoning into the next run using fewer tokens. It is opaque and not intended to be human-interpretable.\n\nFor stateless input-array chaining, append output items as usual. If you are\nusing `previous_response_id`\n\n, pass only the new user message each turn. In both\ncases, the compaction item carries context needed for the next window.\n\nLatency tip: After appending output items to the previous input items, you can\ndrop items that came before the most recent compaction item to keep requests\nsmaller and reduce long-tail latency. The latest compaction item carries the\nnecessary context to continue the conversation. If you use\n`previous_response_id`\n\nchaining, do not manually prune.\n\n## User journey\n\n- Call\n`/responses`\n\nas usual, but include`context_management`\n\nwith`compact_threshold`\n\nto enable server-side compaction. - As the response streams, if the context size crosses the threshold, the server triggers a compaction pass, emits a compaction output item in the same stream, and prunes context before continuing inference.\n- Continue your loop with one pattern: stateless input-array chaining (append\noutput, including compaction items, to your next input array) or\n`previous_response_id`\n\nchaining (pass only the new user message each turn and carry that ID forward).\n\n## Example user flow\n\n```\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\nconversation = [\n    {\n        \"type\": \"message\",\n        \"role\": \"user\",\n        \"content\": \"Let's begin a long coding task.\",\n    }\n]\n\nwhile keep_going:\n    response = client.responses.create(\n        model=\"gpt-5.3-codex\",\n        input=conversation,\n        store=False,\n        context_management=[{\"type\": \"compaction\", \"compact_threshold\": 200000}],\n    )\n\n    conversation.extend(response.output)\n\n    conversation.append(\n        {\n            \"type\": \"message\",\n            \"role\": \"user\",\n            \"content\": get_next_user_input(),\n        }\n    )\n```\n\n## Standalone compact endpoint\n\nFor explicit control, use the\n[standalone compact endpoint](/api/docs/api-reference/responses/compact) for\nstateless compaction in long-running workflows.\n\nThis endpoint is fully stateless and ZDR-friendly.\n\nYou send a full context window (messages, tools, and other items), and the\nendpoint returns a new compacted context window you can pass to your next\n`/responses`\n\ncall.\n\nThe returned compacted window includes an encrypted compaction item that carries forward key prior state and reasoning using fewer tokens. It is opaque and not intended to be human-interpretable.\n\nNote: the compacted window generally contains more than just the compaction item. It can also include retained items from the previous window.\n\nOutput handling: do not prune `/responses/compact`\n\noutput. The returned window\nis the canonical next context window, so pass it into your next `/responses`\n\ncall as-is.\n\n### User journey for standalone compaction\n\n- Use\n`/responses`\n\nnormally, sending input items that include user messages, assistant outputs, and tool interactions. - When your context window grows large, call\n`/responses/compact`\n\nto generate a new compacted context window. The window you send to`/responses/compact`\n\nmust still fit within your model’s context window. - For subsequent\n`/responses`\n\ncalls, pass the returned compacted window (including the compaction item) as input instead of the full transcript.\n\n### Example user flow\n\n```\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n# Full window collected from prior turns\nlong_input_items_array = [...]\n\n# 1) Compact the current window\ncompacted = client.responses.compact(\n    model=\"gpt-5.6\",\n    input=long_input_items_array,\n)\n\n# 2) Start the next turn by appending a new user message\nnext_input = [\n    *compacted.output,  # Use compact output as-is\n    {\n        \"type\": \"message\",\n        \"role\": \"user\",\n        \"content\": user_input_message(),\n    },\n]\n\nnext_response = client.responses.create(\n    model=\"gpt-5.6\",\n    input=next_input,\n    store=False,  # Keep the flow ZDR-friendly\n)\n```\n\n", "url": "https://wpnews.pro/news/compaction", "canonical_source": "https://developers.openai.com/api/docs/guides/compaction", "published_at": "2026-07-18 08:12:07+00:00", "updated_at": "2026-07-18 08:21:12.291342+00:00", "lang": "en", "topics": ["artificial-intelligence", "ai-products", "ai-tools", "developer-tools"], "entities": ["OpenAI", "Responses API"], "alternates": {"html": "https://wpnews.pro/news/compaction", "markdown": "https://wpnews.pro/news/compaction.md", "text": "https://wpnews.pro/news/compaction.txt", "jsonld": "https://wpnews.pro/news/compaction.jsonld"}}