Docs · Provider

Grok in Claude Code, Codex, Gemini CLI and OpenCode

xAI serves its Grok models on three APIs at once: OpenAI Chat Completions, OpenAI Responses and Anthropic Messages. Add an xAI API key to magpie once, and Claude Code, Codex, Gemini CLI, OpenCode and every other agent you connect can use Grok through magpie’s local gateway at 127.0.0.1:3425, each agent with a model of its own.

Add xAI

  1. Get a keyMake one on console.x.ai.
  2. Add the providerIn magpie’s Providers tab, press + Add provider, pick xAI, paste the key and save. magpie asks xAI which models the key can use.
  3. Pick it for an agentIn the Agents tab, click the model on the agent’s row and choose a Grok model such as grok-4.7.
Terminal
magpie provider add xai xai-…
magpie provider test xai

Model ids in magpie are provider/model, so xAI’s read xai/grok-4.7: the provider, then xAI’s own id. The key is kept in magpie’s settings; magpie doesn’t take it from your shell.

Endpoints

APIBase URLUsed for
Chat Completionshttps://api.x.ai/v1the model list, OpenCode’s requests, and every request magpie translates, Gemini CLI’s among them
Responseshttps://api.x.ai/v1Codex’s requests, and web search that xAI runs itself
Anthropic Messageshttps://api.x.aiClaude Code’s requests

An agent whose API xAI serves is passed on to that API. When magpie has to translate, it uses Chat Completions: it keeps Responses first only for OpenAI’s own models at OpenAI, and Messages first only for Claude models, and Grok is neither.

Model list

magpie fetches the list from xAI, at https://api.x.ai/v1/models, so agents see the models your key can use. Names, reasoning levels and prices come from models.dev’s xai catalog. Pick the models agents see in the provider’s model list, or from the terminal:

Terminal
magpie provider models xai +grok-4.7
magpie provider refresh-every xai 1d

In each agent

Terminal
magpie claude xai/grok-4.7
magpie codex xai/grok-4.7
magpie gemini model xai/grok-4.7
magpie opencode xai/grok-4.7

Grok Build, xAI’s own CLI, can take magpie’s models too: magpie adds them to ~/.grok/config.toml, so they join its /model picker.

What magpie fixes for xAI

xAI’s API turns away a few things that agents send. magpie changes them on the way, on requests it passes on and on those it translates alike, so the agent never sees these errors:

Reasoning

Cache, cost and usage

Several keys and failover

One xAI provider can hold several keys: press Add another key under it, or paste several at once. Requests go to the first key that’s on, and when it runs out or is rate limited the next one takes over.

Terminal
magpie provider spend-cap xai <key> 20 month
magpie provider key-balance xai <key> 25
magpie provider fallback xai deepseek/deepseek-v4-pro

Troubleshooting

FAQ

How do I use Grok in Claude Code?

Add your xAI API key in magpie (Providers, Add provider, xAI), then pick a Grok model such as grok-4.7 on Claude Code’s row, or run magpie claude xai/grok-4.7. Claude Code’s requests go through magpie’s gateway to xAI’s Anthropic Messages endpoint, https://api.x.ai.

Can Codex use Grok 4?

Yes. xAI serves the Responses API that Codex speaks, so magpie codex xai/grok-4.7 sends Codex’s requests to https://api.x.ai/v1 as they are, with what xAI would refuse fixed on the way: tool schemas, a tool choice with no tools, and whole numbers Grok writes as 2500.0.

Can Gemini CLI and OpenCode use Grok?

Yes. Gemini CLI speaks Google’s API, so magpie translates its requests to xAI’s Chat Completions and the replies back. OpenCode speaks Chat Completions, so its requests go to xAI as they are. Run magpie gemini model xai/grok-4.7 or magpie opencode xai/grok-4.7.

Does web search work with Grok?

Yes. Codex’s web search is run by xAI itself on its Responses API. For Claude Code, Gemini CLI and OpenCode, magpie gives the model its own search tool, answered by the searcher chosen in Settings, or by one magpie picks.

Why did Codex fail after switching from GPT to Grok mid-conversation?

xAI can’t read reasoning that another vendor sealed, and answers “Could not decrypt the provided encrypted_content”. magpie asks again without those items and leaves them out for the rest of the conversation.

Does magpie show my xAI balance?

No. The Usage tab counts tokens, cache and cost for every Grok call, priced from models.dev, but magpie reads no balance from xAI. You can type the balance in with magpie provider key-balance, and the Usage page shows it less what magpie has priced since.

By provider: DeepSeek · Kimi and Kimi Code · GLM Coding Plan · Qwen · MiniMax · Gemini · Mistral · OpenRouter · GitHub Copilot · Ollama

Every agent: Models in Claude Code, Models in Codex, Models in Gemini CLI, Models in OpenCode. Ask on Discord if you get stuck.