XAI
batchling is compatible with XAI through any supported framework
The following endpoints are made batch-compatible by XAI:
/v1/chat/completions
Check model support and batch pricing
Before sending batches, review the provider's official pricing page for supported models and batch pricing details.
The Batch API docs for XAI can be found on the following URL:
https://docs.x.ai/developers/advanced-api-usage/batch-api
XAI client compatibility
The XAI provider is not made available through the base client but through an OpenAI client with modified base_url.
This is due to XAI heavy reliance on gRPC architecture within its client over REST-based requests, making it harder to intercept requests.
Example Usage
API key required
Set XAI_API_KEY in .env or ensure it is already loaded in your environment variables before running batches.
Here's an example showing how to use batchling with XAI:
import asyncio
import os
from dotenv import load_dotenv
from openai import AsyncOpenAI
from batchling import batchify
load_dotenv()
async def build_tasks() -> list:
"""Build OpenAI requests."""
client = AsyncOpenAI(api_key=os.getenv(key="XAI_API_KEY"), base_url="https://api.x.ai/v1")
questions = [
"Who is the best French painter? Answer in one short sentence.",
"What is the capital of France?",
]
return [
client.chat.completions.create(
messages=[{"role": "user", "content": question}], model="grok-4-1-fast-non-reasoning"
)
for question in questions
]
async def main() -> None:
"""Run the OpenAI example."""
tasks = await build_tasks()
responses = await asyncio.gather(*tasks)
for response in responses:
model = response.chat_get_completion["model"]
content = response.chat_get_completion["choices"][0]["message"]["content"]
print(f"{model} answer:\n{content}\n")
async def run_with_batchify() -> None:
"""Run `main` inside `batchify` for direct script execution."""
async with batchify():
await main()
if __name__ == "__main__":
asyncio.run(run_with_batchify())
Output:
grok-4-1-fast-non-reasoning answer:
Claude Monet is widely regarded as the best French painter for his revolutionary Impressionist masterpieces like *Water Lilies*.
grok-4-1-fast-non-reasoning answer:
**Paris** is the capital of France.
This is a well-established fact: Paris has served as the capital since the 10th century (with brief interruptions, like during the French Revolution), and it's confirmed by official sources such as the French government's website (e.g., diplomatie.gouv.fr) and international bodies like the United Nations. It's home to landmarks like the Eiffel Tower and Louvre, and France's central government is based there.