Skip to main content
API Reference: 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.
This guide explains how to perform batch predictions using TrueFoundry’s AI Gateway with OpenAI, Microsoft Foundry, Azure OpenAI, Vertex AI, or AWS Bedrock providers. Anthropic batches use Anthropic’s native Message Batches API instead; see Anthropic Message Batches. For how the gateway prices OpenAI and Anthropic batches, see Batch cost tracking.

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:
Requirements: Valid JSON per line, meaningful 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:PassRole to 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, use url: "/v1/responses" and body.input in each JSONL line.
See Azure OpenAI batch documentation for details.
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:
  1. Upload: Upload JSONL file → Get file ID
  2. Create: Create batch job → Get batch ID
  3. Monitor: Check status until complete
  4. Fetch: Download results

Step-by-Step Examples

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.
Anthropic Provider
Anthropic Provider
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 batch
  • in_progress: Processing the requests
  • completed: All requests processed successfully
  • failed: Batch processing failed (use error_file_id from 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 via output_file_id
Anthropic batches report 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

  1. 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.
  2. While the provider runs it, the gateway checks each unfinished batch with the provider about every 30 minutes.
  3. 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_batches and output_cost_per_token_batches rates. 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.
Private (custom) pricing on a model and service-tier overlays are not applied to batches. For example, 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.
On self-hosted gateways, batch cost tracking needs gateway tracing to be enabled. If tracing is off, creating an OpenAI or Anthropic batch that would be costed returns 503.

Best Practices

  1. File Format: Use meaningful custom_id values and valid JSONL format
  2. Error Handling: Implement proper error handling and status monitoring
  3. Security: Store API keys securely, use minimal IAM permissions
  4. 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:
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.objectAdmin or equivalent object-level read/write permissions on the bucket).
Create batch prediction jobs
  • The service account must have permission to create and manage batch prediction jobs in Vertex AI (e.g. roles/aiplatform.user or the aiplatform.batchPredictionJobs.create permission).

AWS Bedrock Permissions

These are the minimum permissions required to use the Bedrock Batch APIs. For complete official guidance, see AWS Bedrock Batch Inference Permissions.
The service role (role_arn) used for creating and executing the batch job requires:Trust Relationship:
Permission Policy: