Add support for OpenAI background mode and handle Anthropic `stop_reason=pause_turn`

Merged
DO
5mo ago
+12257-103

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=True model setting. Background responses are polled (openai_background_poll_interval) — or, when started with stream=True, resumed via retrieve(stream=True, starting_after=...) — until they complete, and surfaced as one response/stream.

Both are built on shared primitives:

  • A new 'suspended' ModelResponseState and ModelResponse.suspended_retry_delay, marking a response the agent should continue.
  • Continuation handled inline in ModelRequestNode (no separate graph node), so wrap_model_request / before_model_request / after_model_request hooks fire once around the whole turn, including its continuations.
  • Model.cancel_suspended_response() (no-op by default; OpenAIResponsesModel cancels the background job) so an interrupted or cancelled run tears down the server-side job instead of leaving it running.
  • FallbackModel pins continuations back to the model that produced the suspended response.

Docs

  • docs/models/anthropic.md — pause_turn handling
  • docs/models/openai.md — background mode
  • docs/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.