« All posts

Code or Diffusion: A Field Guide to Programmatic Image Generation

Generating diagrams and charts as code instead of diffusion cuts costs and produces testable, diffable, version-controlled image assets.

A diagram is really a data structure that happens to be visible, yet teams routinely generate diagrams, charts and dashboards through pixel-based diffusion models at $0.02-$0.20 per render, ending up with unfixable, non-editable output. The alternative is having an LLM write the image as code (SVG, draw.io XML, Three.js, Blender, etc.) and letting a renderer turn that code into pixels without any image-generation API fee. Generation stays probabilistic, but the resulting source artifact is deterministic, storable, editable and diffable.

The approach hinges on three ordered decisions: first, whether the target is structure or texture (anything describable with coordinates and labels belongs in code; photoreal skin, grain and real faces belong to diffusion); second, picking the lowest renderer tier that covers the job, from simple SVG up through Blender and Unreal Engine, where each tier unlocks a new capability class rather than simply more power; and third, choosing a model tier. Testing the same prompt across Amazon Bedrock models showed roughly a 95x cost spread between Nova Pro and Claude Fable 5, with capability — not token count — driving quality, and diminishing returns above Opus.

The overlooked advantage is a property, not a price: broken code fails loudly (parse errors, red CI), but valid-yet-wrong code fails quietly too — the difference is that code lets you write machine-checkable assertions (does the SVG contain a given label?), while validating a bitmap usually means bolting on OCR or computer vision. At scale the economics compound: roughly 1,000 code-based images cost about $4 in model spend versus $20-$200 for the diffusion equivalent, and the code-based batch remains editable and verifiable while the bitmaps do not.