POST /batches
Provider capabilities
The table below summarizes gateway support for this endpoint by provider.Legend:
- ✅ Supported by provider and TrueFoundry
- Provided by provider, but not by TrueFoundry
- Provider does not support this feature
Anthropic batches use Anthropic’s native
/v1/messages/batches API; see Anthropic Message Batches.
For every gateway endpoint and provider, see Supported APIs.
The Batch API does not support Virtual Models. Batch jobs run asynchronously on a single provider, so the AI Gateway cannot apply virtual model routing, retries, or fallbacks to requests inside a batch. Always pass a real catalog model identifier (for example
openai-main/gpt-4o) as the model when creating a batch job — passing a virtual model name will be rejected.Client Setup
All providers use the OpenAI SDK with provider-specific headers. Choose your provider to get started:Input File Format
Create a JSONL file with one JSON object per line. Each line represents a single request:custom_id values. See provider notes below for any provider-specific limits (e.g. minimum records, URL/body format).
Before using AWS Bedrock batch processing, ensure you have:
- S3 Bucket: For storing input and output files.
- IAM Execution Role: With permissions for S3 access and Bedrock model invocation.
- User Permissions: Including
iam:PassRoleto pass the execution role to Bedrock.
Before using Vertex AI batch processing, ensure you have:
- Cloud Storage Bucket: The bucket must be in the same region as the model, and the service account must have read/write access to the bucket.
- Batch Prediction Permissions: The service account must have permission to create batch prediction jobs.
Before using Azure OpenAI batch processing, ensure you have:
- Deployment type: A model deployed as Global Batch or Data Zone Batch (standard/online deployments cannot be used for batch).
- Headers: Set
x-tfy-azure-api-version(e.g.2024-12-01-preview) to match the API version used by your batch deployment. - Endpoint: When creating the job, use
endpoint="/chat/completions"for chat (Azure API uses this form). For the Responses API, useurl: "/v1/responses"andbody.inputin each JSONL line.
Before using Microsoft Foundry batch processing, ensure you have a deployment that supports batch processing and an integration for the deployment in your Microsoft Foundry account. Microsoft Foundry v1 uses implicit API versioning; do not add a dated Azure API version header unless the integration uses the legacy
api_version fallback.Workflow Steps
The batch process follows these steps for all providers:- Upload: Upload JSONL file → Get file ID
- Create: Create batch job → Get batch ID
- Monitor: Check status until complete
- Fetch: Download results
Step-by-Step Examples
1. Upload Input File
1. Upload Input File
2. Create Batch Job
2. Create Batch Job
3. Check Batch Status
3. Check Batch Status
4. Fetch Results
4. Fetch Results
Anthropic Message Batches
Anthropic batches go through Anthropic’s native Message Batches API at/v1/messages/batches, not /batches. There is no input file to upload: the requests are sent in the create call. Use the Anthropic SDK with the gateway as its base URL, and set x-tfy-provider-name to your Anthropic provider account.
Every request in a batch must use the same model. Set it in each request’s
params.model, either as the TrueFoundry model name (for example anthropic-main/claude-sonnet-4-5) or as the Anthropic model id (claude-sonnet-4-5). A TrueFoundry model name must belong to the provider account in x-tfy-provider-name; the gateway replaces it with the Anthropic model id before sending the batch. A batch that mixes models is rejected with 400.1. Create Message Batch
1. Create Message Batch
Anthropic Provider
2. Check Batch Status
2. Check Batch Status
Anthropic Provider
3. Fetch Results
3. Fetch Results
Fetch results from the gateway’s results endpoint. The SDK’s
client.messages.batches.results() downloads from the results_url in the batch, which points at Anthropic directly and does not go through the gateway.Anthropic Provider
Batch Status Reference
validating: Initial validation of the batchin_progress: Processing the requestscompleted: All requests processed successfullyfailed: Batch processing failed (useerror_file_idfrom the batch response to fetch error details)finalizing: Results being prepared (OpenAI / Azure OpenAI)expired: Did not complete within the time window (OpenAI / Azure OpenAI)cancelling/cancelled: Batch cancelled; partial results may be available viaoutput_file_id
processing_status instead: in_progress, canceling, then ended. Once a batch has ended, each request’s outcome (succeeded, errored, canceled, or expired) is in its result line and in request_counts.
Batch cost tracking
The AI Gateway records the cost of OpenAI and Anthropic batches after the provider finishes them. Batch spend then shows up in Metrics, counts against budget limits, and appears on the trace of the request that created the batch.How a batch is costed
- When you create a batch that will be costed (see Which batches are costed), the gateway checks your budgets. If an enforced budget for that model is already used up, the request is rejected with 429 and nothing is sent to the provider. Otherwise the gateway records who submitted the batch (user, team, virtual account), the model, and the budgets that matched.
- While the provider runs it, the gateway checks each unfinished batch with the provider about every 30 minutes.
- When the provider finishes, the gateway reads the batch results, prices each successful request, and records one cost entry for the whole batch. On the trace of the create request, it appears as a
Model: <model> (batch)span with the batch’s token counts and cost.
Which batches are costed
For OpenAI, the gateway does not read the input file, so it takes the model from the
x-tfy-provider-model header on the create request. Set it to the model name in your provider account (for example gpt-4.1 for openai-main/gpt-4.1) or to the OpenAI model id. Without the header, the batch still runs but its cost is not recorded.
OpenAI Provider
How batch prices are calculated
Batch requests are priced from the public pricing catalog at the provider’s batch rates:- Input and output tokens use the catalog’s
input_cost_per_token_batchesandoutput_cost_per_token_batchesrates. When a model has no batch rate, the gateway uses half the standard rate, which is the discount OpenAI and Anthropic give batch jobs. - Cache read and cache write tokens are priced at half the standard cache rates.
- Tiered pricing, such as long-context rates, is halved tier by tier.
- The provider account’s discount %, if set, still applies.
- Only successful requests are charged. Failed, errored, cancelled, and expired requests cost nothing; requests that finished before a batch was cancelled or expired are still charged.
openai-main/gpt-4.1 costs $2 per 1M input tokens and $8 per 1M output tokens normally, and $1 and $4 in a batch. A batch of 1,000 successful requests with 500 input and 100 output tokens each is billed as 500,000 × $1/1M + 100,000 × $4/1M = **$0.90**, half the $1.80 the same requests cost one at a time.
Timing and limits
- Cost is recorded when the batch is priced, not when it was created. A batch created on the last day of a month and finished the next day counts toward the new month’s budget.
- The gateway stops checking a batch 3 days after it was created. A batch the provider has not finished by then is not costed.
- Result files larger than 200 MB are not costed.
Best Practices
- File Format: Use meaningful
custom_idvalues and valid JSONL format - Error Handling: Implement proper error handling and status monitoring
- Security: Store API keys securely, use minimal IAM permissions
- Provider-specific: Check the prerequisites above (e.g. Azure: batch deployment type and endpoint; Bedrock: minimum 100 records, IAM and S3)
Vertex AI Permissions
The following permissions are required for Vertex AI batch prediction:Storage and service account requirements
Storage and service account requirements
Cloud Storage bucket
- The Cloud Storage bucket used for batch input and output must be in the same region as the Vertex AI model you are using.
- The service account that runs the batch job must have read and write access to this bucket (e.g.
roles/storage.objectAdminor equivalent object-level read/write permissions on the bucket).
Batch prediction job permissions
Batch prediction job permissions
Create batch prediction jobs
- The service account must have permission to create and manage batch prediction jobs in Vertex AI (e.g.
roles/aiplatform.useror theaiplatform.batchPredictionJobs.createpermission).
AWS Bedrock Permissions
User Permissions (for API calls)
User Permissions (for API calls)
These are the minimum permissions required to use the Bedrock Batch APIs. For complete official guidance, see AWS Bedrock Batch Inference Permissions.
Service Role Permissions (for batch execution)
Service Role Permissions (for batch execution)
The service role (role_arn) used for creating and executing the batch job requires:Trust Relationship:Permission Policy: