You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sessions/index.md
+49Lines changed: 49 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -153,6 +153,54 @@ result = await Runner.run(
153
153
print(result.final_output) # "California"
154
154
```
155
155
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
+
156
204
### SQLite sessions
157
205
158
206
The default, lightweight session implementation using SQLite:
@@ -443,6 +491,7 @@ For detailed API documentation, see:
0 commit comments