Guide

Use Grok with the OpenAI SDK

You don't need xAI's own SDK to call Grok. Nicrron serves Grok models through an OpenAI-compatible API, so the OpenAI SDK you already use calls them after two changes: the base URL and the key.

The same key also calls every other model on Nicrron, so trying Grok against what you use today is a one-word change.

1. Get a key

Make an API key in the dashboard and add credit. Keep the key in an environment variable, for example NICRRON_API_KEY.

2. Point the SDK at Nicrron

Set base_url (Python) or baseURL (TypeScript) to https://api.nicrron.ai/v1, and use the model id xai/grok-4.7.

Python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.nicrron.ai/v1",
    api_key=os.environ["NICRRON_API_KEY"],
)

reply = client.chat.completions.create(
    model="xai/grok-4.7",
    messages=[{"role": "user", "content": "Write a haiku about the sea."}],
)
print(reply.choices[0].message.content)
TypeScript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.nicrron.ai/v1",
  apiKey: process.env.NICRRON_API_KEY,
});

const reply = await client.chat.completions.create({
  model: "xai/grok-4.7",
  messages: [{ role: "user", content: "Write a haiku about the sea." }],
});
console.log(reply.choices[0].message.content);

3. Pick a Grok model

Model idContextInput / 1MOutput / 1M
xai/grok-code-fast-1200k$1.00$2.00
xai/grok-4.20-non-reasoning200k$1.25$2.50
xai/grok-4.20200k$1.25$2.50
xai/grok-4.7200k$2.00$6.00
xai/grok-4.6200k$2.00$6.00
xai/grok-4.5200k$2.00$6.00
xai/grok-4.3200k$1.25$2.50

List prices in US dollars from the live catalog; requests paid from credit add a 5% platform fee. More on the xAI models page.

4. Stream the answer

Streaming works the same as with OpenAI: pass stream=True and read the chunks.

Python
stream = client.chat.completions.create(
    model="xai/grok-4.7",
    messages=[{"role": "user", "content": "Explain DNS in three sentences."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

5. Add a fallback

Nicrron takes a models list as well as model. If Grok 4.7 can't answer, the same request goes to the next model in the list, and reply.model says which one answered. The OpenAI SDK sends extra fields through extra_body.

Python
reply = client.chat.completions.create(
    model="xai/grok-4.7",
    messages=[{"role": "user", "content": "Hello!"}],
    # Tried in order if Grok 4.7 can't answer: an outage or a rate limit.
    extra_body={"models": ["openai/gpt-6.2-sol"]},
)
print(reply.model)  # the model that actually answered

More in Fall back to another model.

Questions

Can I use Grok with the OpenAI SDK?
Yes. Point the OpenAI SDK at https://api.nicrron.ai/v1 with a Nicrron API key, and set the model to a Grok id such as "xai/grok-4.7". The request and the answer have the OpenAI chat completions shape, so the rest of your code stays the same.
Which Grok models can I call this way?
7 right now: Grok Code Fast 1, Grok 4.20 (non-reasoning), Grok 4.20, Grok 4.7, Grok 4.6, Grok 4.5, Grok 4.3. The ids and prices are on nicrron.ai/models.
Do streaming and function calling work with Grok?
Streaming works with stream: true, as server-sent events of chat.completion.chunk. Tools use the OpenAI tools format; Nicrron passes them to models that support function calling.
How much does Grok cost through Nicrron?
The provider's list price per token, shown for each model on its page. Requests paid from Nicrron credit add a 5% platform fee. API keys spend paid credit: add credit in the dashboard, with no subscription.