Skip to main content
Use grok-imagine-image-2.0 for new Grok Imagine integrations: generate images from text, or edit with one to three reference images. Billing counts each successfully returned image, so a request that returns several images is billed for each one.

What grok-imagine-image-2.0 supports

Editing accepts OpenAI-compatible file uploads, not the JSON image objects shown in xAI’s documentation. Requests with four or five references fail, so enforce the three-image limit in your client and don’t retry an over-limit request.

Choose a Grok Imagine model

The earlier models have their own prices: $0.025 per image for grok-imagine-image and $0.045 per image for grok-imagine-image-quality. Like 2.0, they are billed per successfully returned image. Existing workloads don’t need to migrate right away. Before switching, compare one image from each model using the same prompt, ratio, and resolution.

Before you call the API

1

Create and protect an API key

Create a key in the default group in Token management and set Billing mode (计费模式) to Usage first (按量优先, recommended) or Per-call (按次计费). A Usage first key can call both per-call and usage-based models; a Per-call key calls only per-call models.Keep the key in a server-side environment variable, never in browser code, a public repository, or logs.
2

Confirm model access and price

In Models and pricing, check that the key can use grok-imagine-image-2.0 and review the current price.
3

Set up your environment

Export the key and install the OpenAI Python SDK, which the Python examples use:
4

Start with one image

Use n=1 first. Save the output, inspect its content and dimensions, and check the call record before you increase resolution, output count, or concurrency.

Generate an image

This request creates one 2K, 16:9 image at medium quality and writes the response to grok-generation.json:
A successful response returns HTTP 200 with one item in data:

Save the result

The URL is temporary, so download the image right away. Save this script as save_images.py. It writes every image in a response to disk, whether the response contains url or b64_json, and picks the file extension from the image data:
save_images.py
The script prints a line such as Saved grok-lighthouse-1.jpg. Open the file and check the subject, text, and composition. To receive the image bytes in the response instead of a URL, set response_format to b64_json; the same script handles both.

Generate with the Python SDK

The OpenAI Python SDK doesn’t expose every Grok image option as a named parameter, so pass aspect_ratio, resolution, and quality in extra_body. This example requests Base64 output and saves the decoded image:

Aspect ratio, resolution, and quality

grok-imagine-image-2.0 accepts these 15 fixed aspect ratios: Omit aspect_ratio to let the model choose, or pass a value when the layout must be predictable.
  • resolution="1k" is faster and suits drafts, thumbnails, and batch candidates.
  • resolution="2k" produces larger files with longer latency, for final assets and cropping.
  • quality="low" favors speed; quality="medium" favors detail.
  • Don’t send quality="high" or resolution="4k".
Responses don’t report which quality tier was used. Set quality to low or medium explicitly rather than relying on auto, so latency and output quality stay predictable.

Edit one reference image

Editing requires a file upload. Put a local product-photo.png in the current directory, then run:
Reference editing regenerates the scene; it isn’t pixel-level mask inpainting. Check the subject, color, text, and composition in the saved image.

Edit with two or three references

Repeat the image[] field for each reference. This request supplies the subject, the scene, and the style as three separate files:
Upload order maps to image 1, image 2, and image 3 in the prompt. A successful request returns one image; with aspect_ratio=3:2, expect a landscape result such as 1248×832. Count the files before you send the request, because a fourth reference makes the whole request fail.

Edit with the Python SDK

The OpenAI SDK works for single-image edits. This example returns a URL and downloads the image immediately:
For multiple references, start with the cURL form above to confirm how your HTTP client serializes repeated multipart fields.

Parameters

Pricing and billing

Billing counts the images a request actually returns: LaoZhang API charges the same price for 1k and 2k and for low and medium. LaoZhang API prices are set separately from xAI list pricing, so don’t compare them directly. Check the console before a large batch, and treat call records as the final billing source.

Error handling

Retry valid requests that hit 429, network errors, or clearly temporary 5xx errors with jittered exponential backoff. After a client timeout, check the call record before you resubmit, so you don’t generate and pay for the same image twice.

Before you go to production

  1. Generate one 1k, low image with the production key.
  2. Save the result and check its real format and dimensions.
  3. Test the exact resolution, quality, aspect ratio, and n values you plan to use.
  4. For edits, use distinct, non-sensitive references and confirm that each one affects the output.
  5. Enforce the three-reference limit in your client.
  6. Set a timeout of at least 60 seconds for 1k low requests and 120–180 seconds for medium or 2k.
  7. Reconcile success rate, latency, output count, and call records before raising concurrency.

Frequently asked questions

What is the exact model ID?

Use grok-imagine-image-2.0, including the final .0. grok-imagine-image and grok-imagine-image-quality are separate, earlier models.

Am I billed per image? Do resolution and quality change the price?

Per successfully returned image. 1k, 2k, low, and medium currently share one per-image price; see Pricing and billing for examples. Latency differs by tier, so plan timeouts accordingly. A request that fails with an error and returns no image isn’t billed per image. A client timeout isn’t the same as a failure, though: the server may have finished and charged for the image, so check the call record before you resend. Call records are the final billing source.

Can I edit with xAI’s JSON format or send four or five references?

No to both. Editing accepts only multipart file uploads, with up to three references, as the table at the top of this page shows. xAI’s JSON image objects return HTTP 400. xAI’s documentation says the OpenAI SDK’s images.edit() doesn’t work for Grok editing; that applies to xAI’s own JSON-only API. On LaoZhang API, images.edit() works for single-image edits, as shown in Edit with the Python SDK. For several references, start with the cURL image[] form.

What aspect ratio does an edit return?

According to xAI, an edit without aspect_ratio follows the first reference image’s aspect ratio. To fix the frame, add aspect_ratio to the edit request; the three-reference example uses aspect_ratio=3:2 and returns a 1248×832 image.

Can I keep editing the previous result?

Yes. Download the previous result, upload it as the image, and describe only this round’s change in the prompt, such as “Keep everything the same and add a gold border.” Each round is a new edit request, billed for the images it returns.

How should I store generated images?

Download URL results promptly into your own storage; xAI also describes the returned URLs as temporary. Use b64_json when the image must arrive inside the API response.