Kolkata ·  
AI engineering
foundations
short · 3 min

Prompts as code

Prompts kept in version control and tested like code.

updated 23 Sept 2026

Most teams have a rule: nothing reaches production without review and tests. Now look at where the prompt usually lives in an LLM app:

SYSTEM = "You are a support bot. Be nice. Answer questions."

A string, pasted in somewhere. Change one sentence of that system prompt and the product behaves differently, yet the change can slip past the rule because nobody thinks of it as code. Let's turn it into code, one step at a time.

Step one: make it a function

# prompts/ticket_triage.py
def triage_prompt(ticket: str, product: str) -> dict:
    return {
        "system": (
            f"You sort support tickets for {product} into one label: "
            "billing, bug or account. Reply with the label only."
        ),
        "messages": [{"role": "user", "content": ticket}],
    }

It sits in one small module next to the feature that uses it. The changing parts (the ticket, the product name) come in as typed arguments, not strings glued together across three files. OpenAI's own docs use the same picture: your developer instructions are the function body, and the user's message is the argument.

Step two: change it through a pull request

Now a prompt change is a diff someone reviews. That helps, but only so much. A reviewer can read a one-line prompt change and have no idea what it does to the output. Which is why the next step matters more.

Step three: test it before it ships

What does a test for a prompt look like? Exact-match asserts only go so far, since the output is open-ended text. In practice it's a small set of real tickets with the labels we expect, run before any change ships, and a score we keep track of. That's an eval set, and it gets its own article later.

Step four: pin the model with it

Here's the part people skip. The same prompt can behave differently on another model, and even on another snapshot of the same model. So the prompt and the model version are one unit. Pin the snapshot right next to the prompt, change them together, and run the tests when either moves. Then roll the change out through your normal deploy, behind a feature flag if you want a staged release.

Two ways to lose it again

One is letting the provider hold it. OpenAI used to let you store prompt objects on its side and call them by id. It's now winding that down: new ones were de-emphasised from 2026-06-03, and the v1/prompts endpoint is set to shut down on 2026-11-30. Its guide now says to keep production prompts in your own code, for the same reasons as above, and has a migration guide for moving saved ones over.

The other is letting a framework write it. Some agent frameworks take fields like role, goal and personality and build the prompt for you. That gets you going fast, but when the output is wrong you can't easily see or tune the exact text the model got. The "12-Factor Agents" guide makes it a rule: own your prompts. Write the full prompt yourself, so you know exactly what the model is told. At the very least, make sure you can print the exact prompt a framework sends.

Once prompts live in the repo, a new question shows up: which version of the prompt answered this user, and when did it change? That's prompt versioning.

Further reading