高级 API 用法

异步请求

使用 xAI API 时,你可能需要处理数百甚至数千个请求。按顺序逐个发送这些请求可能非常耗时。

为了提高效率,可以使用 AsyncClient(来自 xai_sdk)或 AsyncOpenAI(来自 openai,从而并发发送多个请求。下面的 Python 脚本演示如何使用 AsyncClient批量异步处理请求,显著缩短总执行时间:

Rate Limit

调整 max_concurrent 参数,控制并行请求的最大数量。

并发请求数量不能超过 API Console 中显示的 rate limit。

import asyncio
import os

from xai_sdk import AsyncClient
from xai_sdk.chat import Response, user

async def main():
    client = AsyncClient(
        api_key=os.getenv("XAI_API_KEY"),
        timeout=3600, # Override default timeout with longer timeout for reasoning models
    )

    model = "grok-4.5"
    requests = [
        "Tell me a joke",
        "Write a funny haiku",
        "Generate a funny X post",
        "Say something unhinged",
    ]
    # Define a semaphore to limit concurrent requests (e.g., max 2 concurrent requests at a time)
    max_in_flight_requests = 2
    semaphore = asyncio.Semaphore(max_in_flight_requests)

    async def process_request(request) -> Response:
        async with semaphore:
            print(f"Processing request: {request}")
            chat = client.chat.create(model=model, max_tokens=100)
            chat.append(user(request))
            return await chat.sample()

    tasks = []
    for request in requests:
        tasks.append(process_request(request))

    responses = await asyncio.gather(*tasks)
    for i, response in enumerate(responses):
        print(f"Total tokens used for response {i}: {response.usage.total_tokens}")

if __name__ == "__main__":
    asyncio.run(main())