Skip to content

OpenAIResponsesCompactionSessionOptions

type OpenAIResponsesCompactionSessionOptions = object;
client?: OpenAIClient;

OpenAI client used to call responses.compact.

When omitted, the session will use getDefaultOpenAIClient() if configured. Otherwise it creates a new OpenAI() instance via new OpenAI().


compactionMode?: OpenAIResponsesCompactionMode;

Controls how the compaction request is built.

  • auto (default): Uses input when the last response was not stored or no response id is available.
  • previous_response_id: Uses the server-managed response chain.
  • input: Sends the locally stored session items as input and does not require a response id.

maxRollbackItems?: number;

Optional positive integer budget for complete history snapshots used for rollback. The budget plus one must be a safe integer. Omitting the budget preserves unlimited reads.

Snapshot reads request at most this many items plus one overflow item. When the runner replays users from a still-active compaction window, it skips automatic compaction if the stored history covered by the model input and response already exceeds the budget. Otherwise, an oversized rollback snapshot throws a UserError, including for manual and forced calls. The session checks the snapshot before the compaction API call and refreshes it before replacing history.

This does not limit bytes, tokens, stored history, model input, or ordinary candidate reads. Underlying retrieval defaults remain independent of this budget.


model?: OpenAI.ResponsesModel;

The OpenAI model to use for responses.compact.

Defaults to DEFAULT_OPENAI_MODEL. The final slash-delimited component must resemble an OpenAI model name (for example gpt-4.1, o3, or a fine-tuned ft:gpt-* identifier). For a namespaced ID such as openai/gpt-4.1, the client receives the full ID.


replayRetainedUserMessages?: boolean;

Include leading user messages retained by standalone compaction in fresh local model inputs and their input guardrails. Defaults to false.

This wrapper must be the only writer to the underlying session. Discard the wrapper after any external change. A new wrapper cannot replay users until it successfully compacts the session itself.

Replay requires the full history and a matching sequence of leading users followed by the latest compaction item. A pop, clear, uncertain write, or later compaction item ends replay of that window. Session and model input filters, server-managed history, handoffs, and resumes keep their usual input selection. Replay can increase token usage and allow automatic compaction to run again. If the replayed window is still active and the stored history covered by the model input and response already exceeds maxRollbackItems, the session skips automatic compaction and keeps the saved history. Other compaction errors can still surface after the answer has been saved.


shouldTriggerCompaction?: (context) => boolean | Promise<boolean>;

Custom decision hook that determines whether to call responses.compact.

The default implementation compares the length of OpenAIResponsesCompactionDecisionContext.compactionCandidateItems to an internal threshold (10). Override this to support token-based triggers or other heuristics using OpenAIResponsesCompactionDecisionContext.compactionCandidateItems or OpenAIResponsesCompactionDecisionContext.sessionItems.

Parameter Type

context

OpenAIResponsesCompactionDecisionContext

boolean | Promise<boolean>


underlyingSession?: Session & object;

Session store that receives items and holds the compacted history.

The underlying session is the source of truth for persisted items. Compaction clears the underlying session and writes the output items returned by responses.compact.

This must not be an OpenAIConversationsSession, because compaction relies on locally stored items and replaces the underlying session history after responses.compact.

Defaults to an in-memory session for demos.

Name Type

[OPENAI_SESSION_API]?

"responses"