gRPC API

Batch Management

xai_api.BatchMgmt

An API service for processing batch requests asynchronously at lower priority than chat service.

Methods of xai_api.BatchMgmt

CreateBatch

unary

Creates a new batch.

GetBatch

unary

Retrieves an individual batch.

ListBatches

unary

Retrieves a list of all batches owned by the team.

CancelBatch

unary

Stops processing of all outstanding requests in the batch.

AddBatchRequests

unary

Adds requests to a batch.

ListBatchRequestMetadata

unary

Lists metadata about individual requests in a batch.

ListBatchResults

unary

Lists processing results of a batch.

GetBatchRequestResult

unary

Retrieves an individual request in a batch.

Create a batch and add requests

import os
import xai_sdk
from xai_sdk.chat import user

client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))

# Create a batch
batch = client.batch.create("my_batch")
print(f"Batch ID: {batch.batch_id}")

# Add chat requests to the batch
chats = []
for country in ["UK", "USA", "Egypt"]:
    chat = client.chat.create(
        model="grok-4.6",
        batch_request_id=f"capital_{country.lower()}",
    )
    chat.append(user(f"What is the capital of {country}?"))
    chats.append(chat)

client.batch.add(batch.batch_id, chats)

List batches and get results

import os
import xai_sdk

client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))

# List all batches
batches = client.batch.list(limit=10)
for b in batches.batches:
    print(f"{b.name}: {b.batch_id}")

# Get results for a specific batch
results = client.batch.list_batch_results("BATCH_ID")
for r in results.results:
    print(f"{r.batch_request_id}: {r.response}")

Last updated: September 2, 2026