# 2. Prompt engineering

How to write prompts that work. System prompts, clear instructions, delimiters, few-shot examples, output formats, prompt templates, and common mistakes.

Source: https://glamorous-boar.cloudvent.net/learn/prompt-engineering/

---

A **prompt** is everything you send the model. **Prompt engineering** is writing it so the model reliably does what you want. It is less about magic phrases and more about being clear, specific and testable, the way you would brief a smart new colleague who knows nothing about your situation.

## Anatomy of a good prompt

1. **Role and goal** (system prompt): who the model is and what success looks like.
2. **Context**: the facts it needs (documents, data, conversation).
3. **Task**: the specific request.
4. **Constraints**: rules, tone, length, what not to do.
5. **Output format**: exactly how to answer.
6. **Examples** (optional): one or more input → output pairs.

<CodeBlock language="text" filename="A RAG system prompt" showCopy={true}>

```
You are the HR assistant for Acme. Answer employees' questions using only
the numbered sources below.

Rules:
- Cite every claim inline with the source number, e.g. [1] or [2][3].
- If the sources do not contain the answer, say "I don't know" and suggest
  contacting HR. Never guess.
- Answer in at most four sentences, in plain language.

Sources:
{{context}}

Question: {{question}}
```

</CodeBlock>

## Principles

**Be explicit.** *Summarise this* is ambiguous; *Summarise this support ticket in two bullet points: the customer's problem and what they want* is not.

**Say what to do, not only what not to do.** *Answer in plain English for a non-technical reader* beats *Don't be technical*.

**Separate instructions from data with delimiters.** Wrap untrusted or long content in clear markers (XML-style tags, triple quotes, fenced blocks) and tell the model it is data. This improves accuracy and is a first defence against prompt injection (chapter 11).

**Give the model an out.** Explicitly allow *I don't know* or *not answerable*. Models invent answers when they think an answer is required.

**Specify the output format.** Lists, JSON, a fixed set of labels, a length. For machine-readable output use structured output or function calling (chapter 3) rather than hoping for valid JSON.

**Put the important things first and last.** Models attend best to the beginning and end of long prompts.

**Explain why.** *Keep answers short because they are shown on a phone* gets better compliance than *Keep answers short*.

## Zero-shot, few-shot and examples

- **Zero-shot**: instructions only. Works for most tasks with modern models.
- **Few-shot**: include a few examples of input and desired output. Powerful for formats, tone, edge cases and classification boundaries. Choose diverse examples, include a tricky one, and keep them consistent with your instructions (models copy examples more faithfully than they follow rules).

The Classifier node supports per-label examples for exactly this reason.

## Prompt templates

In applications, prompts are **templates** with variables filled at run time: `{{question}}`, `{{context}}`, `{{history}}`. Templates make prompts versionable, testable and reusable. Keep them in one place, give variables clear names, and fail loudly when a variable is missing (AgenticMind's Text Template does this in `strict` mode with `TEMPLATE_MISSING_VARIABLE`).

## Prompting techniques you will meet

| Technique | Idea | Chapter |
|---|---|---|
| Chain of thought | Ask for step-by-step reasoning before the answer | 9 |
| Self-consistency | Sample several answers, take the majority | 9 |
| Step-back prompting | First ask for the general principle, then solve | 9 |
| Decomposition | Break the question into sub-questions | 9 |
| ReAct | Interleave reasoning and tool use | 7 |
| Self-critique / self-refine | Ask the model to critique and improve its answer | 9 |
| Role prompting | *You are an expert tax accountant* | — |
| Prompt chaining | Split a task into several smaller prompts in a pipeline | 14 |

## Prompt chaining beats mega-prompts

One giant prompt that classifies, extracts, reasons and formats is hard to debug. A **chain** of small prompts, each with one job, is easier to test, cheaper (small models can handle simple steps) and more reliable. That is what a visual workflow is: a prompt chain with types, branches and traces.

## Common mistakes

- **Vague success criteria**: you cannot evaluate what you did not define.
- **Contradictory instructions**: *be thorough* and *be brief* in the same prompt.
- **Too little context**: the model cannot know your company's policies.
- **Too much irrelevant context**: distracts and costs.
- **Tiny `max_tokens` with reasoning models**: empty answers.
- **Trusting one test**: always try several inputs, including adversarial and out-of-scope ones, and keep them as an evaluation set.

## Iterate like an engineer

1. Write the prompt.
2. Collect 10–50 realistic test inputs with expected properties.
3. Run them all, read the failures, change one thing, re-run.
4. Keep the test set; it becomes your regression suite (chapter 12).

## Try it in AgenticMind

- **Text Template** builds prompts from several inputs; **LLM** `system_prompt` holds the role and rules.
- **Context Builder** has an editable RAG prompt template with `{{context}}` and `{{question}}`.
- **Classifier** labels with descriptions and few-shot examples.
- The **Evaluation Lab** turns your test inputs into a dataset and compares prompt versions case by case.
- **Wingman** is itself a carefully engineered prompt plus validation: a good example of *the model proposes, the system checks*.

<Alert variant="tip" title="Key takeaways" text="Write prompts like briefs for a capable newcomer: role, context, task, constraints, format, examples. Separate data from instructions. Let the model say it does not know. Prefer chains of small prompts to one giant prompt, and test every change against a fixed set of inputs."/>
