# imagegen

> The command Agents use to generate images from a prompt, with optional reference images, and save them as local files.

Source: https://air.build/docs/cli/imagegen/

`imagegen` turns a text prompt into one or more image files. You can add reference images (local files or web addresses) for the model to work from. It waits for the images, saves them and prints their paths.

## Where it runs

The same command is available to Agents on a cloud Team and on a Mac Team. Sign-in and Agent attribution come from the Agent's environment, so there is nothing to set up. The command needs `curl`, `jq` and `base64`, which the Agent's environment provides. See [Choose where your Agents run](https://air.build/docs/get-started/where-agents-run/) for the two kinds of Team.

## Synopsis

```text
imagegen [options] PROMPT
imagegen [options] --prompt-file PATH
imagegen --help
```

The prompt is every word that is not an option, joined with spaces, so quoting it is optional. Everything after `--` is read as prompt, even words that start with `-`.

## Options

Every option that takes a value also accepts the `--option=value` form.

| Option | Value | Default | Meaning |
| --- | --- | --- | --- |
| `-i`, `--image` | A path or an `http(s)://` address | None | Adds a reference image. Repeat it for several images. Local files must be PNG, JPEG, WebP or GIF (by extension: `.png`, `.jpg`, `.jpeg`, `.webp`, `.gif`) and not empty. |
| `-n`, `--count` | A whole number from 1 to 10 | `1` | How many images to generate. |
| `-o`, `--output` | A path | A new folder under `$TMPDIR` | With one image: the exact file to write. With several: the folder to write them into. |
| `--model` | A model id, no spaces | The service's default | The image model to use. |
| `--size` | A size or an aspect shorthand, no spaces | `auto` | The output size. Passed to the model as given. |
| `--quality` | `auto`, `low`, `medium`, `high` | `auto` | Output quality. |
| `--format` | `png`, `jpeg`, `webp` | `png` | The file format to ask for. |
| `--background` | `auto`, `transparent`, `opaque` | `auto` | The background. `transparent` needs `png` or `webp`. |
| `--prompt-file` | A path to a UTF-8 text file | None | Reads the prompt from the file. Use either this or a prompt on the line, not both. |
| `-h`, `--help` | — | — | Prints the help. |

## Where the files go

| `--output` | `--count` | Result |
| --- | --- | --- |
| Not given | Any | A new folder `$TMPDIR/imagegen.XXXXXX/` (or under `/tmp` when `TMPDIR` is not set), with files `image-1.png`, `image-2.png`, … |
| A file path | `1` | That exact file. Missing parent folders are created. |
| A folder path | More than `1` | `image-1.<ext>`, `image-2.<ext>`, … inside that folder, which is created if needed. |

The extension follows the format the service returns (`png`, `jpeg`, `webp`, `gif` or `svg`), falling back to `--format`. `imagegen` never overwrites: if a target file already exists, it stops with an error. Without `--output`, nothing is written into the project folder; copy the files you want to keep into the project yourself.

## Output

On success, stdout has one absolute file path per line and nothing else, so it can be piped:

```text
/tmp/imagegen.Ab12Cd/image-1.png
/tmp/imagegen.Ab12Cd/image-2.png
```

Progress goes to stderr: `Generating 2 images with <model>...` before the request and `Generated 2 images.` at the end.

## Exit codes and errors

Every error is printed to stderr and starts with `Error:`. Usage errors add `Usage: imagegen [options] PROMPT` and `Run imagegen --help for details.`

| Exit code | Meaning |
| --- | --- |
| `0` | The images were saved; their paths are on stdout. |
| `1` | The request reached the service and failed, or the result could not be saved. |
| `2` | The command line or the inputs are wrong, or the environment is missing something. Nothing was saved. |

| Message | Exit | What to do |
| --- | --- | --- |
| `a prompt is required.` / `the prompt cannot be empty.` | 2 | Pass a prompt or `--prompt-file`. |
| `pass either PROMPT or --prompt-file, not both.` | 2 | Keep one of them. |
| `prompt file is not readable: PATH` | 2 | Check the path. |
| `--count must be an integer from 1 to 10.` | 2 | Use 1–10. |
| `--quality must be auto, low, medium, or high.` | 2 | Use one of those values. |
| `--format must be png, jpeg, or webp.` | 2 | Use one of those values. |
| `--background must be auto, transparent, or opaque.` | 2 | Use one of those values. |
| `transparent backgrounds require png or webp output.` | 2 | Add `--format png` or `--format webp`. |
| `--model must be a nonempty model id without whitespace.` / `--size must be a nonempty value without whitespace.` | 2 | Fix the value. |
| `reference image is not readable: PATH` / `reference image is empty: PATH` | 2 | Check the file. |
| `unsupported reference image type: PATH` | 2 | Use a `.png`, `.jpg`, `.jpeg`, `.webp` or `.gif` file. |
| `prompt and reference images exceed the 20 MiB request limit.` | 2 | Use fewer or smaller reference images, or pass them as web addresses. |
| `output already exists: PATH` | 2 | Choose another `--output`, or move the existing file. |
| `multi-image output must be a directory: PATH` | 2 | With `--count` above 1, `--output` must be a folder. |
| `unknown option: …` | 2 | Check the spelling against the table above. |
| `the Agent runtime is missing the required '…' command.`, or a message that the Agent runtime did not provide what the command needs | 2 / 1 | The command is being run outside an AirBuild Agent's environment. Run it from an Agent. |
| `could not reach the Airbuild image service.` | 1 | Network problem. Try again. |
| The service's own message, or `the Airbuild image service returned HTTP <status>.` | 1 | Read the message; it says what the service rejected, for example the prompt or a value. |
| `Airbuild returned no generated images.` / `requested N image(s), but Airbuild returned M.` | 1 | Try again, or change the prompt. |
| `could not download generated image N.` / `generated image N is empty.` / `Airbuild returned invalid base64 for image N.` / `Airbuild returned no usable data for image N.` | 1 | Try again. |

## Examples

```bash
# One image, saved to a new folder under $TMPDIR
imagegen "A friendly robot app icon, flat vector style"

# Polish a sketch, saved to an exact file
imagegen --image sketch.png --output final.png "Polish this sketch"

# Three variants into one folder
imagegen --count 3 --output variants "Three calm onboarding illustrations"

# A transparent PNG with a long prompt kept in a file
imagegen --background transparent --format png --prompt-file prompt.txt -o Assets/badge.png
```
