Add provider batch API support with `BatchModel` and `model_request_batch`
- Closes #1771
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,BatchResultStatuspydantic_ai.direct.model_request_batchpydantic_ai.PendingModelResponse(alsopydantic_ai.output.PendingModelResponse): an output marker that ends a run while a model request waits on the providerModel.batch_max_requests,batch_encode(),batch_create(),batch_status(),batch_results(),batch_cancel(),batch_decode(), implemented byAnthropicModel,OpenAIResponsesModel,GoogleModel, and in memory byTestModelandFunctionModel
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
- Recorded batches per provider: Anthropic, OpenAI, Gemini (round trip, item error, cancellation, agent run,
model_request_batch) - Queueing, resumption, continuations, cancellation and
PendingModelResponse:tests/models/test_batch.py PendingModelResponsewith streamed background responses (run_stream,run_stream_events):tests/models/test_continuation_stream.py- Temporal, DBOS and Prefect:
test_*batch_model*intests/durable_exec/
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