Skip to content
For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
Primary navigation

Compact a response

responses.compact(ResponseCompactParams**kwargs) -> CompactedResponse
POST/responses/compact

Compact a conversation. Returns a compacted response object.

Learn when and how to compact long-running conversations in the conversation state guide. For ZDR-compatible compaction details, see Compaction (advanced).

ParametersExpand Collapse
model: Union[Literal["gpt-5.6-sol", "gpt-5.6-terra", "gpt-5.6-luna", 96 more], str, null]

Model ID used to generate the response, like gpt-5 or o3. OpenAI offers a wide range of models with different capabilities, performance characteristics, and price points. Refer to the model guide to browse and compare available models.

input: Optional[Union[str, Iterable[ResponseInputItemParam], null]]

Text, image, or file inputs to the model, used to generate a response

instructions: Optional[str]

A system (or developer) message inserted into the model’s context. When used along with previous_response_id, the instructions from a previous response will not be carried over to the next response. This makes it simple to swap out system (or developer) messages in new responses.

previous_response_id: Optional[str]

The unique ID of the previous response to the model. Use this to create multi-turn conversations. Learn more about conversation state. Cannot be used in conjunction with conversation.

prompt_cache_key: Optional[str]

A key to use when reading from or writing to the prompt cache.

maxLength64
prompt_cache_options: Optional[PromptCacheOptions]

Options for prompt caching. Supported for gpt-5.6 and later models. By default, OpenAI automatically chooses one implicit cache breakpoint. You can add explicit breakpoints to content blocks with prompt_cache_breakpoint. Each request can write up to four breakpoints. For cache matching, OpenAI considers up to the latest 80 breakpoints in the conversation, without a content-block lookback limit. Set mode to explicit to disable the implicit breakpoint. The ttl defaults to 30m, which is currently the only supported value. See the prompt caching guide for current details.

Deprecatedprompt_cache_retention: Optional[Literal["in_memory", "24h"]]

How long to retain a prompt cache entry created by this request.

service_tier: Optional[Literal["auto", "default", "fast", 2 more]]

Specifies the processing type used for serving the request. - If set to ‘auto’, then the request will be processed with the service tier configured in the Project settings. Unless otherwise configured, the Project will use ‘default’. - If set to ‘default’, then the request will be processed with the standard pricing and performance for the selected model. - If set to ‘flex’, then the request will be processed with the Flex Processing service tier. - To opt-in to Fast mode at the request level, include the service_tier=fast or service_tier=priority parameter for Responses or Chat Completions. The response will show service_tier=priority regardless of if you specify service_tier=fast or priority in your request. - When not set, the default behavior is ‘auto’. When the service_tier parameter is set, the response body will include the service_tier value based on the processing mode actually used to serve the request. This response value may be different from the value set in the parameter.

ReturnsExpand Collapse
class CompactedResponse:
id: str

The unique identifier for the compacted response.

created_at: int

Unix timestamp (in seconds) when the compacted conversation was created.

formatunixtime
object: Literal["response.compaction"]

The object type. Always response.compaction.

output: List[ResponseOutputItem]

The compacted list of output items. This is a list of all user messages, followed by a single compaction item.

Token accounting for the compaction pass, including cached, reasoning, and total tokens.

Compact a response

from openai import OpenAI

client = OpenAI()

compacted_response = client.responses.compact(
    model="gpt-5.1-codex-max",
    input=[
    {
        "role": "user",
        "content": "Create a simple landing page for a dog petting cafe.",
    },
    # All items returned from previous requests are included here, like reasoning, message, function call, etc.
    {
        "id": "msg_001",
        "type": "message",
        "status": "completed",
        "content": [
        {
            "type": "output_text",
            "annotations": [],
            "logprobs": [],
            "text": "Below is a single file, ready-to-use landing page for a dog petting café:...",
        },
        ],
        "role": "assistant",
    },
    ]
)
# Pass the compacted_response.output as input to the next request
print(compacted_response)
{
  "id": "resp_001",
  "object": "response.compaction",
  "created_at": 1764967971,
  "output": [
    {
      "id": "msg_000",
      "type": "message",
      "status": "completed",
      "content": [
        {
          "type": "input_text",
          "text": "Create a simple landing page for a dog petting cafe."
        }
      ],
      "role": "user"
    },
    {
      "id": "cmp_001",
      "type": "compaction",
      "encrypted_content": "gAAAAABpM0Yj-...="
    }
  ],
  "usage": {
    "input_tokens": 139,
    "input_tokens_details": {
      "cached_tokens": 0,
      "cache_write_tokens": 0
    },
    "output_tokens": 438,
    "output_tokens_details": {
      "reasoning_tokens": 64
    },
    "total_tokens": 577
  }
}
Returns Examples
{
  "id": "resp_001",
  "object": "response.compaction",
  "created_at": 1764967971,
  "output": [
    {
      "id": "msg_000",
      "type": "message",
      "status": "completed",
      "content": [
        {
          "type": "input_text",
          "text": "Create a simple landing page for a dog petting cafe."
        }
      ],
      "role": "user"
    },
    {
      "id": "cmp_001",
      "type": "compaction",
      "encrypted_content": "gAAAAABpM0Yj-...="
    }
  ],
  "usage": {
    "input_tokens": 139,
    "input_tokens_details": {
      "cached_tokens": 0,
      "cache_write_tokens": 0
    },
    "output_tokens": 438,
    "output_tokens_details": {
      "reasoning_tokens": 64
    },
    "total_tokens": 577
  }
}