# Grok API error codes

Diagnose Grok API errors by HTTP status and retry only rate limits or temporary server failures.

## Error response format

```json
{
  "error": {
    "message": "Invalid model ID",
    "type": "invalid_request_error",
    "code": "invalid_model"
  }
}
```

## Error codes

| Code | Cause | What to do | Retry |
| --- | --- | --- | --- |
| 400 | Invalid request | Fix JSON, fields, and value types. | No |
| 401 | Missing or invalid API key | Check the Authorization header. | No |
| 403 | Access is forbidden | Check account and model access. | No |
| 404 | Wrong endpoint or model ID | Check the base URL, path, and model. | No |
| 405 | Wrong HTTP method | Use the documented method. | No |
| 415 | Wrong content type | Send JSON with Content-Type: application/json. | No |
| 422 | Field validation failed | Fix the request schema. | No |
| 429 | Rate limit exceeded | Reduce concurrency and honor Retry-After. | Yes, with backoff |
| 500 | Temporary server error | Save the request ID and retry a limited number of times. | Yes |
| 502/503/504 | Temporary gateway failure | Retry with increasing delays. | Yes |

## Safe retries

- Do not retry non-retryable 4xx responses until the request is fixed.
- Honor Retry-After when present.
- Cap attempts and use exponential backoff with jitter.
- Use idempotency for operations with side effects.
