Grok API Error Codes Explained
Every status code api.x.ai returns, what actually causes it, and whether your client should retry it.
TL;DR
- →Retryable: 429, 500, 502, 503, 504. Everything else is your request.
- →401 means the key is rejected; 403 means the key is fine but not entitled. Different fixes.
- →422 is nearly always a context-window overflow — prompt tokens plus
max_tokens. - →Errors spread across every model and key = incident, not bug. Check status.x.ai.
The Full Error Code Table
The xAI API is OpenAI-SDK compatible, so the status codes will look familiar — but the causes behind them differ in places, particularly around entitlements and capacity.
400 Bad RequestDo not retryCause: Malformed JSON body, an unknown parameter, or an invalid parameter combination.
Fix: Log the full response body — xAI returns a message field naming the offending parameter. Validate the payload against the OpenAI-compatible chat completions shape before sending.
401 UnauthorizedDo not retryCause: Missing, malformed, revoked, or mistyped API key in the Authorization header.
Fix: Confirm the header is exactly "Authorization: Bearer xai-...". Strip trailing newlines picked up from .env files or CI secret stores, and re-issue the key in console.x.ai if it was rotated.
403 ForbiddenDo not retryCause: Valid key, but the account is not entitled to the model, endpoint, or feature requested.
Fix: Check model entitlements for your organization. This also appears when billing is not configured or a spend limit has been reached — rotating the key will not help.
404 Not FoundDo not retryCause: Wrong path, or a model ID that does not exist (model_not_found).
Fix: Base URL must be https://api.x.ai/v1. Fetch the live model list rather than hard-coding an ID that may have been superseded.
422 Unprocessable EntityDo not retryCause: Semantically invalid request — usually prompt plus max_tokens exceeding the context window, or an unsupported tool/response_format combination.
Fix: Count tokens before sending and cap max_tokens against the remaining window. Drop unsupported parameters rather than passing an entire OpenAI request through untouched.
429 Too Many RequestsRetryableCause: Account rate limit hit — requests per minute or tokens per minute.
Fix: Exponential backoff with jitter, plus a client-side queue. Read the rate limit response headers to see which limit tripped rather than guessing.
500 Internal Server ErrorRetryableCause: Server-side fault inside xAI. Not caused by your payload.
Fix: Retry with backoff. Sustained 500s across multiple models indicate a real incident — check status.x.ai and fail over.
502 / 504 Gateway ErrorsRetryableCause: Edge or gateway timeout in front of the model servers, often during a long non-streaming completion.
Fix: Switch long generations to streaming so the connection produces bytes early, and raise your client read timeout above your worst-case generation time.
503 Service UnavailableRetryableCause: Capacity exhaustion or maintenance — the model is temporarily unable to accept work.
Fix: Back off, then fail over to a secondary provider if the window exceeds your latency budget. Alert on sustained 503s; they are the clearest early signal of an xAI incident.
Classify Before You Retry
The most expensive mistake in Grok integrations is a blanket retry wrapper. Retrying a 422 three times does not fix the context overflow — it triples your latency and your token spend on a request that was always going to fail. Retrying a 401 will not re-authenticate a revoked key. Split your handler in two: a deterministic branch that surfaces the error to the caller immediately, and a transient branch with exponential backoff plus jitter.
Cap the transient branch. Three attempts over roughly seven seconds is usually the right ceiling for an interactive path; beyond that, fail over rather than keep waiting. See the Grok API failover guide for the routing side of that decision.
Log the Body, Not Just the Code
xAI returns a JSON error object with a human-readable message that usually names the exact offending parameter. Teams that log only the HTTP status spend hours guessing at 400s that the response body would have explained in one line. Capture the status, the message, the model ID and the request ID together — that quadruple is enough to resolve almost every non-incident failure without reproducing it.
Alert Pro
14-day free trialStop checking — get alerted instantly
Next time Grok goes down, you'll know in under 60 seconds — not when your users start complaining.
- Email alerts for Grok + 9 more APIs
- $0 charged today — card required to start
- Cancel anytime — $9/mo after trial
Related Guides
Frequently Asked Questions
What does a 429 error mean on the Grok API?
A 429 means you exceeded an account-level rate limit — either requests per minute or tokens per minute. xAI limits scale with your account tier and usage history rather than a single published free-tier number, so two keys on the same organization can hit 429 at different volumes. Back off exponentially (1s, 2s, 4s) and queue burst traffic client-side instead of firing concurrent retries, which only deepens the limit.
Is a Grok API 401 error the same as a 403?
No. A 401 Unauthorized means the API key itself was rejected — missing Authorization header, a typo, a revoked key, or a key pasted with trailing whitespace. A 403 Forbidden means the key is valid but not entitled to what you asked for: a model your team has not been granted, or an endpoint outside your plan. Rotating the key fixes a 401 and does nothing for a 403.
Which Grok API errors should I retry automatically?
Retry 429, 500, 502, 503 and 504 with exponential backoff and jitter. Never blindly retry 400, 401, 403, 404 or 422 — those are deterministic client-side faults and the identical request will fail identically, burning quota and latency budget. A retry policy that does not distinguish the two classes is the single most common cause of a small xAI incident turning into a full outage for your app.
Why am I getting model_not_found from api.x.ai?
The model string in your request body does not match an ID your account can call. The usual causes are a display name instead of an API ID (sending "Grok 3" rather than grok-3), a model that has since been superseded, or a model your organization has not been enabled for — which surfaces as 403 rather than 404 in some cases. List the models your key can actually reach from the models endpoint rather than hard-coding a string in application code.
How do I tell a Grok API error from an xAI outage?
Look at the spread. Errors concentrated on one endpoint, one model, or one key are almost always your request. A simultaneous jump in 500s and 503s across every model and every key is an incident — confirm against status.x.ai, or watch apistatuscheck.com/api/grok for independent uptime checks and alerts that fire before the official status page is updated.
📡 Monitor your APIs — know when they go down before your users do
Better Stack checks uptime every 30 seconds with instant Slack, email & SMS alerts. Free tier available.
Affiliate link — we may earn a commission at no extra cost to you