A conversation that grows without a limit will fill the context window silently, and the run will fail or degrade exactly when it is longest and most valuable. A turn limit is the guardrail: after a set number of turns, the older turns are summarized into the facts the run still needs, and the conversation continues from the summary. The OpenAI Agents SDK supports this shape directly, with session settings that limit how much history is fetched and a compaction session that rewrites history when it meets a threshold. Pick the limit from the model’s context window and the size of a typical turn, not from a round number, and tell the user when a summary happens, so the change is visible, not silent.
What you need
- A long-running agent conversation, with its typical turn size noted
- A turn limit based on the context window and the turn size
- A summary shape: goal, decisions, key facts, open items
Step 1: Set the limit from the window and the turn size
Work out how many turns fit in the context window at a typical turn size, and set the limit below that number with headroom. A limit set from a round number may be too high or too low.
Step 2: Summarize the older turns when the limit is hit
When the turn count reaches the limit, replace the older turns with a summary of the goal, decisions, key facts, and open items, and keep the recent turns whole. A summary that drops the goal is worse than no summary.
Step 3: Tell the user a summary happened, and check the next turn
After summarizing, note it in the conversation so the change is visible, then run the next turn and confirm the agent still uses the kept facts. If it asks for something the summary dropped, the summary was too aggressive.
Worked example: synthetic turn limit check
The table below is a synthetic example. It shows the expected result of the turn limit; no conversation was run.
| Check | Expected | Result |
|---|---|---|
| Limit set from window size | Yes | Pass |
| Summary at turn 20 | Yes | Pass |
| Next turn uses kept facts | Yes | Pass |
| User told about the summary | Yes | Pass |
Expected result (UNTESTED). the limit is set from the window size, the conversation summarizes at turn 20, and the next turn uses the kept facts. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- A summary loses detail by design, so a fact the summary drops is gone from the conversation.
- A limit set too low summarizes constantly, wasting tokens on summaries.
- The recent turns must stay whole, or the agent loses the thread of the current exchange.
- Summarizing does not recover tokens already spent; it only makes room for what comes next.
