Skip to content

OpenAIResponsesCompactionSession

Session decorator that triggers responses.compact when the stored history grows.

This session is intended to be passed to run() so the runner can automatically supply the latest responseId and invoke compaction after each completed turn is persisted.

To debug compaction decisions, enable the debug logger for openai-agents:openai:compaction (for example, DEBUG=openai-agents:openai:compaction).

new OpenAIResponsesCompactionSession(options): OpenAIResponsesCompactionSession;
Parameter Type

options

OpenAIResponsesCompactionSessionOptions

OpenAIResponsesCompactionSession

readonly [OPENAI_SESSION_API]: "responses";
OpenAISessionApiTagged.[OPENAI_SESSION_API]
addItems(items): Promise<void>;

Append new items to the conversation history.

Parameter Type Description

items

AgentInputItem[]

Items to add to the session history.

Promise<void>

OpenAIResponsesCompactionAwareSession.addItems


clearSession(): Promise<void>;

Remove all items that belong to the session and reset its state.

Promise<void>

OpenAIResponsesCompactionAwareSession.clearSession


getItems(limit?): Promise<AgentInputItem[]>;

Retrieve items from the conversation history.

Parameter Type Description

limit?

number

The maximum number of items to return. When provided the most recent limit items should be returned in chronological order.

Promise<AgentInputItem[]>

OpenAIResponsesCompactionAwareSession.getItems


getSessionId(): Promise<string>;

Ensure and return the identifier for this session.

Promise<string>

OpenAIResponsesCompactionAwareSession.getSessionId


popItem(): Promise<
| AgentInputItem
| undefined>;

Remove and return the most recent item from the conversation history if it exists.

Promise< | AgentInputItem | undefined>

OpenAIResponsesCompactionAwareSession.popItem


runCompaction(args?): Promise<
| OpenAIResponsesCompactionResult
| null>;

Invoked by the runner after it persists a completed turn into the session.

Implementations may decide to call responses.compact (or an equivalent API) and replace the stored history.

This hook is best-effort. Implementations should consider handling transient failures and deciding whether to retry or skip compaction for the current turn.

Parameter Type

args?

OpenAIResponsesCompactionArgs

Promise< | OpenAIResponsesCompactionResult | null>

OpenAIResponsesCompactionAwareSession.runCompaction