Proposal: AI codegen
First step introducing automated AI codegen into an engineer's workflow
Using an AI agent to write code is unavoidable nowadays. But sometimes we want more than that.
Problem
I was building a form where the user types values into fields. Some values can be validated on the frontend, some must be validated on the backend, and some need both. Hence, I have to write the check “the year must be from 1900 to 2100” in both places: zod in TypeScript for the FE, and Rust for the BE.
Before, we would usually think of a codegen solution at this point. However, since what we actually want is “translation”, an LLM comes to mind.
Solution
As mentioned, we will primarily use an LLM for this task. The general idea is to write only the Rust part, and set up a harness to guard it and translate it into the other kinds of output we want.
Here is the workflow (still very rough!) that I am using:
Workflow
For every snippet that I intend to be the “source”, I wrap it with:
// To AI agent: ALWAYS copy this comment
// @ai-codegen-type: form object with validation
// @ai-codegen-src-hash:
// @ai-codegen-dst-path:
// @ai-codegen-dst-hash:
... my code here ...
// @ai-codegen-end
It is quite verbose; I will optimize it later.
Then, I have a script hooked into the git pre-commit hook that goes through all matches of @ai-codegen-type: . For each type, there is a prompt file that describes how to generate the target - what to make, where to put it, etc. It can be as simple as this:
# ai-codegen guide for type "form object with validation"
Generate the zod validation from the given snippet in fe/generated/. Don't forget tests and ensure consistency with other code.
For each snippet, the script calculates its hash. If the hash is the same, the source snippet has not been modified, so it is skipped. Otherwise, the script triggers a subagent (or claude -p) to do the translation.
Retro
A great thing about this method is that I don’t have to write a proper codegen myself (or spend tokens on it), which is error-prone, time-consuming, and not really efficient. For example, sometimes I want a particular if to be BE-only, so I put // @ai-codegen-be-only on it and trust my agent NOT to bring it to the FE targets. To summarize, if a task is ambiguous, the best tool to solve it is an LLM.
Of course, this wastes some tokens. But it is only as wasteful as “asking the agent to generate for us”, minus the cost of “having to remember to ask it to generate again once we change the source”.