Creating many AI images at once: the balance, the price and the request limit

All in One AIAI Images·· 2 views· 4 min read

In short: before asking for dozens of images, do three things: the maths (the price per image times the number of images), check your balance and get your program ready for the limit of requests per minute. Test everything first with a sandbox_ key, which costs nothing.

1. Doing the maths

The price of each model is on the Models tab and in pricing.credits, in the GET /v1/models list. Multiply it by the number of images. For example, if the Models tab shows 2 credits per image, 50 images need 100 credits. That is the base price: options such as size or quality can change the real cost.

Then check your balance with GET /v1/ai/balance. Each request reserves its own credits; if the balance does not cover the reservation, that request gives 402 insufficient_credits and charges nothing. The next ones would give the same error, so your program should stop there.

2. Testing the program in the Sandbox

With a sandbox_ key, each generation is completed at once, with an example link and 0 credits. The model list, the prices and the balance are the real ones. This way you check the whole cycle without spending, and only then switch to the prod_ key.

3. Keeping within the request limit

Each generation request counts, and so does each status check. Check every 5 seconds or more, and do not send all the images at the same time. The limit of each key comes in the X-RateLimit-Limit header, and the whole account has 300 requests per minute, adding up its keys. A 429 brings Retry-After: wait those seconds. See the request limit per key.

4. An example, one image at a time

It asks for each image, waits until it is ready, downloads it and moves to the next. It stops at the first request error, such as a 402:

import os, time, uuid, requests

API = "https://api.interweb.co.mz/v1"
H = {"Authorization": "Bearer " + os.environ["INTERWEB_API_KEY"]}
prompts = ["A white mug on a wooden table", "A blue notebook on a desk"]

for i, prompt in enumerate(prompts, 1):
r = requests.post(API + "/media/generations", timeout=30,
headers=dict(H, **{"Idempotency-Key": str(uuid.uuid4())}),
json={"model": "openai/gpt-image-2/text-to-image",
"input": {"prompt": prompt, "size": "1024x1024"}})
if r.status_code != 202:
print(i, r.status_code, r.json()["error"]["code"])
break
job = r.json()["data"]
while job["status"] == "processing":
time.sleep(5)
job = requests.get(API + "/media/generations/" + job["id"], headers=H, timeout=30).json()["data"]
if job["status"] == "completed":
with open("image-%d.jpg" % i, "wb") as f:
f.write(requests.get(job["outputs"][0], timeout=60).content)
print(i, job["status"], job["credits_charged"], job.get("error"))

Each image has its own Idempotency-Key. If you have to repeat the request for one image after a network failure, use that image's same value: you do not pay twice. See the Idempotency-Key header.

5. Saving as they are ready

The links in outputs are not permanent: download each image as soon as it is ready, as in the example. Keep the prompt of each one next to the file too. The ones that fail cost nothing and you can ask for them again at the end.

In the terminal, aio generate image does the same for one image at a time and downloads it to --out. You can call it from a script, one line per image.

Planning a large batch? Check your balance and buy credits in All in One AI.

See All in One AI

SEE ALSO

What an AI image costs in credits: the price per image, the reserve and the refund

The request limit per key: 429 rate_limited and the Retry-After header

Not paying twice for a generation: the Idempotency-Key header

Saving an AI image to your website or project: why you should download it

All in One AI

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from $5.36/mo (3-year plan, with coupon)

See plans
Was this answer helpful? Your answer helps us improve this article.