Topic 6 of 6 · 4 min read
Estimating and capping image generation costs
Goal: Estimate the monthly cost of an image feature from usage numbers and set per-user limits that keep it within budget.
Estimate cost as images per month times the price of the chosen size and quality, then multiply by the real number of calls per accepted image: previews, retries and variations; cap it per user before launch.
Check yourself · 1 of 3
Each image call costs $0.04 and users make 3 calls for every image they keep. What does one kept image cost?
Why it matters
Image calls are charged per image, and the price depends on the model, size and quality. A feature that looks cheap per call becomes a budget problem when each user triggers many calls. Unlike a slow page, an unlimited image button turns a bug or an abusive user directly into an invoice.
How to do it
- Get the price from the pricing page. Prices and models change. Read the vendor's current pricing page, note the date, and put the number in a config value, not in your head.
- Count calls per accepted image. Users generate several variations, regenerate, edit and retry. If an average user makes five calls to keep one image, the real cost per kept image is five times the price per call. Measure this in a pilot.
- Estimate monthly spend. Active users times images per user times calls per kept image times price per call. Add a margin for retries after errors.
- Reduce the price per call. Use low quality and small size for previews, final quality only on save. Cache by a hash of template version, slots and settings so the same request returns the stored image instead of a new one.
- Set limits. A per-user daily or monthly quota, a rate limit per minute, and a global monthly ceiling that turns the feature off or into a queue when reached. Return a clear message when a quota is used.
- Watch actual spend. Log cost per call, show spend per day, and alert when it passes a threshold.
Before and after
Before: "Images cost four cents each and we expect 1,000 users with 10 images each, so about $400." The estimate ignores regenerations.
After: Assume 1,000 monthly users, 10 kept images each, 4 calls per kept image and a price of $0.04 per call (a made-up number for this example). Cost: 1,000 x 10 x 4 x 0.04 = $1,600. With draft previews at a quarter of the price for 3 of the 4 calls, the cost per kept image falls from $0.16 to $0.07, so the month costs about $700.
What goes wrong
- A loop in the client calls the endpoint repeatedly and no limit stops it.
- Quotas exist per account but not per IP or device, so free accounts are created to get more images.
- A retry policy multiplies cost during an outage.
- Cache keys include a timestamp, so the cache never hits.
Examples
A monthly estimate
| Item | Value |
|---|---|
| Users per month | 2,000 |
| Kept images per user | 5 |
| Calls per kept image | 3 |
| Price per call (example) | $0.05 |
2,000 x 5 x 3 x 0.05 = $1,500. Adding a 10 percent retry margin gives $1,650.
A cache key
const key = sha256(`${TEMPLATE_VERSION}|${subject}|${colour}|${size}|${quality}`);
const hit = await storage.find(key);
if (hit) return hit;
The key leaves out timestamps and user ids, so identical requests share one image.
Key points
- Cost is price per call times calls per kept image times kept images per user times users.
- Read current prices from the vendor's pricing page and store them in config with a date.
- Cheap previews and a cache keyed on template, slots and settings cut the number of full-price calls.
- Set per-user quotas, rate limits and a global monthly ceiling before launch.
- Log cost per call and alert on daily spend.
Common mistakes
- Estimating from price per call without counting regenerations and variations.
- Launching without a per-user quota and a global spend ceiling.
- Using a cache key that includes a timestamp or request id.
- Hard-coding a price instead of reading it from a dated config value.
Check yourself · 2 of 3
Which change most directly reduces the number of full-price calls?
Check yourself · 3 of 3
A cache for generated images never hits. Which cache key is the likely cause?
Practice items, not counted towards your mastery.
Start here
- Image generation guide (OpenAI) (opens in a new tab)DocsFree
Size, quality, edit and mask parameters for the OpenAI image endpoints.
- Image generation with Gemini (Google) (opens in a new tab)DocsFree
Gemini's text-to-image and conversational editing, including reference images and its watermark note.
- FLUX documentation (Black Forest Labs) (opens in a new tab)DocsFree
How the FLUX API submits a job and returns a polling address instead of the image.
More for this topic
- OpenAI API pricing (opens in a new tab)DocsFree
Current image prices by model; check it before any estimate.
Where to go in your country
- Article 50: Transparency obligations (EU AI Act) (opens in a new tab)DocsFreeEU
Marking duty for providers of generated content and disclosure duty for deepfakes.