Configuring Image Generations with xAI’s Grok Imagine API
Building reliable visual pipelines means balancing framing, detail, and cost before a single asset hits production. With grok-imagine-image-2.0 on xAI’s /v1/images/generations endpoint (and matching SDKs), developers can control aspect ratio, resolution, batch size, and generation quality directly in each request.
Framing with aspect_ratio
The aspect_ratio parameter controls output dimensions across image generation and editing workflows:
- Automatic selection (
auto): Omittingaspect_ratio—or passing"auto"—lets the model choose a ratio suited to the prompt. - Explicit ratios: Pass strings such as
"16:9","1:1", or"9:16"when layout constraints are fixed, such as widescreen headers, square cards, or vertical mobile feeds. - Multi-image edit behavior: When editing with up to five source images, output aspect ratio defaults to the first input image unless overridden with
aspect_ratio.
Controlling Quality and Resolution on grok-imagine-image-2.0
Quality and resolution work together to shape visual fidelity, latency, and spend:
quality(low,medium,auto): Supported ongrok-imagine-image-2.0. When omitted or set toauto, the service selects the quality per request—currently defaulting tolowfor image generation andmediumfor image editing. Pinninglowormediumlocks in a specific setting, and billing reflects the quality served.resolution(1k,2k): Defaults to1kwhen omitted, with2kavailable for higher pixel detail. Pairingmediumquality with2kresolution is recommended when fine details must stay crisp and recognizable.- Flat per-image pricing: Image generation uses flat per-image pricing (listed at
$0.04per image ongrok-imagine-image-2.0) regardless of prompt length, while image edits bill for both input and generated output images.
Batching, Output Formats, and Deferred Requests
Beyond dimensions and quality, the API supports practical production ergonomics:
- Batch counts (
n): Generate between1and10images per request (defaulting to1on REST and OpenAI-compatible clients). In the xAI Python SDK, usesample()for a single image andsample_batch(n=...)for batches. For distinct prompts, running concurrent calls withAsyncClientandasyncio.gatheris faster than sequential requests. - Response format (
urlvs.base64): The defaulturlformat returns temporary hosted URLs, whereas requestingbase64/b64_jsonreturns inline image data for immediate embedding or durable storage. - Deferred execution (
deferred: true): Setdeferred: trueon/v1/images/generationsto receive arequest_idimmediately and pollGET /v1/images/{request_id}. Deferred requests useresponse_format: "url"and retain results for 24 hours.
Example: OpenAI-Compatible Client
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.XAI_API_KEY,
baseURL: "https://api.x.ai/v1",
});
const response = await client.images.generate({
model: "grok-imagine-image-2.0",
prompt: "Editorial architectural study of a glass conservatory in morning mist",
aspect_ratio: "16:9",
resolution: "2k",
quality: "medium",
});
console.log(response.data[0].url);
Practical Workflow Takeaways
Start with auto aspect ratio and default settings during rapid prompt exploration, where matching the subject to the prompt matters most. Once a prompt proves reliable, pin aspect_ratio for UI fit and step up quality to medium and resolution to 2k for assets where small textures, typography, or architectural edges need to hold up under closer inspection.
Leave a Reply