Grok 4.5 API Complete Guide: Token Efficiency, Coding Agent Routing, and OpenAI-Compatible Integration
Everything you need to start building with the Grok 4.5 API — API key, first call in 3 minutes, and the token-efficiency numbers that make it a serious routing option. We cover all Grok 4.x models, pricing tiers, tool calling, streaming, coding-agent integration, common errors, and how to route Grok 4.5 through TheRouter.
Grok 4.5 is xAI's frontier reasoning model, launched on July 8, 2026. The model ID is grok-4.5, it accepts text and image inputs, supports a 500,000-token context window, and is priced at $2 per million input tokens and $6 per million output tokens ($0.50/M cached). What sets it apart from other frontier models is not benchmark supremacy — Claude Fable 5 leads all four coding evals xAI published — but token efficiency: Grok 4.5 resolves tasks with 4.2× fewer output tokens than Claude Opus 4.8 (max) on SWE-Bench Pro, which translates to a dramatic cost-per-task advantage for high-volume agent workflows.
We wrote this guide because xAI is a verified provider in our routing layer, and Grok 4.5 changes the cost calculus for coding-agent routing in ways that matter if you're running agent loops at scale. This is the single getting-started-to-production reference we wanted when we integrated the model.
OpenAI-compatible means a provider exposes a chat-completions endpoint whose request and response shape matches the OpenAI API contract closely enough that an unmodified OpenAI SDK call works against it after swapping three values: API key, base URL, and model name. The minimum surface in practice is POST /v1/chat/completions with messages, model, and an OpenAI-shaped streaming response.
Getting Started in 3 Minutes
Step 1: Create an account. Go to console.x.ai and sign up. xAI supports email and Google OAuth.
Step 2: Get your API key. Navigate to API Keys in your console and click "Create API Key." Copy it immediately — keys are shown once.
Step 3: Make your first call. Install the OpenAI Python SDK:
pip install --upgrade openai
Then run:
from openai import OpenAI
client = OpenAI(
api_key="YOUR_XAI_KEY",
base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
model="grok-4.5",
messages=[{"role": "user", "content": "Explain token efficiency in LLM APIs."}],
)
print(response.choices[0].message.content)
That's it. The xAI API implements the OpenAI Chat Completions specification, so any code that works with OpenAI works here with a base_url change.
All Grok 4.x Models: IDs, Capabilities, and Speed Tiers
xAI currently offers several models in the Grok 4 family. Here's the complete lineup:
| Model | Model ID | Context | Reasoning | Speed | Best For |
|---|---|---|---|---|---|
| Grok 4.5 | grok-4.5 | 500K | Configurable (low/med/high) | 80 TPS | Frontier coding, agentic tasks, knowledge work |
| Grok 4 | grok-4 | 256K | Yes | Standard | Complex reasoning, multi-step problem solving |
| Grok 4 Fast | grok-4-fast | 2M | Default non-reasoning | Fast | High-throughput chat, quick completions |
| Grok 4 Fast Reasoning | grok-4-fast-reasoning | 2M | Yes | Fast | Fast reasoning with large context |
| Grok 4.1 Fast | grok-4-1-fast-non-reasoning | 2M | No | Fastest | Speed-critical, non-reasoning workloads |
| Grok 4.1 Fast Reasoning | grok-4-1-fast-reasoning | 2M | Yes | Fast | Fast reasoning with latest improvements |
Grok 4.5 also responds to the aliases grok-4.5-latest and grok-build-latest. The grok-build-latest alias reflects the model's role as the backbone of xAI's Grok Build CLI coding agent.
Source: xAI Models documentation, retrieved 2026-07-19.
Token Efficiency Deep-Dive: The 4.2× Fewer Tokens Claim
This is the number that matters for routing economics. On SWE-Bench Pro, xAI reports that Grok 4.5 resolves tasks with an average of 15,954 output tokens, compared to 67,020 for Claude Opus 4.8 (max) — a 4.2× difference.
What does this mean in practice?
- Cost per resolved task: At list prices, Grok 4.5's output cost per resolved SWE-Bench Pro task is roughly $0.10 (15,954 × $6/M), versus approximately $1.68 for Opus 4.8 (67,020 × $25/M). That's a ~17× cost difference per task when you combine both the lower per-token price and fewer tokens.
- Wall-clock time: At 80 tokens per second, 15,954 tokens takes roughly 200 seconds. At comparable throughput, 67,020 tokens would take over 800 seconds. Agent loops that iterate multiple times compound this difference.
- Agent loop budgets: If your agent runs 10 iterations to solve a problem, the output token savings compound. A pipeline that used to cost $16.80 in Opus output per task could drop to $1.00 with Grok 4.5.
Two important caveats. First, this is a vendor-measured comparison on a single benchmark. Second, Opus's longer outputs include extended reasoning, which is part of how it achieves higher accuracy on some evals. Efficiency and depth represent a genuine trade-off, not a free lunch.
Source: xAI Grok 4.5 announcement, retrieved 2026-07-19; analysis by Apidog, retrieved 2026-07-19.
Pricing Comparison: Grok 4.5 vs Claude Opus 4.8 vs GPT-5.6 Sol
For routing decisions, per-token pricing tells only half the story — you need cost per finished task. Here's how the frontier models compare:
| Model | Input ($/M) | Output ($/M) | Cached Input ($/M) | Context Window | Avg Output Tokens/Task (SWE-Bench Pro) |
|---|---|---|---|---|---|
| Grok 4.5 | $2.00 | $6.00 | $0.50 | 500K | ~15,954 |
| Claude Opus 4.8 | $15.00 | $75.00 | $3.75 | 200K | ~67,020 |
| GPT-5.6 Sol | $2.00 | $8.00 | $0.50 | 1M | Not published |
| Claude Sonnet 5 | $3.00 | $15.00 | $0.30 | 200K | Not published |
| DeepSeek V4-Pro | $2.19 | $8.76 | $0.55 | 1M | Not published |
Grok 4.5's pricing is in the same band as GPT-5.6 Sol and DeepSeek V4-Pro for per-token rates. The differentiation is in the token-efficiency claim: if Grok 4.5 consistently produces shorter outputs that still resolve tasks correctly, the effective cost per task drops further than the per-token rate suggests.
Higher-context pricing note: xAI applies different pricing for requests that exceed 200K tokens in the context window. Budget accordingly for long-context workloads.
Sources: xAI Pricing, retrieved 2026-07-19; Anthropic Pricing, retrieved 2026-07-19; OpenAI Pricing, retrieved 2026-07-19; DeepSeek Pricing, retrieved 2026-07-19.
Coding Benchmarks: Where Grok 4.5 Actually Stands
Grok 4.5 is not the top scorer on coding benchmarks — and xAI's own charts show this transparently. Here are the vendor-reported standings:
| Benchmark | Fable 5 (max) | GPT-5.5 (xhigh) | Grok 4.5 | Opus 4.8 (max) |
|---|---|---|---|---|
| DeepSWE 1.0 | 66.1% | 64.3% | 62.0% | 55.8% |
| DeepSWE 1.1 | 70% | 67% | 53% | 59% |
| Terminal Bench 2.1 | 84.3% | 83.4% | 83.3% | 78.9% |
| SWE-Bench Pro | 80.4% | 58.6% | 64.7% | 69.2% |
Against Opus 4.8, it's a genuine split: Grok wins DeepSWE 1.0 and Terminal Bench 2.1, Opus wins DeepSWE 1.1 and SWE-Bench Pro. Against the frontier (Fable 5), there's no contest — Fable 5 leads all four. xAI's pitch is price-performance, not supremacy, and they printed the frontier numbers rather than cropping them out.
All benchmark data is vendor-reported. Independent evaluations from Artificial Analysis and LMArena were not yet published for Grok 4.5 at the time of writing.
Source: xAI Grok 4.5 announcement, retrieved 2026-07-19; benchmark analysis by Apidog, retrieved 2026-07-19.
Coding Agent Integration: Tool Calling, Streaming, and Context
Grok 4.5 supports the features coding agents need:
Tool Calling (Function Calling)
from openai import OpenAI
client = OpenAI(api_key="YOUR_XAI_KEY", base_url="https://api.x.ai/v1")
tools = [
{
"type": "function",
"function": {
"name": "search_codebase",
"description": "Search the project codebase for relevant files",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"}
},
"required": ["query"],
},
},
}
]
response = client.chat.completions.create(
model="grok-4.5",
messages=[{"role": "user", "content": "Find all files related to authentication"}],
tools=tools,
tool_choice="auto",
)
print(response.choices[0].message.tool_calls)
Grok 4.5 supports native parallel tool calling — it can request multiple function calls in a single response, which reduces round-trips in agent loops.
Streaming
stream = client.chat.completions.create(
model="grok-4.5",
messages=[{"role": "user", "content": "Refactor this function for better error handling"}],
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
Streaming is served at 80 tokens per second. For agent workflows where you need to process output incrementally (e.g., applying code edits as they stream in), this throughput is competitive with fast-tier models from other providers.
Reasoning Effort Control
response = client.chat.completions.create(
model="grok-4.5",
messages=[{"role": "user", "content": "Debug this race condition"}],
extra_body={"reasoning_effort": "high"}, # low, medium, or high
)
High reasoning is the default for Grok 4.5. Use low for routine tasks like code formatting or simple extraction, medium for standard coding tasks, and high (default) for complex debugging, multi-step planning, and mathematical reasoning. Lower reasoning settings reduce latency and output token usage.
Structured Outputs
response = client.chat.completions.create(
model="grok-4.5",
messages=[{"role": "user", "content": "Analyze this code for security issues"}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "security_analysis",
"schema": {
"type": "object",
"properties": {
"issues": {
"type": "array",
"items": {
"type": "object",
"properties": {
"severity": {"type": "string"},
"description": {"type": "string"},
"line": {"type": "integer"},
},
},
}
},
},
},
},
)
Common Errors and Fixes
| Error | Cause | Fix |
|---|---|---|
401 Unauthorized | Invalid or expired API key | Regenerate key at console.x.ai |
429 Too Many Requests | Exceeded 150 req/s or 50M TPM limit | Add exponential backoff; request limit increase |
400 Invalid model | Typo in model ID | Use grok-4.5, not grok-4-5 or grok4.5 |
413 Request Too Large | Context exceeds 500K tokens | Trim input or use a chunking strategy |
| Long-context pricing surprise | Requests >200K tokens billed at higher rate | Monitor input token counts; keep under 200K when possible |
| Streaming SSE parsing errors | Client not handling [DONE] sentinel | Use official OpenAI SDK which handles this automatically |
| Tool call format mismatch | Expecting Anthropic-style tool use | xAI uses OpenAI-format tool_calls, not tool_use blocks |
Rate limits: The default published rate limit is 150 requests per second and 50 million tokens per minute. Account-specific limits may differ — check your console for your actual allocation.
Source: xAI Models documentation, retrieved 2026-07-19.
Production Checklist: When to Route to Grok 4.5
Use Grok 4.5 when:
- Cost per task matters more than peak accuracy. If you're running hundreds or thousands of agent tasks per day, the 4.2× token efficiency translates to significant savings even before the lower per-token price.
- You need fast iteration in agent loops. At 80 TPS with shorter outputs, Grok 4.5 completes agent turns faster than models that emit 4× more tokens.
- Your workload is coding + knowledge work. Grok 4.5 was explicitly trained for both — it's not just a coding model that happens to do knowledge work.
- You want 500K context for repository-level analysis. Many coding agents need to see large portions of a codebase; 500K tokens accommodates that.
Consider alternatives when:
- Peak benchmark accuracy is the requirement. Claude Fable 5 (max) leads all four published coding evals. For tasks where a few percentage points of accuracy matter more than cost, Fable 5 or Opus 4.8 may be the better choice.
- You need >500K context. Grok 4 Fast and Grok 4.1 Fast offer 2M token context windows, and DeepSeek V4 and GPT-5.6 Sol also support 1M+.
- EU availability is required. Grok 4.5 had limited EU availability at launch — verify current status before deploying for EU users.
TheRouter Integration: Routing Grok 4.5 as Primary or Fallback
xAI is a verified provider in TheRouter's routing layer. The existing xAI provider page covers general integration; here's what's specific to Grok 4.5.
Grok 4.5 as a cost-efficient primary: For coding-agent workloads where you're currently routing to Opus 4.8, switching the primary to xai/grok-4.5 and keeping Opus as a fallback can reduce output token costs by an order of magnitude — with a modest accuracy trade-off on the harder repo-level benchmarks.
Grok 4.5 as a fallback: If Fable 5 or Opus 4.8 is your primary for maximum accuracy, routing to Grok 4.5 on rate-limit errors or provider outages keeps your pipeline running at much lower cost than falling back to another frontier-priced model.
Model fallback configuration: TheRouter supports model fallback routing that automatically switches to a backup provider when the primary returns errors or exceeds latency thresholds. This is where token efficiency really compounds — if your fallback model costs 17× less per task, occasional fallback routing becomes a cost optimization rather than a cost penalty.
Note that xai/grok-4.5 is not yet in TheRouter's models-data.ts at the time of writing — the xAI provider is verified, but the specific Grok 4.5 model ID may need to be added. Check the live models page for current status.
See also: xAI Grok API Integration Guide for general xAI setup, Grok 4 vs Claude Opus 4 Comparison for the prior generation, Coding Agent Model Routing Comparison 2026 for the broader landscape.
What's Next for Grok 4.5
As of July 19, 2026 — eleven days after launch — independent benchmarks from Artificial Analysis and LMArena have not yet been published. The vendor-reported numbers suggest a strong mid-tier coding model with exceptional token efficiency, but independent evaluation will determine where Grok 4.5 actually lands in the broader landscape.
We'll update this guide as independent benchmarks arrive, EU availability expands, and we accumulate production routing data through TheRouter. For the latest xAI model listings, check the xAI provider page.
All pricing and specifications in this guide are current as of July 19, 2026. xAI may change pricing, rate limits, or model capabilities at any time — always verify against the official xAI documentation before making production decisions.