Add support for OpenAI background mode and handle Anthropic `stop_reason=pause_turn`
Merged
DO
5mo ago+12257+12257+01234567890123456789012345678901234567890123456789-103-103-012345678901234567890123456789
Supersedes #4306 (originally by @mattbrandman), moved to a branch on the main repo so CI and the auto-review bot can run against it. The design has changed substantially since that PR — there is no longer a separate continuation graph node; see below.
What this does
Adds general model-turn continuation infrastructure so that a single logical model turn can span multiple provider round-trips, and surfaces as one ModelResponse (and one AgentStream when streaming):
- Anthropic
stop_reason=pause_turn(#2600): when a long-running server-side tool (e.g. web search) makes Anthropic pause a turn, it is now continued automatically instead of erroring. Segments are accumulated into a single response. - OpenAI background mode (#3268): opt in with the
openai_background=Truemodel setting. Background responses are polled (openai_background_poll_interval) — or, when started withstream=True, resumed viaretrieve(stream=True, starting_after=...)— until they complete, and surfaced as one response/stream.
Both are built on shared primitives:
- A new
'suspended'ModelResponseStateandModelResponse.suspended_retry_delay, marking a response the agent should continue. - Continuation handled inline in
ModelRequestNode(no separate graph node), sowrap_model_request/before_model_request/after_model_requesthooks fire once around the whole turn, including its continuations. Model.cancel_suspended_response()(no-op by default;OpenAIResponsesModelcancels the background job) so an interrupted or cancelled run tears down the server-side job instead of leaving it running.FallbackModelpins continuations back to the model that produced the suspended response.
Docs
docs/models/anthropic.md—pause_turnhandlingdocs/models/openai.md— background modedocs/hooks.md— hooks fire once per turn, including continuations
Checklist
- Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
- No breaking changes in accordance with the version policy.
- PR title is fit for the release changelog.