API / call-grok-4-7-via-api

API

Call Grok 4.7 via the API

Send requests with model id grok-4.7 to POST https://api.x.ai/v1/responses. Official: Grok 4.7, Models, Quickstart. Announcement: Introducing Grok 4.7.

Export a key from the API Keys page:

export XAI_API_KEY="your_api_key"

At a glance

Property Value
Model name grok-4.7
Context window 500,000 tokens
Knowledge cutoff May 2026
Modalities Text and image input; text output
Reasoning low, medium, high (default), or xhigh
APIs Responses, Chat Completions

Token rates

Prompt size Input / 1M Cached input / 1M Output / 1M
Below 200k prompt tokens $2.00 $0.50 $6.00
≥ 200k prompt tokens $4.00 $1.00 $12.00

Long-context pricing bills all tokens in the request at the higher rate once the prompt reaches the threshold (Models).

curl

curl https://api.x.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -d '{
    "model": "grok-4.7",
    "input": "Find and fix the bug, then explain it: function median(a){a.sort();return a[a.length/2]}"
  }'

Python (xai_sdk)

pip install xai-sdk
import os
from xai_sdk import Client
from xai_sdk.chat import user

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

chat = client.chat.create(model="grok-4.7")
chat.append(user("Find and fix the bug, then explain it: function median(a){a.sort();return a[a.length/2]}"))

response = chat.sample()
print(response.content)

Prompt cache key and encrypted reasoning

Set a prompt_cache_key on Responses (or the x-grok-conv-id header on Chat Completions). That routes a conversation's requests to the same server so cache hits stay reliable; without it you often pay full input on a cache-cold server.

On Responses, grok-4.7 always returns reasoning.encrypted_content, even when include does not list it. Pass those reasoning items back unchanged in the next request's input so multi-turn keeps the model's reasoning (Encrypted reasoning content). Chat Completions is unchanged on that point.

Pitfalls

Related