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

# Image generation

> Generate and edit images through Orbitrage with the OpenAI Images API — routed, billed, and traced.

Image generation works through the standard OpenAI Images API. Point it at
Orbitrage and we route it to our image model (`gpt-image-2`), bill per image at
the real token rates, and trace it alongside your chat calls.

## Generate

<CodeGroup>
  ```python Python theme={null}
  import os, orbitrage
  orbitrage.init(os.environ["ORBITRAGE_API_KEY"], user_id="customer_42")

  from openai import OpenAI
  client = OpenAI()

  import base64

  img = client.images.generate(
      model="gpt-image-2",
      prompt="A minimalist orbit diagram, warm off-white background, single orange accent.",
      size="1024x1024",
  )
  # gpt-image-2 returns the image as base64 in `b64_json` (NOT a hosted `url`) —
  # same as OpenAI's gpt-image family. `data[0].url` will be None; use b64_json.
  with open("orbit.png", "wb") as f:
      f.write(base64.b64decode(img.data[0].b64_json))
  print("saved orbit.png")
  ```

  ```typescript Node.js theme={null}
  import { orbitrage } from "orbitrage";
  await orbitrage.init({ apiKey: process.env.ORBITRAGE_API_KEY, userId: "customer_42" });

  import OpenAI from "openai";
  import fs from "node:fs";
  const client = new OpenAI();

  const img = await client.images.generate({
    model: "gpt-image-2",
    prompt: "A minimalist orbit diagram, warm off-white background, single orange accent.",
    size: "1024x1024",
  });
  // gpt-image-2 returns base64 in b64_json (url is null) — decode and save.
  fs.writeFileSync("orbit.png", Buffer.from(img.data[0].b64_json!, "base64"));
  console.log("saved orbit.png");
  ```
</CodeGroup>

## Edit

Pass an input image (and optional mask) to `images.edit`:

```python theme={null}
edited = client.images.edit(
    model="gpt-image-2",
    image=open("input.png", "rb"),
    prompt="Add a thin orange ring around the planet.",
)
```

<Warning>
  `gpt-image-2` returns the image as **base64** in `data[0].b64_json`, not a hosted
  URL — so `data[0].url` is always `None` (this is how the OpenAI/Azure gpt-image
  family works). Decode `b64_json` and save/serve the bytes yourself, as above.
</Warning>

<Note>
  Image calls are billed at `gpt-image-2`'s multi-rate token pricing (text-in,
  image-in, image-out) — the exact cost is recorded on every call. Vision **input**
  (passing images to a chat model) is just a normal `chat.completions` call with an
  `image_url` content block; it routes to a vision-capable model automatically.
</Note>
