A prompt that looks impressive in a playground is not yet a production component. Production behaviour depends on the model version, input distribution, retrieval sources, tool permissions, output handling, latency, cost, and the consequences of a wrong answer. Prompt engineering is therefore part of software and systems engineering.
Define the Task Before Writing the Prompt
Specify what the model is expected to do, what it must not do, the permitted sources of truth, the output contract, and when it should abstain or escalate. Separate instructions from untrusted user content and retrieved documents. State which facts must come from tools or source material rather than model memory.
For structured workflows, use schema-constrained or structured output capabilities where available, then validate the result in application code. A JSON-looking response is not proof that it is valid, complete, authorised, or safe to execute.
Version Prompts Like Code
Keep prompts and templates in version control. Record the model identifier, system instructions, template variables, retrieval configuration, tool schema, decoding settings, and any post-processing rules. A prompt change can alter application behaviour even when no application source file changes.
Use explicit release identifiers and a rollback path. Avoid editing a production prompt invisibly in a dashboard. If a provider changes model behaviour behind a stable alias, your evaluation and monitoring should help detect the change.
Build an Evaluation Set That Represents Reality
Create a reviewed set of representative inputs from the task's real operating conditions. Include normal cases, edge cases, ambiguous requests, malformed inputs, multilingual examples when relevant, and examples that should be refused or escalated. Protect private data and document how the evaluation set was created.
Score dimensions that matter for the use case: factual correctness, source faithfulness, completeness, format validity, tool selection, refusal quality, latency, and cost. Use human review for subjective or consequential outputs. Compare candidate versions on the same set and inspect individual regressions; a single aggregate score can hide a serious failure in a smaller subgroup.
Test the Entire Pipeline
For retrieval-augmented generation, evaluate retrieval separately from generation. Check whether the right source passages were retrieved, whether the answer is supported by those passages, and whether the system admits when evidence is missing. For tool-using systems, test permissions, argument validation, timeouts, duplicate actions, partial failures, and recovery from unavailable dependencies.
Do not treat self-critique or repeated model calls as a universal fix. Additional calls can increase cost and latency, and they may repeat the same mistake. Add a second model or review step only when measured results justify the complexity.
Design for Prompt Injection and Unsafe Outputs
Retrieved webpages, uploaded documents, emails, and user input are untrusted data. They can contain instructions that attempt to redirect the model, disclose information, or trigger tools. A prompt that says “ignore malicious instructions” is not a complete security boundary.
Enforce authorisation outside the model. Give each tool the minimum required permissions, validate every argument, separate read and write capabilities, require confirmation for consequential actions, and keep secrets out of model context. Treat model output as untrusted input before rendering it, storing it, executing code, or passing it to another system.
Observe Quality After Release
Monitor request volume, latency, token use, cost, validation failures, retrieval quality, tool errors, user corrections, and escalation rates. Avoid logging raw sensitive prompts by default; define redaction, retention, access, and deletion rules. Sample outputs for review in a way that respects user privacy and applicable law.
Set release gates based on risk. A low-impact drafting assistant may tolerate a different error profile from a system that affects payments, health, legal work, or access to essential services. For high-impact workflows, keep qualified human oversight and a documented incident response path.
Keep a Rollback and Change Process
Test changes in a controlled environment, compare against a baseline, review failures, and deploy gradually where possible. Record the model and prompt version used for each important result. If quality or safety degrades, route traffic back to the previous version while investigating.
The goal is not to discover one perfect prompt. It is to build a system whose behaviour can be measured, explained, constrained, and improved as models and real-world inputs change.
Sources and further reading
- OWASP Top 10 for Large Language Model ApplicationsOWASP GenAI Security Project
- AI Risk Management FrameworkNational Institute of Standards and Technology