Prompt engineering is the practice of writing instructions that get a language model to do useful work reliably. Not once, in a demo, but every time the code runs. The difference between a prompt that works and one that only works sometimes is usually a handful of patterns, applied consistently. This guide covers the prompt engineering practical patterns that hold up in real projects, with examples you can copy and adapt.
Most prompts fail for boring reasons. The task is vague, the model has to guess the output format, or there is no way to tell whether the answer is right. Fix those three problems and most of the magic disappears. What is left is a craft: clear instructions, good examples, and a test loop around the prompt.
Start With the Job, Not the Words
Before you write a prompt, write down the job in one sentence. Who is the output for, what should it contain, and how will you know it is correct? A prompt that says “summarize this ticket” leaves the model guessing at length, tone, and audience. A prompt that says “summarize this ticket in three bullet points for a support engineer, and name the error message verbatim” gives the model something to aim at.
That one sentence also becomes your test. Run the prompt on five real inputs, read the outputs, and note where they miss.
Give the Model a Role and a Constraint
A role tells the model which part of its knowledge to use. “You are a senior backend engineer reviewing a pull request” produces different output than “you are a helpful assistant.” Roles are not personality dressing. They narrow the model’s choices toward the style and standards of a specific job.
Constraints do the rest. Tell the model what to leave out, what format to use, and what to do when it does not know something. A few constraints that pay off almost every time:
- Output format first. Ask for JSON, a table, or a numbered list before you ask for the content.
- A length limit. “In under 150 words” beats hoping the model stops on its own.
- A fallback rule. “If the answer is not in the provided text, say so” stops the model from inventing one.
- What not to do. “Do not add features the user did not ask for” is worth more than a paragraph of positive instruction.
Keep the constraint list short. Five rules the model follows beat fifteen it half-remembers. If you need more rules, split the task into two prompts instead of stacking rules on one.
Show, Don’t Just Tell: Few-Shot Prompting Examples
Few-shot prompting examples are the fastest way to fix output format. Instead of describing a format in words, give the model two or three input-output pairs and let it copy the pattern. This works well for classification, extraction, and rewriting tasks where the format matters as much as the content.
Here is a small example for classifying support tickets:
Classify each ticket as BUG, FEATURE, or QUESTION.
Return only the label and one sentence of reasoning.
Ticket: "The export button does nothing after the latest update."
BUG - The export button stopped working after an update.
Ticket: "Can we add dark mode to the dashboard?"
FEATURE - The user asks for a new capability.
Ticket: "How do I reset my API key?"
QUESTION - The user asks how to do something.
Pick examples that cover the tricky cases, not the easy ones: the borderline ticket, the one with two possible labels. Two or three examples usually do the job; adding ten rarely helps and eats context the real input could use.
When your prompts grow past a few examples, keep them in files and review them like any other asset. The guide on prompt versioning git workflows shows a practical setup for prompt templates that change often.
Chain Small Steps Instead of One Big Ask
Long prompts that ask for five things at once tend to get three of them right and quietly drop the rest. Break the work into steps, and let each step see the output of the last one. A common chain for a code-review task looks like this:
- Read the diff and list every changed function.
- For each function, check error handling and edge cases.
- Write findings as a list, most serious first, with file and line numbers.
Each step is easy to check on its own, and a failure in step two does not corrupt step one. Chaining also makes debugging cheaper. When the final output is wrong, you can look at the intermediate step and see exactly where it went off track, instead of guessing which part of a giant prompt to blame.
Chaining costs extra latency and calls, so use it where correctness matters more than speed. For simple tasks, one well-written prompt is still the right call.
Ask for Structured Output From LLMs
Asking for structured output llm responses makes your code simpler and your failures louder. When a model returns free text, your parser has to guess where the answer ends and the commentary begins. When it returns JSON with a fixed schema, parsing is one line, and a malformed response is an error you can catch and retry.
A practical pattern is to name the fields, their types, and what each one means:
Return a JSON object with exactly these fields:
{
"summary": "one sentence, under 25 words",
"severity": "low, medium, or high",
"affected_files": ["list of file paths"],
"action_required": true or false
}
Do not include any text outside the JSON object.
Some providers offer schema-enforced output, which guarantees the shape or fails outright. Otherwise, validate the JSON yourself and retry once on failure. For more on this, see the deeper walkthrough of prompt engineering patterns that work in production, which covers structured output alongside eleven other patterns.
Prompt Engineering Practical Patterns Need a Test Loop
The prompt engineering practical patterns in this guide only help if you can tell when they stop working. Models change, and a prompt tuned for one model version can drift on the next. A small test set catches that drift before your users do.
Build a set of ten to twenty real inputs with expected outputs, and run the prompt against them whenever you change it. Track two numbers: how many outputs are correct, and how many fail in the same way. A change that fixes three cases and breaks one is a bad trade. A change that fixes one edge case and touches nothing else is safe to ship.
Keep the test set in version control next to the prompt. When the model provider ships an update, run the set again. If scores drop, you will know within minutes instead of learning about it from a support ticket. This is the same discipline as writing system prompts that survive model updates, and it pays for itself the first time a silent model change would otherwise have shipped to production.
What Prompt Engineering Cannot Fix
Prompting has real limits, and knowing them saves weeks of wasted tuning. A prompt cannot give a model knowledge it was never trained on, and it cannot make a model reliably correct about facts it has to guess. If your task needs up-to-date information or your own private data, retrieval is the answer, not a cleverer prompt. The comparison of RAG, fine-tuning, and prompt engineering lays out when each approach fits and when prompting alone is the wrong tool.
Prompting also cannot fix a task that is badly defined. If two engineers disagree about what a good output looks like, no prompt will resolve that. Settle the definition first, write it down, and then encode it in the prompt. Prompts also cannot replace evaluation: a prompt that looks good on three examples and fails on the fourth is not a good prompt.
A Short Checklist to Take With You
Before you ship a prompt, run through this list:
- The job is written in one sentence, with a clear output and a way to check it.
- The model has a role, a format, and a rule for what to do when it does not know.
- Two or three examples show the format instead of describing it.
- Multi-step work is chained, with each step checked on its own.
- The output is structured, validated, and retried once on failure.
- A test set of real inputs runs on every change.
Start with the pattern that fixes your most frequent failure. Add the rest one at a time, and let the test set tell you whether each change helped. That loop, more than any single prompt trick, is what makes prompt engineering work in practice.