> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apiyi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Natural-Language Image Generation in AI Agents

> Break "let the agent generate an image" into an executable path: intent, locating files, calling the Images API, and returning results — with APIYI GPT-Image Skills, copy-paste prompt templates, and a cost comparison.

## The short version

An agent does not generate images itself; it has to call an image model. Behind a sentence like "blend these two images together" there are usually three stages: **planning, tool calls, and an image API call**. Most of the money is spent on the text side, not on the image itself.

First, the cost scale of three approaches:

| Approach | Text side | Image side | Best for |
| - | - | - | - |
| Agent without a Skill, exploring on its own | Many turns, long context — often the main cost | May never reach the image endpoint | Ad-hoc exploration |
| Agent + GPT-Image Skill | A few text turns | Per image, from \$0.025 | Daily image work, batch iteration |
| AI Image Master (web) | No text-model charges | Per image | Prompt validation, small batches |

## How one sentence unfolds

**1. Receive the request**

You describe the goal in natural language, for example "put the person from a.png into the scene in b.png".

**2. Plan**

The agent decides whether this is text-to-image, an edit, or multi-image fusion, and which inputs it needs.

**3. Call tools**

Find files, read images, and list directories or search code when necessary. **This is the step most likely to run away**: when the agent is unsure where a file is, it scans the project first.

**4. Choose how to call**

With a Skill installed it executes directly; without one it reads docs, writes a script, and tries parameters. Both reading and writing bill as text tokens.

**5. Call the Images API**

Text-to-image goes to `/v1/images/generations`; edits and multi-image fusion go to `/v1/images/edits`.

**6. Return the result**

Save the image and return the path or preview to the conversation; if more adjustment is needed, the next turn begins.

| Stage | Who runs it | What it bills | Common failure |
| - | - | - | - |
| Planning | Text model | Input + output | Full context resent every turn |
| Tool calls | Agent client | Tool results enter the next turn's input | Scanning directories, reading large files |
| Reading docs / writing scripts | Text model | Input + output | Trial and error, possibly never reaching image generation |
| Calling the Images API | Image model | Per image or per image token | Unfamiliar parameters, unsupported sizes |
| Returning | Client | Usually no extra charge | Large-image transfer timeouts |

> [!WARNING]
>
> **Do not use the Responses built-in `image_generation` tool inside Codex.** It adds a fixed tool fee per generation (about \$0.20) and its behavior is not stable across routes. When you need an image, call `/v1/images/*` directly or use the Skill described below.

## A real case: where the money went for one "blend" task

On 2026-11-27 (UTC+8), a user doing image compositing in Codex produced two very different groups of requests in the logs:

| Period (UTC+8) | Requests | Volume | Cost |
| - | - | - | -: |
| 09:16–10:15 (UTC+8) | 47 × `/v1/responses` (`gpt-6.2-astra`) | About 2.2M input; tens to just over a thousand output tokens per turn | About \$3.3 |
| 11-26 03:00–03:06 (UTC+8) | 8 × `/v1/images/edits` (`gpt-image-2.6-sunburst`) | About 27k input, 10k output | About \$0.54 |

Two facts stand out:

1. **No image request at all during that hour.** All 47 were text requests. The agent was planning, reading files, and making small decisions; the task never reached the image endpoint.
2. **The text-side exploration cost several times the image cost.** The eight requests that actually produced images cost about \$0.54; the text side cost about \$3.3 in one hour.

This is not the model "deliberately making the task complicated". It is the agent's default behavior: when it does not know where a file is, it scans; when it does not know how to call the API, it reads docs. The difference is whether someone handed it that knowledge up front.

## Why the agent "scans the whole project"

* **It does not know where the files are**: the first reaction is to list directories and search for filenames instead of asking you
* **The fixed base is large**: the system prompt, tool definitions, `AGENTS.md`, and project context are read again every turn
* **Context grows monotonically**: every tool result is appended to the next turn's input, so later turns cost more
* **A new conversation is not free**: it clears history, but the fixed base still has to be written to cache again

In this case, the agent's input grew from about 30k tokens to about 63k, and the run saw four cache writes from zero in 59 minutes (the initial cold start plus three rebuilds). None of that says anything about whether the model is genuine — it is the normal overhead of an agent loop. For Codex's cost profile, see [OpenAI Codex](https://docs.apiyi.com/en/scenarios/programming/codex-cli.md).

## The cheaper path: turn exploration into a Skill

APIYI's GPT-Image Skills package the "how to call the API" knowledge into a `SKILL.md` plus one script, so the agent does not have to read docs and write scaffolding first:

* Default model `gpt-image-2.6-all` at a flat \$0.025 per image — the cheapest way to iterate
* One script covers 11 models plus 1 alias
* `-i` can be repeated for up to 16 input images, so multi-image fusion works out of the box
* When you need official quality tiers or a precise `mask`, switch with `--model` to `gpt-image-2.6-flare` / `gpt-image-2.6-sunburst`

Installation takes three steps: create the directory, write the `.env`, and hand the directory to the agent. For the full guide and parameter table, see [GPT-Image Series Agent Skill](https://docs.apiyi.com/en/api-capabilities/gpt-image-2/skills.md).

| Path | Text turns | Image calls | Result in this case |
| - | - | - | - |
| No Skill, exploring | Many (47 in this case) | Possibly none | About \$3.3, 0 images |
| With a Skill, following SKILL.md | Few (typically 1–3) | One or more | Not measured in this case; images from \$0.025 each |

> [!NOTE]
>
> A Skill does not make cost zero: the agent's text turns still bill, and images still bill per image or per image token. What it removes is the "read docs, write scaffolding, retry parameters" stretch.

## Prompt templates (copy-paste ready)

**Template 1: precise image work, with files and tools constrained**

```text theme={null}
Only work on the two images I specify: /path/a.png and /path/b.png.
Do not scan directories, do not read other files, do not modify code.
Put the person from a.png into the scene in b.png, keeping b.png's lighting and color.
Call /v1/images/edits directly with gpt-image-2.6-sunburst, and write the result to /path/out.png.
When done, reply only with the output path and the cost. Do not explain the process.
```

**Template 2: forbid project scanning**

```text theme={null}
Do not run ls, find, grep, or any directory scan.
Use only the file paths and parameters I give you in the message.
If a path is missing, ask me — do not go looking for it.
```

**Template 3: cap turns and output**

```text theme={null}
This task allows at most 5 tool calls: read only the 2 files I name and call the image API once.
If you exceed that, stop and ask me; do not keep scanning.
Execute directly. Do not restate the request, do not compare options, do not print intermediate reasoning.
```

**Template 4: force the Skill path**

```text theme={null}
Use APIYI's gpt-image-2 Skill with the default gpt-image-2.6-all, one image is enough.
Do not look up the API docs yourself, do not scaffold a new project; follow SKILL.md and execute.
```

> [!TIP]
>
> Put these constraints in the project's `AGENTS.md` rather than repeating them every time. But `AGENTS.md` is read on every turn, so keep it to a few dozen lines.

## When not to use an agent

If image generation is a fixed pipeline, a batch job, or you just want to validate a prompt, leave the agent out:

* **Prompt validation / model comparison**: use [AI Image Master](https://imagen.apiyi.com) (`imagen.apiyi.com`). It uses the same APIYI key and the same image endpoints, with only image charges and no text-model charges.
* **Batch and scheduled jobs**: call `/v1/images/*` from a script. Costs are predictable, retries are explicit, and the bill is easy to reconcile.
* **Production pipelines**: scripts + queues + monitoring are more controllable than an agent.

> [!NOTE]
>
> AI Image Master is a web test bench, not a cheaper channel. Images still bill at the price of the model you select; see [Model Pricing](https://docs.apiyi.com/en/models/index.md).

## Cost comparison

| Model | Billing | Price |
| - | - | - |
| `gpt-5.6-luna` | Per token | \$0.2 / \$1.2 per 1M (input / output) |
| `gpt-6.2-sol` | Per token | \$2 / \$10 per 1M |
| `gpt-6.2-astra` | Per token | \$8 / \$40 per 1M |
| `gpt-image-2.6-all` | Per call | \$0.025 / image |
| `gpt-image-2.6-flare` | Per token | About \$0.006–0.21 / image (1K tier) |
| `gpt-image-2.6-sunburst` | Per token | About \$0.006–0.21 / image (1K tier) |

> [!NOTE]
>
> Model prices match the official rates and may change with them; the table above is for reference only. The authoritative source is the **Model Pricing** tab in the top navigation: [Model Pricing](https://docs.apiyi.com/en/models/index.md). Listed prices match the official rates; after recharge bonuses, the effective price is about 83–91% of the listed price. See [Recharge Promotions](https://docs.apiyi.com/en/faq/recharge-promotions.md).

## Related docs

> [card] [GPT-Image Series Agent Skill](https://docs.apiyi.com/en/api-capabilities/gpt-image-2/skills.md)
>
> A ready-to-use SKILL.md plus script, defaulting to gpt-image-2.6-all

> [card] [How can I use the API more economically?](https://docs.apiyi.com/en/faq/api-cost-optimization.md)
>
> A cost checklist covering model tiers, caching, output, and retries

> [card] [OpenAI Codex setup](https://docs.apiyi.com/en/scenarios/programming/codex-cli.md)
>
> base\_url, the responses protocol, and cost notes

> [card] [Image editing and multi-image fusion](https://docs.apiyi.com/en/api-capabilities/gpt-image-2/image-edit.md)
>
> /v1/images/edits parameters, sizes, and masks


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.