xAI Grok API: Connecting Grok 4.7, Price and the 256K Context

How to call grok-4.7 through an OpenAI-compatible API, what to do about the 256K context limit and what typical requests cost. Plus a comparison with Grok 4.6.

4 min · · models · API keys

If you need the xAI Grok API but connecting directly is inconvenient, the grok-4.7 model can be called through the gateway in the ordinary OpenAI-compatible format. Below: how to connect, how a 256K context differs from the million tokens of other models, how to send images, and what typical requests cost. You can top up the balance from Russia by SBP, MasterCard or crypto.

What the model is

Prices as of October 18, 2026, $ per 1M tokens. "Index" is the Intelligence Index (Artificial Analysis).

ModelZukko input / outputCache: read / writeOfficial input / outputContextImagesIndex
grok-4.70.175 / 0.5130.025 / 0.1753 / 15256Kyes47
grok-4.60.138 / 0.40.013 / 0.1383 / 15256Kyes44
grok-4.50.113 / 0.3380.013 / 0.1133 / 15256Kyes40

The main difference from most catalog models: a 256K context, not 1M. For conversations, code and medium-length documents it is plenty, but for huge repositories and books you will have to cut the input or pick a model with a 1M context. Grok 4.7 has a higher intelligence index than 4.6 (47 versus 44), and a higher Zukko price too. Context, image input and official prices are the same.

Connecting

You need a fetch_... key, created in the Zukko dashboard. The OpenAI-format address ends in /v1.

bash
pip install openai
export ZUKKO_API_KEY="fetch_your_key"
bash
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.zukko.pro/v1",
    api_key=os.environ["ZUKKO_API_KEY"],
)

resp = client.chat.completions.create(
    model="grok-4.7",
    messages=[
        {"role": "system", "content": "Answer briefly."},
        {"role": "user", "content": "Explain what request idempotency is."},
    ],
    max_tokens=400,
)

print(resp.choices[0].message.content)
print(resp.usage)

The same with curl:

bash
curl https://api.zukko.pro/v1/chat/completions \
  -H "Authorization: Bearer $ZUKKO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"grok-4.7","messages":[{"role":"user","content":"Hello"}],"max_tokens":100}'

If you get a 404 model_not_found, check the id: it is grok-4.7, with a dot.

Image input

grok-4.7 accepts images. In the OpenAI format an image is passed as an image_url block; a local file is encoded as a data URL:

bash
import base64

with open("screenshot.png", "rb") as f:
    b64 = base64.b64encode(f.read()).decode()

resp = client.chat.completions.create(
    model="grok-4.7",
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "What is wrong with this screenshot?"},
            {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}},
        ],
    }],
    max_tokens=500,
)
print(resp.choices[0].message.content)

The image counts toward input tokens; see exactly how many in usage.prompt_tokens after the request rather than guessing in advance. Common size and format errors are covered in the article on image upload errors.

What to do about the 256K context limit

If the input exceeds the model window, the request ends with an error. Typical outcomes: 413 if the request is too large, or a provider message about context overflow; the wording may change, so rely on the response code. Such errors are analyzed in the article on context_length_exceeded. Practical techniques:

  • Trim the conversation history: keep the system prompt and the last N messages, and compress older ones into a short summary.
  • For long documents, split them into parts, process them one by one and then merge the results.
  • For truly large inputs, choose a model with a 1M context, for example claude-sonnet-5-5 or gpt-6.1-sol.
  • Watch usage.prompt_tokens and alert yourself when the input approaches 80% of the window.

A simple guard against overflow by character count (a rough estimate, verify against usage):

bash
MAX_CHARS = 300_000  # a heuristic with margin for Russian text

def trim_history(messages: list[dict]) -> list[dict]:
    system, rest = messages[0], messages[1:]
    while rest and sum(len(m["content"]) for m in rest if isinstance(m["content"], str)) > MAX_CHARS:
        rest.pop(0)
    return [system] + rest

What typical requests cost

Formula: input / 1M × input price + output / 1M × output price (+ cache, if any).

Chat: 3000 input and 500 output tokens

  • Grok 4.7: 3000 × 0.175 / 1M + 500 × 0.513 / 1M = 0.000525 + 0.0002565 = about $0.00078.
  • Grok 4.6: 3000 × 0.138 / 1M + 500 × 0.4 / 1M = 0.000414 + 0.0002 = about $0.00061.

A nearly full window: 200,000 input and 2000 output tokens

  • Grok 4.7: 200,000 × 0.175 / 1M + 2000 × 0.513 / 1M = 0.035 + 0.001026 = about $0.036.
  • Grok 4.6: 200,000 × 0.138 / 1M + 2000 × 0.4 / 1M = 0.0276 + 0.0008 = about $0.028.

The same request if 150,000 tokens are read from cache. Cache is counted when the provider reports the read in the response. For Grok 4.7: fresh input 50,000 × 0.175 / 1M = 0.00875; read 150,000 × 0.025 / 1M = 0.00375; output 0.001026; total about $0.0135. The gateway does not bill cache write separately for such models.

You can see that the price grows not from "model magic" but from input size: a long context is expensive by itself. We cover this topic separately in the article on the cost of long context. Always check current prices via GET https://api.zukko.pro/v1/models or the "Models" page in the dashboard.

Grok 4.7 or 4.6

The difference by the table: index 47 versus 44, input 0.175 versus 0.138, output 0.513 versus 0.4. If Grok 4.6 gives answers at the level your task needs, it costs less. If not, the upgrade is justified, but you can only confirm that with a test on your data, not with the index. A review of the previous version is in the article Grok 4.6 via API.

FAQ

Do I need a separate xAI account?

No. A fetch_... key is enough: it works with all catalog models, including Claude, GPT and DeepSeek.

Does the Anthropic format work for Grok?

Any model can be called in either the OpenAI format or the Anthropic format. For code on the OpenAI SDK it is simpler to use /v1/chat/completions.

Why is the context only 256K?

That is the model's limit in the catalog. If you need a million tokens, choose from the models with a 1M context in the GET /v1/models list.

How do I limit spending?

Create a separate key with a daily dollar limit and a list of allowed models, and set max_tokens in requests.

To try the model, create a key in the Zukko dashboard: top-ups from $1.

zukko

Pay 25×
less

for Claude Code & ChatGPT

One fetch_* key and base URL api.zukko.pro — for Cursor, Claude Code, Codex, and any SDK.

Get API keyDocumentation

api.zukko.pro · fetch_*

xAI Grok API: Grok 4.7, price and 256K context · Zukko