AI ENGINEERING · PROMPTING AND STRUCTURED OUTPUT · PROMPTING · 7 OF 35

Prompt Engineering Basics: Anatomy of a Good Prompt

How to write prompts that work in production: the six parts of a good prompt, system prompt versus user message, a weak and a strong prompt side by side, tags around documents, saying what to do instead of what not to do, permission to say I don't know, and treating prompts as code with an eval set and versions.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
1 / 17

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Role + goal
  • context in tags
  • one task
  • constraints
  • exact format
  • a way out
  • evals
  • versions

Notes

The idea

Here is the mental model. Picture a very smart colleague on their first day. They have read a huge amount, but know nothing about your product, your users, or what good looks like to you. The prompt is your brief to them, and it is all they get. Whatever you leave out, they will guess. Prompt engineering is mostly the skill of not leaving the important things out.

The core idea
  • the model has read a lot, but knows nothing about
  • your product, your users or what "good" means to you
  • a prompt is a brief for a smart new colleague on their first day
  • whatever you leave out, it will guess

Anatomy

A good prompt has up to six parts. A role and goal, saying who the model is and what success means. Context, the facts and documents it needs. The task, the one specific job. Constraints, like length, tone and what to do when unsure. The output format, the exact shape of the answer. And examples, a few samples of good output. A quick question needs two of these. A production feature usually needs all six.

Six parts of a prompt
  • 1 role + goal: who it is, what success means
  • 2 context: facts, documents
  • 3 task: the one job to do
  • 4 constraints: length, tone, what to do when unsure
  • 5 output format: exact shape
  • 6 examples: one to three
  • not every prompt needs all six

System vs user

Chat APIs give you two main places to write. The system prompt is written by you and stays the same on every call. It holds the role, the rules, the format and the tone. The user message changes every call, the actual question or document. Models are trained to give system instructions more weight. But that is a strong hint, not a security wall, so never rely on it alone to protect secrets.

Two places to write
  • system prompt written by you, same on every call: role, rules, format, tone
  • user message changes every call: the question, the document
  • user text never overrides
  • your rules by being louder

A weak prompt

Let us work an example. The job is to summarise support tickets for the on call engineer. The weak prompt is one line. Summarise this support ticket, then the ticket. Run it three times and you get five paragraphs, then a bullet list, then a polite reply to the customer. None of them says how urgent it is, and the error code got paraphrased away. The model did nothing wrong. It was never told what the summary is for.

Weak
Summarize this support ticket:

[ticket text pasted here]

result, three runs:
  run 1: five paragraphs
  run 2: a bullet list
  run 3: a polite reply
         to the customer
no severity, error code lost

A strong prompt

Now the strong version, for the same model and ticket. A role, you triage tickets for on call engineers. A goal, they must decide what to do in thirty seconds. The ticket inside tags. Then the task, three constraints, and a fixed format: severity from P one to P four, three bullets, one next step. Each line removes a guess. Now three runs give the same shape, with the error code intact.

Strong
You triage tickets for on-call engineers.
Goal: they decide what to do in 30 s.
<ticket>{ticket_text}</ticket>
Summarise the ticket above.
- at most 3 bullets, plain English
- copy error codes exactly
- missing info: write "unknown"
Format:
Severity: P1-P4
Summary: <bullets>
Next step: <one line>

Clear beats clever

You will see lists of magic phrases online. You are the world's best expert. This is extremely important. All caps. Promising a tip. On modern models these do little, and shouting can make a model over apply a rule everywhere. What works is being specific. Say who reads the output and why a rule exists. Replace adjectives with numbers. Short becomes under sixty words. Clear beats clever, and it keeps working when you upgrade the model.

What actually helps
  • weak tricks "you are the world's best" "this is very important!!" ALL CAPS, threats, tips
  • what works say who reads the output say why a rule exists give numbers, not adjectives "short" → "under 60 words"

Tags and delimiters

When the prompt includes documents, wrap each one in clear delimiters. X M L style tags work well, like a contract tag with an id, or a fenced block. Now the model knows exactly where the data ends and your instructions begin, and you can refer to contract A by name. With long documents, put them first and the question last. That ordering often improves answers. Tags also make injected text easier to spot, though they are not a full defence.

Mark the data
<contract id="A">...</contract>
<contract id="B">...</contract>

Compare contract A and B on
termination terms.

why:
- model knows where data ends
- you can refer to it by name
- long docs first, question
  last often scores better

Say what to do

Tell the model what to do, not just what to avoid. Do not use markdown is weaker than write in plain paragraphs of flowing prose. Do not be too long is weaker than answer in two sentences. A don't still puts the unwanted thing in front of the model, and leaves the right behaviour to chance. Keep genuine prohibitions, like never reveal another customer's data, but pair each one with what to do instead.

Do, not don't
  • don't: "Don't use markdown."
  • do: "Write plain paragraphs of flowing prose."
  • don't: "Don't be too long."
  • do: "Answer in 2 sentences."
  • a "don't" names the thing
  • you do not want, and leaves
  • the alternative to chance

Allow I don't know

Models are trained to be helpful, so when the answer is missing they tend to produce something plausible anyway. Give them an honest way out. Answer only from the documents. If the answer is not there, reply exactly, not in the documents. A fixed phrase like that is easy for your code to detect, so you can show a fallback or route to a person. This one line removes a large share of made up answers in question answering features.

A way out
  • without it: question not in the docs → a confident invented answer
  • with it: "Answer only from the <docs>. If the answer is not there, reply exactly: Not in the documents."

Output format

Be exact about the output format, because often your code reads it, not a person. Name the fields, their order and their labels. Show one filled in sample. Say what to write when a field is unknown, so the model does not invent a value. When a program must parse the result, ask for JSON and validate it. Episode three of this unit covers structured output modes that make valid JSON close to guaranteed.

Pin down the format
  • say the shape: fields, order, labels, length
  • show one filled-in sample
  • say what to output when a field is unknown
  • if code parses it: ask for JSON, validate it, or use a schema mode (episode 3)

Iterate with evals

How do you know a prompt got better? Not by trying two inputs and squinting. Build a small eval set, say thirty real tickets with the severity a senior engineer picked. Version one scores twenty one of thirty, seventy percent. Adding the role and format gives twenty six, about eighty seven percent. The unknown rule gives twenty seven, ninety percent. Change one thing at a time and rerun every case, because a fix for one input often breaks another.

A small eval set
30 real tickets + expected severity

v1  weak prompt       21/30  70%
v2  + role, format    26/30  87%
v3  + "unknown" rule  27/30  90%

grade: code checks first
  (format, severity match),
  a model judge for tone
run each case 3x if sampling

Version prompts

Treat prompts as code. Keep them in files in your repository, reviewed like any other change, not edited live in a console. Give each version an id and log it with every model call, so when users report bad answers you know which prompt produced them. Run the eval set before merging. Roll new prompts out gradually and keep rollback to one step. And pin the model version, because a model upgrade changes behaviour just like a prompt edit does.

Prompts as code
  • keep prompts in files, in git, not in a web console
  • each version has an id
  • log prompt id with every call and its output
  • evals run before merge
  • roll out gradually, roll back in one step
  • pin the model version too

Common mistakes

The usual mistakes. Assuming the model knows things it was never told. Rules that contradict each other, like be brief and be thorough, so the model picks one at random. One giant prompt doing five jobs, when a chain of small prompts would be easier to test. Patching each bad output with a special case rule and never rerunning the tests. No fixed format, then fragile parsing. And putting secrets in a system prompt, which users can often coax out.

Common mistakes
  • assuming context the model never got
  • rules that contradict each other ("brief" + "detailed")
  • one giant prompt doing five jobs: split into steps
  • fixing one bad output with a special-case rule, untested
  • no format, then regex hell
  • secrets in the system prompt

Numbers worth knowing

Some ballparks. A token is roughly three quarters of an English word. A production system prompt is often five hundred to five thousand tokens, and it is sent on every call, so its size matters. Start with twenty to fifty eval cases and grow toward hundreds as you collect real failures. One to three examples usually set the format. Output tokens typically cost three to five times input tokens. Keeping the long stable part first lets prompt caching reuse it cheaply.

Ballpark numbers
1 token         ~ 3/4 of an English word
chat question   ~ 20-200 tokens
prod sys prompt ~ 500-5,000 tokens
first eval set  ~ 20-50 cases
mature eval set hundreds to thousands
examples        1-3 usually enough
output tokens   ~ 3-5x price of input

a long stable prefix is cheap
to reuse with prompt caching

Say it like this

How to say it. The system prompt sets the role, the goal, the rules and an exact output format. Documents go inside tags with the question last, and the model is told to answer unknown rather than guess. Prompts live in git with version ids that we log on every call. Every change runs against a fifty case eval set and only ships if the score holds or improves. That shows you treat prompting as engineering, not wordsmithing.

In the interview
  • "The system prompt sets role, goal, rules and an exact output format. Documents go in tags, question last. It may answer 'unknown'. Prompts live in git with ids; every change runs a 50-case eval and ships only if the score holds."

Interview questions

Q1What are the main parts of a good prompt?

A role and goal, the context the model needs, one clearly stated task, constraints such as length and what to do when unsure, an exact output format, and optionally a few examples. Simple questions need only some of these; production prompts usually need all.

Q2What is the difference between the system prompt and the user message?

The system prompt is written by the developer and stays the same across calls: role, rules, format, tone. The user message carries the request of the moment. Models weight system instructions more heavily, but that is not a security boundary.

Q3Why wrap documents in tags or delimiters?

So the model can tell instructions apart from data, and so you can refer to each document by name. It reduces confusion with long or multiple documents and makes injected instructions easier to spot, though it is not a complete defence.

Q4Why say what to do instead of what not to do?

A negative instruction names the unwanted behaviour but leaves the alternative to chance. "Answer in two sentences" is more reliable than "don't be long". Keep real prohibitions, but pair them with the desired behaviour.

Q5How do you reduce hallucinated answers through prompting?

Ground the model in provided documents, tell it to answer only from them, and give an explicit way out such as replying with a fixed phrase when the answer is missing. The fixed phrase lets code detect it and fall back gracefully.

Q6How do you know a prompt change is an improvement?

Run it against a fixed eval set of real inputs with expected outputs, scored by code checks where possible and a model judge where needed. Change one thing at a time, compare scores with the previous version, and watch for regressions on cases that used to pass.

Q7How should prompts be managed in production?

Like code: stored in version control, reviewed, given version ids that are logged with every call, gated by evals before release, rolled out gradually with quick rollback, and paired with a pinned model version.

Q8Do tricks like "you are the world's best expert" or all-caps warnings help?

Rarely on modern models, and shouting can make a model over-apply a rule. Specific, explained instructions with concrete numbers work better and keep working across model upgrades.