Writing system prompts for production: structure, versioning and change control
How to write and manage system prompts for a production AI feature: a clear structure, examples, versioning, review and testing, so prompt changes stop breaking things.
In most AI features, the system prompt is the closest thing to business logic. It decides what the model does, what it refuses and how it sounds. Yet it’s often a long string pasted into the code, edited by whoever last saw a bad answer, with no record of why.
That works for a prototype. In production, a one-line prompt change can quietly break answers for every user. Here’s how we’d write and manage system prompts so they behave like the rest of your code.
Give the prompt a clear structure
A prompt that reads like a wall of rules is hard for the model to follow and hard for your team to change. Split it into sections with plain headings:
- Role and goal: who the assistant is for and what a good outcome looks like, in two or three sentences.
- Context: facts about your product, users or domain that it needs on every request.
- Rules: what it must and must not do, written as short, specific statements.
- Output format: the exact structure you expect back. For structured data, see getting reliable JSON from an LLM.
- Examples: a few good inputs and outputs, including one where the right answer is to decline or say “I don’t know”.
Keep anything that changes per request, such as the user’s data or retrieved documents, out of the system prompt and in clearly marked sections of the message instead. Our post on managing context in LLM apps covers how.
Write rules the model can actually follow
- Be specific. “Keep answers short” is vague. “Answer in at most three sentences unless the user asks for detail” is testable.
- Explain the reason for important rules. A model given the why tends to apply a rule more sensibly than one given only the what.
- Say what to do instead. “Don’t give legal advice” is better as “If asked for legal advice, say you can’t help with that and suggest speaking to a lawyer.”
- Remove rules that no longer apply. Every leftover instruction competes for attention.
- Don’t rely on the prompt for security. Permissions and data access belong in code. See prompt injection for startups.
Version prompts like code
- Keep prompts in your repository, in their own files, not scattered through strings in the codebase.
- Give each version an ID and log it with every request, so you know which prompt produced which answer.
- Change prompts through the same review as any other code change, with a short note on what changed and why.
- Pin the model alongside the prompt. A prompt tuned for one model may behave differently on another.
Test every change before it ships
The most common prompt bug is a fix for one case that breaks three others.
- Run your test set on every prompt change and compare results with the current version, case by case. We cover how in test your LLM feature before users do.
- Add the case that prompted the change to the test set, so the fix stays fixed.
- Look at the failures, not just the score. A small drop can hide a serious new problem.
Roll out prompt changes carefully
- Treat a prompt change as a release, behind the same feature flags you’d use for code.
- Keep the previous version ready so you can switch back in seconds.
- Watch quality signals after the change, such as user ratings, edits and escalations. Our post on rolling out AI features safely walks through this.
The short checklist
- Structure the prompt: role, context, rules, output format, examples
- Write specific rules with reasons and a “do this instead”
- Keep security in code, not in the prompt
- Store prompts in the repo, versioned, reviewed and logged per request
- Test every change against the full set, then roll it out behind a flag
If your prompts have grown into something nobody wants to touch, see our fixed-scope offers or book a 30-minute call.