
Run concurrent Imagine image generations
Fire several Imagine stills with different prompts in parallel so unrelated scenes finish in one wait instead of a serial queue. Official Image Generation shows AsyncClient with asyncio.gather for that job, and tip that same-prompt variations should use sample_batch() with n instead. Model id for current Quality Mode parity is grok-imagine-image-2.0. Create a key and load credits at console.x.ai before you multiply spend across concurrent calls.
What you need
An XAI_API_KEY with Console balance, a short list of distinct prompts (different subjects or scenes), and Python with the xAI SDK. If every image shares one prompt and you only want variations, stop here and use Generate image variations with the Imagine API — that path is one request and cheaper than N concurrent identical prompts.
Generate in parallel
- Export the key and keep it out of source control:
export XAI_API_KEY="your_api_key"
- Build one async task per distinct prompt, then gather them:
import asyncio
import os
import xai_sdk
async def generate_concurrently():
client = xai_sdk.AsyncClient(api_key=os.getenv("XAI_API_KEY"))
prompts = [
"A futuristic city skyline at sunset, sharp glass towers",
"A serene Japanese garden in winter, quiet snow on stone",
"An astronaut floating above Earth, clear helmet reflection",
"A medieval castle on a misty mountain, warm window light",
]
tasks = [
client.image.sample(
prompt=prompt,
model="grok-imagine-image-2.0",
)
for prompt in prompts
]
results = await asyncio.gather(*tasks)
for prompt, result in zip(prompts, results):
print(f"{prompt}: {result.url}")
asyncio.run(generate_concurrently())
- Download or persist each returned URL promptly — hosted image URLs are temporary.
- Pin
aspect_ratioorresolutionon eachsample()call when placements differ — Set aspect ratio on Imagine API images and Set resolution on Imagine API images. - Prefer base64 only when every concurrent worker will write bytes locally — Generate Imagine API images as base64.
Other languages can fan out concurrent HTTP POST calls to https://api.x.ai/v1/images/generations with the same JSON body shape; keep one prompt per request when the prompts differ.
After the batch
Review each still against its own prompt before you chain edits. When one winner needs a region fix, send that URL into Edit an image with the Imagine API. When you later need four looks of the same scene, switch to n / sample_batch so you do not pay for four full concurrent round trips with identical text.
Pitfalls
Using concurrent sample() calls for the same prompt wastes quota that n would cover in one request. Firing dozens of parallel jobs without a Console balance check fails mid-batch and leaves a partial set. Ignoring failed gather exceptions hides a single moderated or errored prompt among successes. Pasting the API key into a ticket or screenshot is a revoke-and-rotate event. Mixing Console API credits with SuperGrok’s weekly Imagine pool on grok.com mixes two billing surfaces the docs keep separate.