Repository navigation
feat(models): add Grok 4.6 and Grok Imagine 2.0 - #3573
Conversation
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>
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
WalkthroughThe 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. ChangesxAI provider support
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
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
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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 winFilter
image_qualityper comparison model. The gateway filters quality values by provider, not by model. A comparison withgrok-imagine-image-2-0first andgrok-imagine-imagesecond forwardsmediumto 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 winReplace the xAI
anyrequest path with an explicit type.
xaiImageRequest: anydisables validation for fields sent to xAI. The test helper then bypasses the same contract withas any. Define anXaiImageRequesttype with literalqualityandresolutionunions. Make the mappers return those unions. Remove the test cast and usetoHavePropertyfor optional fields.
packages/actions/src/prepare-request-body.ts#L1473-L1487: TypexaiImageRequestasXaiImageRequest.packages/actions/src/prepare-request-body.spec.ts#L556-L588: Removeas anyfromprepareXaiImageRequest.As per coding guidelines,
**/*.{ts,tsx}must never useanyoras anyunless 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
📒 Files selected for processing (14)
apps/gateway/src/chat/tools/extract-token-usage.spec.tsapps/gateway/src/chat/tools/extract-token-usage.tsapps/gateway/src/chat/tools/parse-provider-response.spec.tsapps/gateway/src/chat/tools/parse-provider-response.tsapps/gateway/src/lib/costs.spec.tsapps/gateway/src/lib/costs.tsapps/playground/src/components/playground/chat-page-client.tsxapps/playground/src/components/playground/chat-ui.tsxapps/playground/src/components/playground/image-page-client.tsxapps/playground/src/lib/image-gen.spec.tsapps/playground/src/lib/image-gen.tspackages/actions/src/prepare-request-body.spec.tspackages/actions/src/prepare-request-body.tspackages/models/src/models/xai.ts
Adds
xai/grok-4-6andxai/grok-imagine-image-2-0to 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/xhighare accepted,noneandmaxare rejected, andstop/frequency_penalty/presence_penaltyall 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), soperImagePricekeys on"<quality>/<resolution>":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→qualityandimage_config.image_size→resolution, normalising pixel dimensions onto the tier names (xAI 422s on1K,1024x1024and anything outside the enum) and dropping the unifiedautoquality 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:
getModelImageConfignow 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_tokensexcludes reasoning (total_tokens = prompt + completion + reasoning), and the count appears only undercompletion_tokens_details.reasoning_tokens. Neither the streaming nor the non-streaming parser read that nested field — both look for a top-levelusage.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 toxaiprecisely because every other provider folds reasoning intocompletion_tokens, where reading it again would bill the same tokens twice.Two stale cached-input prices.
grok-4-5charged $0.50/M instead of $0.30/M and was missing its over-200K long-context tier;grok-4-3charged $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-1and all five image tiers.Not fixed here
An audit of every xAI mapping against the provider's
/v1/modelsprices 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-reasoningand-non-reasoningare priced $2/$6 per M while xAI now charges $1.25/$2.50 — i.e. we overbill them. (grok-code-fast-1is also stale, but it is already deactivated, so no live traffic is affected.)Verification
responses multi-turnfails for the image model, as it does today for the existinggrok-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 aretest: "skip"and only ran becauseTEST_MODELSoverrides that.🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Bug Fixes