Controlling Framing, Quality, and Cost in xAI’s Image Generation API
Building reliable visual pipelines with xAI’s image generation API (POST /v1/images/generations) comes down to three levers: framing (aspect_ratio), render fidelity (quality and resolution), and execution flow (synchronous, batch, or deferred). Targeting grok-imagine-image-2.0 gives developers explicit control over how scenes are composed and how compute and billing behave under real production loads.
Framing with aspect_ratio
The aspect_ratio parameter sets the output image’s proportions. Omitting it—or passing "auto"—lets the model choose a ratio suited to the prompt. Pinning a ratio keeps layout behavior predictable when images feed directly into fixed UI containers, story cards, or slide decks.
| Ratio | Typical Use Cases |
|---|---|
"16:9" / "9:16" |
Widescreen banners, mobile feeds, vertical stories |
"4:3" / "3:4" |
Presentations, structured editorial frames, portraits |
"auto" (default) |
Model-selected framing when layout dimensions are flexible |
In practice, lock aspect_ratio whenever a frontend component or downstream layout expects a strict orientation, and leave it on "auto" during open-ended exploration.
Tuning quality and resolution on grok-imagine-image-2.0
With grok-imagine-image-2.0, quality accepts "low", "medium", or "auto" (the default). Leaving quality on "auto" lets xAI choose per request—currently serving "low" for new image generations and "medium" for image edits—and billing matches the served quality level. Pinning "low" or "medium" removes per-request variance when you want consistent output detail or predictable cost.
Pair quality with resolution ("1k" by default, or "2k"):
- Drafts and high-volume feeds: Use
"low"quality at"1k"resolution for fast iteration, thumbnails, and broad visual exploration. - Detail-sensitive assets: Pin
"medium"quality and"2k"resolution when fine textures, small visual details, or crisp source stills matter.
Batching, Output Formats, and Deferred Jobs
A single request can generate between 1 and 10 images via n (defaulting to 1 on the REST API and OpenAI-compatible SDKs; in the xAI Python SDK, use sample() for one image and sample_batch(n=...) for batches). For independent prompts, firing parallel requests concurrently—such as with AsyncClient and asyncio.gather—avoids serial waiting.
Choose the output format based on how your app stores or displays assets:
"url"(default): Returns temporary hosted URLs. Download or persist assets you plan to keep long-term rather than relying on temporary links."base64"/"b64_json": Returns image data directly for immediate embedding or saving without an extra fetch step.
For larger batches or long-running calls, pass "deferred": true on /v1/images/generations (or /v1/images/edits) to receive a request_id immediately, then poll GET /v1/images/{request_id} or listen for webhook events (image.generation.completed, image.generation.failed, image.edit.completed, image.edit.failed). Deferred requests use "url" responses and retain results for 24 hours.
Example Request
curl -X POST https://api.x.ai/v1/images/generations
-H "Authorization: Bearer $XAI_API_KEY"
-H "Content-Type: application/json"
-d '{
"model": "grok-imagine-image-2.0",
"prompt": "Architectural cross-section of a desert greenhouse at golden hour",
"aspect_ratio": "16:9",
"resolution": "2k",
"quality": "medium",
"response_format": "b64_json",
"n": 1
}'
When integrating into production workflows, also check moderation status (such as response.respect_moderation in the xAI SDK) and structure prompts around the core subject first before layering style and lighting details.
Leave a Reply