Sub topic 12
Scaling up with batches and APIs
This chapter assumes you can write a single prompt and iterate on it one change at a time. It also assumes you know introductory Python and have used a command line. Once a prompt works, you may need 40 variants across four channels. Typing each one into a web app is slow and hard to repeat. A batch script or an API (application programming interface, a way for your code to call a service) fixes that.
When scaling is worth it
Scale up only after a single prompt produces acceptable results. A batch multiplies whatever the prompt does, including its defects. Good reasons to use a batch:
- You need many variants for an A/B test.
- You run the same brief across products, such as ten bottle colors.
- You want a record of exactly which prompt and settings made each file.
The working model
A scripted workflow has four parts. Each one is a place where errors can occur.
- Jobs. Each job is one prompt plus its settings: size, seed (a number that makes a result repeatable on tools that support it), and a label.
- Calls. Your code sends each job to the tool's API and receives image data.
- Storage. Each file is saved under a predictable name, with a manifest (a list of what was made) beside it.
- Review. A person still checks every image before it is published.
Building a job list
Keep one base brief and change one field per job. This applies the one-change-at-a-time rule from the iteration chapter, so differences in the results can be traced to a cause. The script below builds the jobs and writes a manifest. The generate function is a stub, so the script runs without an account. Replace its body with your tool's API call.
import csv
from pathlib import Path
BASE = {
"subject": "a stainless steel water bottle on a flat rock beside a mountain lake",
"style": "clean product photography, soft natural light",
"palette": "cool blues and warm gray",
"avoid": "text, logos, people",
}
VARIATIONS = {
"palette": ["cool blues and warm gray", "forest green and sand"],
"style": ["clean product photography, soft natural light",
"bright morning light, shallow depth of field"],
}
def build_jobs(base, variations, seeds=(1, 2)):
jobs = []
for field, values in variations.items():
for value in values:
for seed in seeds:
brief = dict(base, **{field: value})
prompt = (f"{brief['subject']}. {brief['style']}. "
f"Color palette: {brief['palette']}. "
f"Do not include: {brief['avoid']}.")
name = f"{field}-{value[:12].replace(' ', '_')}-s{seed}"
jobs.append({"name": name, "prompt": prompt, "seed": seed})
return jobs
def generate(job):
# Stub: replace with a real API call that returns image bytes.
return b""
out = Path("batch_out")
out.mkdir(exist_ok=True)
jobs = build_jobs(BASE, VARIATIONS)
with open(out / "manifest.csv", "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["name", "seed", "prompt"])
writer.writeheader()
for job in jobs:
(out / f"{job['name']}.png").write_bytes(generate(job))
writer.writerow({k: job[k] for k in ("name", "seed", "prompt")})
print(f"Wrote {len(jobs)} jobs")This produces eight jobs: two fields, two values each, two seeds.
Common errors
- Ignoring rate limits. Most APIs cap requests per minute. Send jobs one at a time or in small groups, and retry a failed call after a growing delay.
- Unbounded cost. Each image usually costs money or credits. Test the script on two or three jobs, then check the price per image before running the full list.
- Hard-coded keys. Keep API keys in environment variables, never in the script or in version control.
- No manifest. Without a record of the prompt and seed, you cannot reproduce or explain a winning image.
- Skipping review. A batch of 40 can hide a distorted hand or garbled text in one file. Review each image, as described in the quality review chapter.
Exercise
You run a batch of 24 images and one of them wins an A/B test. A month later you want a similar image for a new channel. What should you have saved, and why?
Show answer
Save the manifest row for that image: its exact prompt, seed, size, and the tool and model version. With those you can regenerate it or change one field and compare. Without them you can only guess how it was made.