Skip to content

Commit bd33a52

Browse files
authored
docs: #2343 clarify how to use compaction in practice (#2344)
1 parent 34ba1d0 commit bd33a52

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

docs/sessions/index.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,54 @@ result = await Runner.run(
153153
print(result.final_output) # "California"
154154
```
155155

156+
### OpenAI Responses compaction sessions
157+
158+
Use `OpenAIResponsesCompactionSession` to compact session history with the Responses API (`responses.compact`). It wraps an underlying session and can automatically compact after each turn based on `should_trigger_compaction`.
159+
160+
#### Typical usage (auto-compaction)
161+
162+
```python
163+
from agents import Agent, Runner, SQLiteSession
164+
from agents.memory import OpenAIResponsesCompactionSession
165+
166+
underlying = SQLiteSession("conversation_123")
167+
session = OpenAIResponsesCompactionSession(
168+
session_id="conversation_123",
169+
underlying_session=underlying,
170+
)
171+
172+
agent = Agent(name="Assistant")
173+
result = await Runner.run(agent, "Hello", session=session)
174+
print(result.final_output)
175+
```
176+
177+
By default, compaction runs after each turn once the candidate threshold is reached.
178+
179+
#### auto-compaction can block streaming
180+
181+
Compaction clears and rewrites the session history, so the SDK waits for compaction to finish before considering the run complete. In streaming mode, this means `run.stream_events()` can stay open for a few seconds after the last output token if compaction is heavy.
182+
183+
If you want low-latency streaming or fast turn-taking, disable auto-compaction and call `run_compaction()` yourself between turns (or during idle time). You can decide when to force compaction based on your own criteria.
184+
185+
```python
186+
from agents import Agent, Runner, SQLiteSession
187+
from agents.memory import OpenAIResponsesCompactionSession
188+
189+
underlying = SQLiteSession("conversation_123")
190+
session = OpenAIResponsesCompactionSession(
191+
session_id="conversation_123",
192+
underlying_session=underlying,
193+
# Disable triggering the auto compaction
194+
should_trigger_compaction=lambda _: False,
195+
)
196+
197+
agent = Agent(name="Assistant")
198+
result = await Runner.run(agent, "Hello", session=session)
199+
200+
# Decide when to compact (e.g., on idle, every N turns, or size thresholds).
201+
await session.run_compaction({"force": True})
202+
```
203+
156204
### SQLite sessions
157205

158206
The default, lightweight session implementation using SQLite:
@@ -443,6 +491,7 @@ For detailed API documentation, see:
443491

444492
- [`Session`][agents.memory.session.Session] - Protocol interface
445493
- [`OpenAIConversationsSession`][agents.memory.OpenAIConversationsSession] - OpenAI Conversations API implementation
494+
- [`OpenAIResponsesCompactionSession`][agents.memory.openai_responses_compaction_session.OpenAIResponsesCompactionSession] - Responses API compaction wrapper
446495
- [`SQLiteSession`][agents.memory.sqlite_session.SQLiteSession] - Basic SQLite implementation
447496
- [`SQLAlchemySession`][agents.extensions.memory.sqlalchemy_session.SQLAlchemySession] - SQLAlchemy-powered implementation
448497
- [`DaprSession`][agents.extensions.memory.dapr_session.DaprSession] - Dapr state store implementation

0 commit comments

Comments
 (0)