Grok (xAI) Text Generation
Grok is xAI's large language model series, fully OpenAI-compatible, so you can migrate existing OpenAI apps with zero code changes. It supports up to 500k token context and image understanding.
Endpoint
POST $URL/v1/chat/completionsSupported Models
| Model | Description | Context |
|---|---|---|
grok-4.6 | Flagship, strongest overall capability and speed | 500k |
grok-4.5 | Previous flagship | 500k |
Parameters
| Param | Type | Required | Description |
|---|---|---|---|
model | string | yes | Model name above |
messages | array | yes | Conversation messages; system/user/assistant can be mixed in any order |
stream | boolean | no | Stream the response, default false |
temperature | number | no | Sampling temperature 0–2 |
max_tokens | integer | no | Max tokens to generate |
tools / tool_choice | array / string | no | Function calling |
response_format | object | no | Structured outputs (e.g. {"type":"json_object"}) |
image | string / object | no | Image input (passed in message content as image), jpg/png, ≤ 20MiB each |
Request Example
bash
curl $URL/v1/chat/completions \
-H "Authorization: Bearer $YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-4.6",
"messages": [
{"role": "user", "content": "Introduce yourself in one sentence"}
]
}'python
from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY", base_url="$URL/v1")
resp = client.chat.completions.create(
model="grok-4.6",
messages=[{"role": "user", "content": "Introduce yourself in one sentence"}],
)
print(resp.choices[0].message.content)Billing Notes
- Billed by actual usage (input / output / cached input priced separately); see the Console "Model Market" for unit prices;
- Long-context tiers: once a request's prompt reaches 200k tokens, all tokens of that request are billed at the long-context rate (even if the completion itself is short);
- Cached input tokens are billed at a discounted rate; there is no separate cache-write fee;
- Reasoning tokens are billed as output usage.
Notes
logprobs/top_logprobsare not supported and are silently ignored;- Grok's knowledge is cut off at training time; enable search tools in the request for real-time information (billed per upstream usage).
