xAI API Integration for Python
Call Grok models through xAI’s Responses API in Python, with access to registered reasoning and structured output options.
Installation
Use Python 3.10–3.14 and run this command in your virtual environment.
python -m pip install llm-api-adapter==0.11.0 llm-api-adapter-xai==0.1.5Authentication
Open Grok API quickstart. Open the Grok Console from the guide, choose your team, and create its API key. Set the following in the terminal where you will run the example.
export XAI_API_KEY="your-api-key"Quickstart
Send a message
Save the example as chat.py and run python chat.py.
import os
from llm_api_adapter.models.messages.chat_message import UserMessage
from llm_api_adapter.universal_adapter import UniversalLLMAPIAdapter
adapter = UniversalLLMAPIAdapter(
organization="xai",
model="grok-4.7",
api_key=os.environ["XAI_API_KEY"],
)
response = adapter.chat(
messages=[UserMessage("Explain what an API is in one sentence.")],
max_tokens=1024,
timeout_s=60,
)
print(response.content)Supported models
Registry snapshot
llm-api-adapter-xai==0.1.5
Prices are first-tier base rates from the adapter registry. Open a model for all tiers, cache rates, pricing schedules, and capability notes. Unknown means the registry has no value.
grok-4.7
- Context tokens
- 500,000
- Base input per million tokens
- 2 USD
- Maximum output tokens
- 500,000
- Base output per million tokens
- 6 USD
- Reasoning
- Supported
- Image URLs
- Unknown
- PDF URLs
- Unknown
- PDF bytes
- Unknown
- JSON Schema output
- Unknown
- Pydantic output
- Unknown
grok-4.6
- Context tokens
- 500,000
- Base input per million tokens
- 2 USD
- Maximum output tokens
- 500,000
- Base output per million tokens
- 6 USD
- Reasoning
- Supported
- Image URLs
- Unknown
- PDF URLs
- Unknown
- PDF bytes
- Unknown
- JSON Schema output
- Unknown
- Pydantic output
- Unknown
grok-4.5
- Context tokens
- 500,000
- Base input per million tokens
- 2 USD
- Maximum output tokens
- 500,000
- Base output per million tokens
- 6 USD
- Reasoning
- Supported
- Image URLs
- Unknown
- PDF URLs
- Unknown
- PDF bytes
- Unknown
- JSON Schema output
- Unknown
- Pydantic output
- Unknown
Provider-specific features and limitations
- Provide the full message history each turn. The adapter does not send a provider response ID to continue a conversation.
- Configure data retention in the Grok Console. The adapter does not set the Responses API store parameter.