Add provider batch API support with `BatchModel` and `model_request_batch`

Closed
KL
1mo ago
+17116-154

Provider batch APIs cost half, at up to a day of latency. This adds them at three levels, none of which needs durable execution: BatchModel for agents, model_request_batch for a list of requests, and batch_* methods on Model to submit in one process and collect in another. It builds on @Kludex's version of this PR and on #3937's API (@sarth6), and supports Anthropic, OpenAI Responses and Gemini.

BatchModel uses the agent graph's existing continuation loop: a waiting request suspends the turn with a response that records its batch, and later checks poll it. So it works in a plain agent.run(), under Temporal, DBOS and Prefect without engine-specific code, and a run resumed from its message history picks up the same batch. Concurrent requests in a process share batches. With PendingModelResponse in the output type, a run ends as soon as its request is submitted instead of waiting, so a job can submit, exit, and collect later by running the agent again with the stored history; this works for OpenAI background mode too.

New public surface

  • pydantic_ai.models.batch: BatchModel, BatchRequest, EncodedBatchRequest, Batch, BatchResult, BatchStatus, BatchResultStatus
  • pydantic_ai.direct.model_request_batch
  • pydantic_ai.PendingModelResponse (also pydantic_ai.output.PendingModelResponse): an output marker that ends a run while a model request waits on the provider
  • Model.batch_max_requests, batch_encode(), batch_create(), batch_status(), batch_results(), batch_cancel(), batch_decode(), implemented by AnthropicModel, OpenAIResponsesModel, GoogleModel, and in memory by TestModel and FunctionModel

User-visible behavior

agent = Agent(BatchModel('anthropic:claude-sonnet-4-5'), output_type=City)
results = await asyncio.gather(*(agent.run(q) for q in questions))  # one batch, half the price

# Submit now, collect later
agent = Agent(BatchModel('anthropic:claude-sonnet-4-5'), output_type=[City, PendingModelResponse])
result = await agent.run(q)  # ends once submitted: result.output is a PendingModelResponse
result = await agent.run(message_history=result.all_messages())  # later: the City, or pending again

Batch responses carry provider_details['processing'] == 'batch', so cost(), usage.cost and usage limits use genai-prices' batch rates.

Verification

What changes for existing users

Request builders were refactored so batches reuse them, without changing what they send. Cancelling a run whose response is suspended (for example an OpenAI background response) now lets the provider-side cancel finish, where a cancel making a request used to be interrupted; a second cancellation still abandons it.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.

🤖 Generated with Claude Code