Using Prompt Patterns for Reliable, Structured Output
This lesson explains the prompt patterns that give applications answers they can use: in the right format, consistent from one call to the next, and safe from user input that tries to change the instructions.
Learning outcomes
After completing this lesson, you will be able to:
- Describe prompt patterns such as personas, delimiters and output templates.
- Identify the practices that make output consistent, such as a low temperature and versioned prompts.
Patterns
Several prompt patterns make a model's output more reliable and consistent. Giving the model a role or persona, such as a polite, concise support agent for a bank, sets its tone. Putting user input between delimiters, such as tags, keeps instructions separate from data, which also helps resist prompt injection. An explicit output format, such as "return only JSON with keys category and confidence", produces output that code can parse.
Few-shot examples show the exact format wanted. Step-by-step reasoning improves answers to complex problems. A refusal instruction, such as replying "Not found" when the answer is not in the document, discourages invented answers. Prompt templates, managed in Bedrock Prompt Management, keep fixed instructions and fill in variables at run time, so every request uses the same tested prompt. The following table summarises the patterns.
| Pattern | Example |
|---|---|
| Role or persona | "You are a support agent for a bank. Be concise and polite." |
| Delimiters around input | Put user text between tags such as <review>...</review> so instructions and data are separated |
| Explicit output format | "Return only JSON with keys category and confidence" |
| Few-shot examples | Two or three input-output examples showing the exact format |
| Step-by-step reasoning | "Work through the problem step by step, then give the final answer on the last line" |
| Refusal instruction | "If the answer is not in the document, reply 'Not found'" |
| Prompt templates | Fixed instructions with variables filled at run time (Bedrock Prompt Management) |
Prompt patterns
Consistency
Generative AI applications should produce consistent and reliable output. The following list describes the recommended practices.
- Use low temperature for factual or structured tasks; higher for creative ones.
- Version prompts and test changes on a fixed set of inputs before using them in production.
- Validate output in code (parse JSON, check required fields) and retry or fall back if invalid.
Unreliable output
An application asks a model to classify support tickets, but responses vary in format (sometimes a word, sometimes a paragraph), breaking the code that reads them. What is the simplest fix?
Specify the output format explicitly (for example, one of a fixed list of labels or a JSON object), include a few examples, and lower the temperature. Fine-tuning is unnecessary for a formatting problem.
How can a prompt make a model return valid JSON more reliably?
Show answer
State the exact schema and fields, give an example, ask for JSON only, and validate the result in code.
Exam Tips
Remember for the exam
- Give a role, clear task, constraints and an explicit output format.
- Separate user input from instructions with delimiters.
- Few-shot examples show the model exactly what output looks like.
- Low temperature improves consistency; validate output in code.
- Prompt templates and versions make prompts manageable and testable.