Launch offer50% off up to $5,000, then 3% for lifeSee offer →

GUIDES · 7 MIN READ

How to Use the Grok API with the OpenAI SDK: A Drop-In Developer Guide

The Grok API speaks OpenAI's protocol. Here's how to connect it to your existing Python or Node.js SDK with one line -- and cut the token bill by 50%.

RK

Ravi Kumar

Co-founder ·

If you are already building on OpenAI's API and want to test Grok without rewriting your integration, the barrier is lower than you think. xAI designed the Grok API to speak OpenAI's protocol, which means the only required change is a base URL and an API key. Your existing openai SDK calls go through unchanged.

This guide covers how to wire it up, what the pricing looks like across all current Grok models, and how to cut that bill in half using Grokified.

What "OpenAI-Compatible" Actually Means

OpenAI-compatible means the Grok API accepts the same endpoint paths (/v1/chat/completions, /v1/models), the same JSON request body structure, and the same streaming format as OpenAI. Any tool or library that supports a custom base_url works with Grok without touching a line of application logic.

That includes:

  • openai Python SDK (v1.x and above)
  • openai Node.js/TypeScript SDK
  • LangChain, LlamaIndex, Vercel AI SDK, and CrewAI
  • Any HTTP client or CI pipeline that can set a base URL

You are not replacing a library. You are replacing a string.

Grok Model Names and Current Pricing

Before writing any code, know what you are pointing at. As of June 2026, xAI offers the following models through the API:

ModelInput (per 1M tokens)Output (per 1M tokens)Context Window
grok-4.3$1.25$2.501M tokens
grok-3$3.00$15.00131K tokens
grok-3-mini$0.25$0.50131K tokens
grok-code-fast-1$0.20$1.50--

grok-4.3 is the current xAI flagship: $2.50 per million output tokens with a 1M token context window puts it at the low end of frontier model pricing. Threads on X this week called it the cheapest US-based frontier model, and the numbers support that -- it undercuts GPT-4o ($15/M output) and Claude Opus 4.8 by a wide margin.

grok-3-mini is the cost floor for high-volume tasks. At $0.50/M output, it is competitive with the cheapest hosted models available from any provider.

Setting Up the Grok API with the OpenAI Python SDK

Install the standard OpenAI package if you do not already have it:

pip install openai

Then configure a client pointed at xAI's endpoint:

from openai import OpenAI

client = OpenAI(
    api_key="your-xai-api-key",
    base_url="https://api.x.ai/v1",
)

response = client.chat.completions.create(
    model="grok-4.3",
    messages=[
        {"role": "system", "content": "You are a concise technical assistant."},
        {"role": "user", "content": "What is the difference between input and output tokens?"},
    ],
)

print(response.choices[0].message.content)

The response object is structurally identical to an OpenAI response. Your existing parsing code, retry logic, and streaming handlers require no changes. If you have error handling built around OpenAI error types, those will still trigger correctly because the error format matches.

Switching to Grokified for 50% Off

Grokified is an OpenAI-compatible proxy for the Grok API. It sells all Grok models at 50% off xAI list price for your first $10,000 of usage, saving up to $5,000, then applies a permanent 3% discount after that threshold. There is no subscription. You top up from $10 and get $5 in free credits on signup.

The code change to use Grokified instead of xAI direct is one line:

from openai import OpenAI

client = OpenAI(
    api_key="your-grokified-api-key",         # from grokified.com
    base_url="https://api.grokified.com/v1",  # only change from xAI direct
)

response = client.chat.completions.create(
    model="grok-4.3",
    messages=[
        {"role": "system", "content": "You are a concise technical assistant."},
        {"role": "user", "content": "What is the difference between input and output tokens?"},
    ],
)

print(response.choices[0].message.content)

Same models. Same response format. Same streaming behavior. The proxy routes your request to xAI and returns the response without meaningful latency overhead for most production workloads.

To verify the connection before touching application code, use curl:

curl https://api.grokified.com/v1/chat/completions \
  -H "Authorization: Bearer your-grokified-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "grok-4.3",
    "messages": [{"role": "user", "content": "Hello"}]
  }'

A 200 with a valid completion means the connection is working. A 401 means the key is wrong. A 404 means the base URL has a typo -- it should be https://api.grokified.com/v1 with no trailing slash.

The Actual Cost Math

At xAI list price, grok-4.3 output costs $2.50 per million tokens. At Grokified's 50% discount, that drops to $1.25 per million.

A production workload burning 10 million output tokens per month pays $25 direct to xAI. Through Grokified, that same workload costs $12.50 per month within your first $10,000 of cumulative spend.

For a team spending $2,000 per month on Grok tokens, the $10K milestone arrives in five months. Savings in that window total $5,000. After the milestone, the 3% lifetime discount applies to every token going forward.

Migration Gotchas to Watch For

Rate limits. xAI's rate limits are tiered by account level. If you are on a low-tier account pushing high throughput, expect 429 errors sooner than you would on GPT-4o. Grokified applies xAI's limits directly without adding an additional throttle layer. Check x.ai/api for current tier limits.

Context windows. grok-4.3 has a 1M token context window, substantially larger than GPT-4o's 128K. If your application hard-codes a context ceiling, you can safely raise it with grok-4.3. grok-3 and grok-3-mini use 131K context windows.

Tool calling. Grok's function calling follows the OpenAI spec. Parallel tool calls, tool choice, and structured outputs all work. Test your existing tool definitions against Grok with a sample of real data before migrating all traffic.

System prompt behavior. Instruction-following on grok-4.3 is strong, but subtle differences from GPT-4o exist around formatting and refusals. If your application depends on exact output structure, run a regression test against your eval set before going live.

Verifying Your Setup Works

List available models to confirm the client is pointed at the right endpoint:

models = client.models.list()
for m in models.data:
    print(m.id)

You should see Grok model IDs in the output. If the call errors, check that base_url ends with /v1 and that your API key is set correctly. A 401 is a key problem; a 404 is a URL problem.

Which Grok Model to Start With

grok-3-mini is the right starting point for classification, summarization, extraction, and other high-volume tasks where cost matters more than peak accuracy. At $0.50/M output through Grokified's pricing, it is one of the cheapest capable hosted models available.

grok-4.3 is the production choice for reasoning, coding, and long-document tasks. The 1M context window removes chunking requirements for most document sizes, and the $1.25/M output price at Grokified is well below comparable frontier models.

grok-code-fast-1 fills a niche: latency-sensitive coding tasks at $1.50/M output, where you need code-optimized generation without the full cost of grok-4.3.

Grok does not currently offer embeddings or image generation endpoints. For applications that depend on those capabilities, you will need a separate provider for those specific calls. For chat completions and tool use, the Grok API covers the core production requirements.

Get Started

Sign up at grokified.com, claim your $5 in free credits, and grab an API key. Swap the base_url in your existing client config to https://api.grokified.com/v1 and run a test completion against grok-4.3.

If your LLM API bill has been climbing and your code already uses the OpenAI SDK, this is the lowest-friction way to cut it without touching your application logic.

grok apiopenai sdkllm proxyapi migrationxai
RK

Ravi Kumar

Co-founder, Grokified

Previously built billing infrastructure for two developer platforms. Writes about the unglamorous parts of running an API business.

Keep reading

All posts →