Skip to main content
kRouter
All posts
Comparisons

Grok in Claude Code and Codex: three ways to pay for it

Grok 4.5 to 4.7 in Claude Code or Codex through Grok Build, OpenCode Go or the xAI API: what each costs, which API it speaks, and the setup.

Kodelyth · The team behind kRouter
· Updated
9 min read

You already pay for OpenCode Go, which includes Grok. You point Claude Code at a gateway, pick grok-4.7, and the gateway sends it to OpenCode's Chat Completions endpoint, as kRouter itself did before v0.5.163. The answer:

HTTP 401
{"type":"error","error":{"type":"ModelError","message":"Model grok-4.7 is not supported for format oa-compat"}}

The key is fine. The problem is where Grok lives. OpenCode serves Grok only on its Responses API, and the endpoint behind a Grok Build subscription speaks the Responses API too. The Anthropic-compatible endpoint Claude Code would need is one xAI now marks as deprecated. Codex has an easier time, because the Responses API is the only one it speaks.

So "Grok in Claude Code" is really two questions: which of the three ways of paying for Grok you use, and what translates between that API and your client.

Three ways to pay for Grok

A Grok subscription with Grok Build. xAI announced Grok Build on May 25, 2026 as a terminal coding agent for SuperGrok and X Premium+ subscribers, and open-sourced it under Apache 2.0 in July. Signed in with a subscription, its usage comes out of that plan rather than API credits, through cli-chat-proxy.grok.com, an endpoint that speaks the Responses API and is meant for xAI's own CLI.

OpenCode Go. $10 a month, or $40 for Go Plus, per OpenCode's Go docs. The docs put Grok 4.7 and 4.6 on the /responses endpoint with a $15 monthly usage limit on Go, which OpenCode works out at about 169 requests per 5-hour window for a request that is mostly cached input. The live roster at opencode.ai/zen/go/v1/models also lists grok-4.5. Limits are 20% of the monthly figure per 5 hours and 50% per week, and OpenCode notes that Grok requests are retained for 30 days and not used for training.

xAI API credits. Pay per token with a key from console.x.ai. From xAI's model list at the time of writing:

ModelContextInput / output per 1M tokens, prompt under 200kAt 200k and above
grok-4.7500k$2.00 / $6.00$4.00 / $12.00
grok-4.5500k$2.00 / $6.00$4.00 / $12.00
grok-4.31M$1.25 / $2.50$2.50 / $5.00
grok-build-0.1256k$1.00 / $2.00$2.00 / $4.00

grok-4.6 is priced like 4.7. grok-build-0.1 is the model xAI now serves in place of the retired grok-code-fast-1, though xAI's own advice on the same page is to use Grok 4.7 for code. Once a prompt reaches 200k tokens, xAI bills every token in that request at the higher rate.

What each client reaches on its own

RouteAPI that serves GrokClaude Code, connected directlyCodex, connected directly
Grok Build subscriptionResponses, at an endpoint meant for xAI's CLINoNo
OpenCode GoResponses onlyNoYes; OpenCode lists Codex as a validated client
xAI APIResponses; Chat Completions and Messages are both marked deprecatedOnly through the deprecated Messages endpointPossibly, through /v1/responses; test it before relying on it

If you use Codex and only want OpenCode Go's Grok, connect directly. A router adds a hop and nothing else. The rest of this post is for Claude Code, for Grok Build, or for mixing these routes behind one model name.

What does not work

Pointing Claude Code at xAI's Messages endpoint. xAI's legacy endpoints reference says the Anthropic SDK compatibility is fully deprecated and tells users to migrate to the Responses API or gRPC. Do not build a daily setup on it.

Sending Grok to OpenCode Go's Chat Completions or Messages endpoint. You get the ModelError above, which OpenCode sends before it even looks at the key; a 400 ModelProtocolUnsupported has also been reported. A new key and retries change nothing. kRouter itself did this before v0.5.163, and took OpenCode's 401 for a bad key. OpenCode Go's three protocols maps every Go model to the endpoint that serves it.

The Grok Web provider, for agents. kRouter can sign in to grok.com with your browser cookie, but that connector flattens the whole conversation into one text message and never sends tool definitions. It works for chat. Claude Code and Codex run on tool calls, so it is no use to them.

Old model ids. xAI retired grok-code-fast-1, grok-3 and grok-4-fast-reasoning, among others, on May 15, 2026. The ids still resolve: grok-code-fast-1 now goes to grok-build-0.1 and most of the others to grok-4.3, and xAI bills a request to a retired id at grok-4.3's rates, not the old model's. kRouter 0.5.163's built-in xAI list still shows those three ids, so add the current ones from the provider page's live list.

Claude Code on Grok, through kRouter

kRouter translates each request from Claude Code's Anthropic Messages format to whatever the upstream speaks: Chat Completions for xAI, Responses for Grok Build and OpenCode Go's Grok. Install it and start it in the background:

npm install -g @sifxprime/krouter
krouter -t

Open http://localhost:20128/dashboard, go to Providers and connect whichever routes you pay for:

  • Grok CLI (Grok Build): sign in with your xAI account through a device code. kRouter lists grok-4.5, the one model Grok Build's catalog published when the connector was verified in v0.5.110, plus -low, -medium and -high versions of it.
  • OpenCode Go: paste the API key from the OpenCode console. Its Grok models are grok-4.5, grok-4.6 and grok-4.7.
  • xAI (Grok): paste a key from console.x.ai, then add the current models from the From API list on the provider page, or with Add Model. Send each one a test request from that page before you rely on it.

Then open CLI Tools, pick the Claude Code card, map the Opus, Sonnet and Haiku slots and click Apply. kRouter writes them into the env block of ~/.claude/settings.json. The shell equivalent, all on a Grok Build subscription:

export ANTHROPIC_BASE_URL=http://localhost:20128/v1
export ANTHROPIC_AUTH_TOKEN=<your-krouter-key>
export ANTHROPIC_DEFAULT_OPUS_MODEL=gcli/grok-4.5-high
export ANTHROPIC_DEFAULT_SONNET_MODEL=gcli/grok-4.5-medium
export ANTHROPIC_DEFAULT_HAIKU_MODEL=gcli/grok-4.5-low

The effort versions are not separate models. kRouter strips the suffix and sends grok-4.5 with that reasoning effort, so a client that can only choose a model name can still choose an effort. Plain gcli/grok-4.5 runs at high. If the client sends a reasoning effort of its own, that wins over the suffix. Claude Code uses the Haiku slot for background work, which is why it gets the low effort. With OpenCode Go or xAI, the same slots take ocg/grok-4.7 or xai/grok-4.7.

On the same machine kRouter needs no key, and the card writes the placeholder sk_krouter if you have none. From another machine every request needs a real key from the Endpoint page.

Set the context window. Claude Code does not recognize a Grok model name, so it assumes 200K and compacts there. xAI lists 500k for Grok 4.5 to 4.7, and the Claude Code card's Max context dropdown has a 500K preset that writes CLAUDE_CODE_MAX_CONTEXT_TOKENS as 498000, just under the limit. On xAI credits, think before raising it: every request whose prompt reaches 200k tokens is billed at double the rate. Context windows with other models covers the setting in full.

Codex on Grok, through kRouter

On the same CLI Tools page, open the OpenAI Codex CLI / App card, choose a Grok model and click Apply. kRouter merges this into ~/.codex/config.toml:

model = "gcli/grok-4.5"
model_provider = "krouter"
 
[model_providers.krouter]
name = "kRouter"
base_url = "http://localhost:20128/v1"
wire_api = "responses"
 
[model_providers.krouter.http_headers]
Authorization = "Bearer <your-krouter-key>"
 
[agents]
default_subagent_model = "gcli/grok-4.5"

Codex sends Responses requests. kRouter sends Grok Build and OpenCode Go's Grok to a Responses endpoint, and Chat Completions to xAI. For a single run, codex -m ocg/grok-4.7 overrides the model. Codex CLI with any model explains the model_providers block.

Protocol gotchas

Run v0.5.163 or later. krouter -v prints the version. That release sent OpenCode Go's Grok models to /responses, and fixed these for every provider kRouter reaches through a Responses API, Grok Build included: a reply cut off by the token limit now ends with a proper stop reason and usage, so Claude Code's stream closes; a client that asked for one JSON reply gets a real error when the upstream fails, instead of a successful reply whose text is the error; and every system and developer message reaches the model, not just the first.

Grok Build always streams. kRouter collects the stream for clients that asked for one JSON reply.

The xAI connector uses Chat Completions. xAI's own comparison calls it the legacy API, marks it deprecated and recommends the Responses API, but gives no removal date. Function calling works on it, and that is what Claude Code and Codex use. xAI's server-side tools, such as web search and code execution, are only on the Responses API, so they are out of reach this way. Responses API vs Chat Completions explains how the two differ.

One model name, three backends

A router earns its place when one allowance runs out. Open Combos in the dashboard and create one named grok with three entries:

  1. gcli/grok-4.5-high, the subscription you have already paid for
  2. ocg/grok-4.7, OpenCode Go's 5-hour window
  3. xai/grok-4.7, pay per token, last

A combo tries its entries in order. When one fails with a rate-limit or quota error, the next answers, and Claude Code or Codex keeps sending the name grok. The entries are not identical models, so expect replies to change a little when the combo moves down the list. Combos has the details.

Choosing

You already haveSimplest routeUse kRouter when
SuperGrok or X Premium+Grok Build's own CLIYou want Grok inside Claude Code or Codex
OpenCode GoCodex connected directlyYou use Claude Code, or want Grok next to Kimi, GLM or DeepSeek
An xAI API keyGrok Build with XAI_API_KEY, which xAI documents for headless useYou use Claude Code, or want a fallback to a cheaper backend
A paid GitHub Copilot planCopilot itself: Grok 4.7 began rolling out on September 21, 2026 in VS Code, Copilot CLI and other Copilot clientsNot needed for Grok alone

One caution on the first row. kRouter's Grok CLI connector presents itself to xAI as the official CLI, down to the client headers. xAI's announcement offers Grok Build to subscribers, but says nothing about other clients using that endpoint. kRouter warns that its Claude Code, Codex and Copilot subscription connectors can get an account restricted or banned, and the same reasoning applies here. If that risk matters, use an API key or OpenCode Go.

Common questions

Can Claude Code use Grok?

Yes, through a translator. Claude Code only speaks Anthropic Messages. xAI has deprecated its Messages endpoint, OpenCode Go serves Grok only on the Responses API, and Grok Build's endpoint speaks Responses too. kRouter converts between them, so gcli/grok-4.5, ocg/grok-4.7 or xai/grok-4.7 can fill any of Claude Code's model slots.

Is Grok Build the same as the xAI API?

Not as kRouter uses them. Signed in with a subscription, Grok Build draws on that plan through the endpoint xAI's CLI uses, while the xAI API bills the credits on your key. Grok Build can also run on an API key, but kRouter's Grok CLI connector (gcli) uses the subscription sign-in, and the xAI connector (xai) uses the key.

Which Grok model ids should I use?

On xAI's API: grok-4.7, grok-4.6, grok-4.5, grok-4.3 or grok-build-0.1. On OpenCode Go: grok-4.5, grok-4.6 or grok-4.7. Through kRouter's Grok CLI connector: grok-4.5, optionally with -low, -medium or -high. Avoid grok-code-fast-1: xAI retired it in May 2026 and sends those requests to grok-build-0.1.

Why does OpenCode Go reject Grok with ModelProtocolUnsupported?

The request went to the wrong endpoint; the key is not the problem. OpenCode serves Grok only on /responses, and the same mistake can also come back as a 401 ModelError naming format oa-compat. If the request comes through kRouter, update to v0.5.163 or later, which sends the Grok models there on its own.

How much context does Claude Code use with Grok?

Claude Code assumes 200K for any Grok name. Set CLAUDE_CODE_MAX_CONTEXT_TOKENS, or pick 500K in the Claude Code card's Max context dropdown, to match the 500k window xAI lists for Grok 4.5 to 4.7. On xAI credits, prompts of 200k tokens or more cost double.

Kodelyth · The team behind kRouter

Published by Kodelyth, the team that builds kRouter. Posts are drafted with AI assistance and reviewed by a person before they go out. kRouter is free and MIT licensed.

Install kRouter