A prompt is code affecting behavior
The first principle: treat prompts as code. A prompt determines the behavior of part of an application no less than ordinary code, so it deserves the same discipline: versioning, structure, careful changes, testing. An "unnoticed" prompt edit can change behavior across thousands of requests — this is not a trifle. Let us cover how to organize prompts as code.
"Prompt as code" practices
- Versioning: keep versions of prompts (in version control, like code). To understand what changed and roll back.
- Separation from logic: keep prompts as explicit, readable parts (not scattered strings in the code) — easier to find, change, test.
- Templates with substitution: a prompt template + variable substitution (user data, context) — like parameterized code.
- Change discipline: change one at a time, test before rollout, be able to roll back (from LLMOps).
Templates with substitution — and caution with injections
A production prompt is usually = a template + substituted variables (user data, context from RAG): "Classify the review: {text}. Categories: {categories}". This is like a parameterized function: a fixed structure + changing inputs. But an important security point: substituting user data is a potential prompt-injection point (from LLMOps). A user can insert text trying to override the instructions ("ignore the rules"). Treat substituted user data as untrusted, separate instructions and data, do not let user input control the behavior (more in the reliable-output module).
Versioning + tests = managed improvement
A key tie with LLMOps: versioning + testing give managed prompt improvement. Changed to tested on a set of examples to compared with the past version to rolled out if better to rolled back on problems. So prompts improve without unnoticed regressions. Change one change at a time (or you will not understand what helped or broke), do not edit the prompt blindly right in production (the change affects all requests), document the intent (why the prompt is such — prompts can be non-obvious). Without this discipline teams get stuck in "we fixed the prompt — something broke, unclear what".
Rule: treat prompts as code — version them (history, rollback), keep them separate from the logic, use templates with substitution (carefully with injection of user data), change disciplinedly (one at a time, a test before rollout). Versions + tests = improvement without regressions.
🧠 What is important to account for when substituting user data into a prompt template?