Skip to content

feat(models): add Grok 4.6 and Grok Imagine 2.0 - #3573

Merged
steebchen merged 1 commit into
mainfrom
add-grok-4.6-and-imagine-image
Aug 12, 2026
Merged

steebchen merged 1 commit into
mainfrom
add-grok-4.6-and-imagine-image

Conversation

@steebchen

@steebchen steebchen commented Aug 12, 2026 •

Copy link
Copy Markdown
Member

Adds xai/grok-4-6 and xai/grok-imagine-image-2-0 to the catalogue.

Every price here was checked against the cost xAI reports back in usage.cost_in_usd_ticks, and that check turned up three billing bugs in the existing xAI family — see below.

The two new models

grok-4-6 — 500K context, vision, tools, JSON output, reasoning. Verified live: minimal/low/medium/high/xhigh are accepted, none and max are rejected, and stop/frequency_penalty/presence_penalty all 400 with "does not support parameter", so they stay undeclared. Priced $2/M in, $0.50/M cached, $6/M out, doubling above the 200K long-context threshold.

grok-imagine-image-2-0 — text-to-image and reference-image editing. Its flat per-image rate varies with both quality (low/medium) and resolution (1k/2k), so perImagePrice keys on "<quality>/<resolution>":

1k 2k
low $0.04 $0.06
medium $0.06 $0.08

The gateway did not forward either knob before, so it could only ever reach xAI's medium/1k default. It now maps image_config.image_quality → quality and image_config.image_size → resolution, normalising pixel dimensions onto the tier names (xAI 422s on 1K, 1024x1024 and anything outside the enum) and dropping the unified auto quality rather than forwarding a value xAI rejects. Billing resolves the tier through the same mappers, so a dropped knob bills at the tier xAI actually served.

The playground exposes this: getModelImageConfig now offers 1K/2K and a Low/Medium quality selector for this model, defaulting to medium so the playground and a bare API call produce the same image and price. Both playground surfaces now read that config instead of each re-deriving it from substring checks, and the chat surface forwards the quality choice it was already collecting.

Billing bugs this surfaced

Reasoning tokens were never billed for any xAI model. xAI is the exception among OpenAI-compatible providers: its completion_tokens excludes reasoning (total_tokens = prompt + completion + reasoning), and the count appears only under completion_tokens_details.reasoning_tokens. Neither the streaming nor the non-streaming parser read that nested field — both look for a top-level usage.reasoning_tokens — so every reasoning request was billed for its handful of visible output tokens and nothing else. One measured request billed $0.00014 against xAI's $0.00097. The fix is scoped to xai precisely because every other provider folds reasoning into completion_tokens, where reading it again would bill the same tokens twice.

Two stale cached-input prices. grok-4-5 charged $0.50/M instead of $0.30/M and was missing its over-200K long-context tier; grok-4-3 charged $0.3125/M instead of $0.20/M (its Bedrock and Azure mappings already had the right figure).

After these fixes the gateway's computed cost matches xAI's own to the cent for grok-4-6, grok-4-5, grok-4-3, grok-build-0-1 and all five image tiers.

Not fixed here

An audit of every xAI mapping against the provider's /v1/models prices found one more discrepancy, left alone because it is a headline repricing of shipped models rather than a correctness fix, and worth deciding separately: grok-4-20-beta-0309-reasoning and -non-reasoning are priced $2/$6 per M while xAI now charges $1.25/$2.50 — i.e. we overbill them. (grok-code-fast-1 is also stale, but it is already deactivated, so no live traffic is affected.)

Verification

  • Scoped e2e for both models: 106 tests pass, including streaming, all five reasoning efforts, tool calls, streaming tool calls, JSON, JSON streaming, image output and Responses-API tool calls.
  • Live billing check against a locally running gateway: all four quality/resolution combinations plus the no-knobs default bill exactly what xAI charged.
  • New unit tests cover the tier grid, the request-body mapping, the nested reasoning-token extraction on both parsers, and the playground config.
  • responses multi-turn fails for the image model, as it does today for the existing grok-imagine-image — the test asks the model to reply with text, which an image-output model cannot do. It never runs by default; both mappings are test: "skip" and only ran because TEST_MODELS overrides that.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added support for xAI’s Grok 4.6 reasoning model and Grok Imagine Image 2.0.
    • Added image quality and resolution options for supported xAI image models, including 1K and 2K output.
    • Added tiered pricing for newer xAI text and image models.
  • Bug Fixes

    • Improved xAI token usage tracking, including reasoning and cached tokens.
    • Image requests now consistently forward supported quality and resolution settings.
    • Unsupported or invalid image options are safely omitted.

Adds xai/grok-4-6 (500K context, vision, tools, reasoning at
minimal..xhigh) and xai/grok-imagine-image-2-0, whose flat per-image
rate varies with both quality (low/medium) and resolution (1k/2k).

The gateway now forwards those two knobs to xAI and bills on the
resulting tier. Every price was verified against the cost xAI reports
in usage.cost_in_usd_ticks, which also surfaced three billing bugs in
the existing xAI family:

- Reasoning tokens were never billed. xAI is the exception among
  OpenAI-compatible providers in excluding reasoning from
  completion_tokens, and it reports the count only under
  completion_tokens_details, which neither the streaming nor the
  non-streaming parser read. Every reasoning request was billed for
  its handful of visible output tokens alone.
- grok-4-5 charged $0.50/M for cached input instead of $0.30/M and was
  missing its over-200K long-context tier.
- grok-4-3 charged $0.3125/M for cached input instead of $0.20/M.

Gateway cost now matches xAI's own figure exactly for grok-4-6,
grok-4-5, grok-4-3, grok-build-0-1 and all five image tiers.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings August 12, 2026 17:39
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@steebchen
steebchen enabled auto-merge (squash) August 12, 2026 17:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The PR adds xAI model metadata, Grok Imagine Image 2.0 controls and request mapping, nested reasoning-token extraction, and xAI image and reasoning pricing updates. Playground requests now forward supported image quality settings.

Changes

xAI provider support

Layer / File(s) Summary
xAI model and image contracts
packages/models/src/models/xai.ts, apps/playground/src/lib/image-gen.ts, apps/playground/src/lib/image-gen.spec.ts
Adds Grok Imagine Image 2.0 and Grok 4.6 metadata. Updates xAI pricing and shared image capability settings.
Playground image controls and forwarding
apps/playground/src/components/playground/chat-ui.tsx, apps/playground/src/components/playground/chat-page-client.tsx, apps/playground/src/components/playground/image-page-client.tsx
Uses shared model configuration for image sizes and quality controls. Forwards supported quality settings in primary, comparison, and image-page requests.
xAI image request normalization
packages/actions/src/prepare-request-body.ts, packages/actions/src/prepare-request-body.spec.ts
Normalizes xAI image resolution and quality values. Omits unsupported values and removes unified auto quality.
Usage parsing and xAI billing
apps/gateway/src/chat/tools/extract-token-usage.ts, apps/gateway/src/chat/tools/parse-provider-response.ts, apps/gateway/src/lib/costs.ts, apps/gateway/src/**/extract-token-usage.spec.ts, apps/gateway/src/**/parse-provider-response.spec.ts, apps/gateway/src/lib/costs.spec.ts
Extracts xAI nested reasoning and cached tokens. Applies xAI image tiers and reasoning-token billing. Tests cover parsing and pricing behavior.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Playground as Playground UI
  participant ImageConfig as getModelImageConfig
  participant RequestBody as prepareRequestBody
  participant XAI as xAI image API
  Playground->>ImageConfig: read model quality and size support
  Playground->>RequestBody: submit image configuration
  RequestBody->>XAI: forward normalized quality and resolution
Loading
sequenceDiagram
  participant XAI as xAI response
  participant Parser as parseProviderResponse
  participant Usage as extractTokenUsage
  participant Costs as costs calculation
  XAI->>Parser: provide completion usage details
  Parser->>Usage: extract reasoning and cached tokens
  Usage->>Costs: calculate xAI charge
Loading

Possibly related PRs

Suggested reviewers: smakosh, ratchaw

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely identifies the two primary models added by the pull request.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch add-grok-4.6-and-imagine-image

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
apps/playground/src/components/playground/image-page-client.tsx (1)

530-556: 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win

Filter image_quality per comparison model. The gateway filters quality values by provider, not by model. A comparison with grok-imagine-image-2-0 first and grok-imagine-image second forwards medium to the older model, which does not support quality and can fail the request.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/playground/src/components/playground/image-page-client.tsx` around lines
530 - 556, Update the imageConfigBody construction in the image-generation flow
to determine image_quality support for every selected comparison model, rather
than relying only on primaryModel’s config. Include image_quality only for
models whose individual getModelImageConfig result supports quality, so
unsupported models such as grok-imagine-image do not receive the field while
supported models retain it.
🧹 Nitpick comments (1)
packages/actions/src/prepare-request-body.ts (1)

1473-1487: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Replace the xAI any request path with an explicit type.

xaiImageRequest: any disables validation for fields sent to xAI. The test helper then bypasses the same contract with as any. Define an XaiImageRequest type with literal quality and resolution unions. Make the mappers return those unions. Remove the test cast and use toHaveProperty for optional fields.

  • packages/actions/src/prepare-request-body.ts#L1473-L1487: Type xaiImageRequest as XaiImageRequest.
  • packages/actions/src/prepare-request-body.spec.ts#L556-L588: Remove as any from prepareXaiImageRequest.

As per coding guidelines, **/*.{ts,tsx} must never use any or as any unless absolutely necessary.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/actions/src/prepare-request-body.ts` around lines 1473 - 1487,
Replace the xAI request’s any-typed path by defining and using an
XaiImageRequest type with literal quality and resolution unions, and update the
mapXaiImageQuality and mapXaiImageResolution return types accordingly in
packages/actions/src/prepare-request-body.ts#L1473-L1487. In
packages/actions/src/prepare-request-body.spec.ts#L556-L588, remove the as any
cast from prepareXaiImageRequest and assert optional fields with toHaveProperty;
both sites require direct changes.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@apps/playground/src/components/playground/image-page-client.tsx`:
- Around line 530-556: Update the imageConfigBody construction in the
image-generation flow to determine image_quality support for every selected
comparison model, rather than relying only on primaryModel’s config. Include
image_quality only for models whose individual getModelImageConfig result
supports quality, so unsupported models such as grok-imagine-image do not
receive the field while supported models retain it.

---

Nitpick comments:
In `@packages/actions/src/prepare-request-body.ts`:
- Around line 1473-1487: Replace the xAI request’s any-typed path by defining
and using an XaiImageRequest type with literal quality and resolution unions,
and update the mapXaiImageQuality and mapXaiImageResolution return types
accordingly in packages/actions/src/prepare-request-body.ts#L1473-L1487. In
packages/actions/src/prepare-request-body.spec.ts#L556-L588, remove the as any
cast from prepareXaiImageRequest and assert optional fields with toHaveProperty;
both sites require direct changes.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b5f9bd8e-1570-42c5-b33a-caf7827139a3

📥 Commits

Reviewing files that changed from the base of the PR and between 05496ce and 50249d1.

📒 Files selected for processing (14)
  • apps/gateway/src/chat/tools/extract-token-usage.spec.ts
  • apps/gateway/src/chat/tools/extract-token-usage.ts
  • apps/gateway/src/chat/tools/parse-provider-response.spec.ts
  • apps/gateway/src/chat/tools/parse-provider-response.ts
  • apps/gateway/src/lib/costs.spec.ts
  • apps/gateway/src/lib/costs.ts
  • apps/playground/src/components/playground/chat-page-client.tsx
  • apps/playground/src/components/playground/chat-ui.tsx
  • apps/playground/src/components/playground/image-page-client.tsx
  • apps/playground/src/lib/image-gen.spec.ts
  • apps/playground/src/lib/image-gen.ts
  • packages/actions/src/prepare-request-body.spec.ts
  • packages/actions/src/prepare-request-body.ts
  • packages/models/src/models/xai.ts

@steebchen
steebchen merged commit 0c2bf77 into main Aug 12, 2026
12 checks passed
@steebchen
steebchen deleted the add-grok-4.6-and-imagine-image branch August 12, 2026 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants