esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

AI engineering / Beginner / 01.07

Prompt Engineering

What a prompt is, why wording changes the answer, and the simple habits that make a model's reply clearer, tidier and easier to trust.

13 min readFree

Writing instructions a model follows

The last lesson, Sampling Parameters, ended with a promise: next, prompt engineering, writing instructions a model follows. Here it is.

Sampling settings change how a model picks its next word. But the biggest lever you hold is much simpler. It is the text you send in. Change the words, and the answer changes.

By the end you will know what a prompt is, why its wording matters, six habits that make replies better, how to build a prompt from a template, and how to test a prompt like a small experiment. You will run three short programs. None of them calls a real model.

Key words

Prompt
The text you give a model so it can reply. A question, an instruction, an example, or all of them together.
Prompt engineering
The work of writing and improving prompts so the model's replies fit what you need.
Context
The background a model needs and does not have: who the reader is, what the goal is, what already happened.
Template
A prompt with blanks in it. You fill the blanks each time, so the prompt stays the same shape.
Placeholder
One blank in a template, written in curly braces, like {task}.

What a prompt is

A prompt is whatever you type to a model. If you have used a chat assistant, you have written prompts already.

Wikipedia defines the craft this way: prompt engineering "is the process of structuring natural language inputs (known as prompts) to produce specified outputs from a generative AI model". OpenAI's guide says almost the same thing in its own words: "the process of writing effective instructions for a model, such that it consistently generates content that meets your requirements".

Notice the word "consistently". One good reply is luck. A prompt that works again and again is engineering.

A model also reads your prompt as text cut into small pieces. The Tokenization lesson in Machine Learning Foundations goes deeper on that. For now, just know that every word you add is something the model reads and, in a paid service, something you pay for.

Why wording changes the answer

People are often surprised that two prompts with the same meaning can give different replies. Say "summarise" or "shorten", "list" or "describe", and the reply may shift in length, tone and layout.

The Wikipedia page says research "consistently demonstrates that LLMs are highly sensitive to subtle variations in prompt formatting, structure, and linguistic properties". It adds that some studies have shown up to 76 accuracy points across formatting changes in few-shot settings. It also says this sensitivity persists even with larger model sizes, additional few-shot examples, or instruction tuning.

Six habits that help

Good prompts share a few habits. Each one below is backed by the vendor guides we read for this lesson. Some are small, but together they change a lot.

1. Be clear and specific

The Claude docs put it plainly: "Think of Claude as a brilliant but new employee who lacks context on your norms and workflows. The more precisely you explain what you want, the better the result."

The same page gives a quick test. Show your prompt to a colleague who knows nothing about the task and ask them to follow it. If they would be confused, the model will be too.

2. Give context

A model knows nothing about your shop, your users or your rules unless you say so. The Claude docs say that giving the motivation behind an instruction, such as explaining why a behaviour matters, "can give Claude more information about your goals and help it deliver more targeted responses".

3. Say the format you want

If you want a one-line reply, a table, a list of three, or plain text with no greeting, say so. The Claude docs advise being specific about the output format and constraints you want.

4. Show an example

A sample is often clearer than a description. The Claude docs say examples are "one of the most reliable ways to steer Claude's output format, tone, and structure", and suggest including 3–5 examples for best results.

Wikipedia gives a tiny case: a prompt may ask a model to complete "maison → house, chat → cat, chien →", and the expected reply is "dog". The model sees the pattern and continues it.

The GPT-3 paper tested this idea at scale. Its abstract says the tasks and few-shot demonstrations were "specified purely via text interaction with the model", applied "without any gradient updates or fine-tuning". In plain words: the examples sat in the prompt, and the model was never retrained. Giving a handful of examples is called few-shot prompting, and it gets its own lesson.

5. Split big jobs into steps

One long request with five jobs inside is easy to get wrong. The Claude docs suggest writing instructions as numbered steps "when the order or completeness of steps matters".

You can go further and split the work across several prompts. The Claude docs call this prompt chaining, and say it is still useful "when you need to inspect intermediate outputs or enforce a specific pipeline structure". A common chain is: write a draft, then check it against a list of rules, then fix it.

Asking a model to think through a problem step by step is a different idea, called chain-of-thought. It also gets its own lesson.

6. Say what to do, not only what not to do

"Don't be long" leaves the model guessing what you do want. The Claude docs give a pair: instead of "Do not use markdown in your response", try "Your response should be composed of smoothly flowing prose paragraphs."

Quick check

Which prompt follows the habit "say what to do"?

A prompt template

Once you know the habits, you do not want to retype them each time. You write a template: the fixed parts stay, and a few placeholders change for each job.

OpenAI's guide describes a message with four sections, usually in this order: Identity, Instructions, Examples and Context. Ours uses six parts instead. Treat both as starting points: OpenAI says the exact best order may vary by model.

  1. 1

    Role

    Who the model should act as. One short line is enough.

  2. 2

    Task

    The one job to do, in a clear sentence.

  3. 3

    Context

    The background that explains why and for whom.

  4. 4

    Format

    How the reply must look: length, layout, what to leave out.

  5. 5

    Example

    One sample input with its ideal output.

  6. 6

    Input

    The new thing for the model to work on this time.

Here is a plain Python builder. It stores the template as text with placeholders, fills it from a dictionary (a list of named values), and prints the finished prompt. It only builds text. It calls no model.

Python
TEMPLATE = """You are {role}.

Task: {task}

Context: {context}

Format: {format}

Example:
Input: {example_in}
Output: {example_out}

Now do the same for this input:
{user_input}"""

def build_prompt(parts):
    return TEMPLATE.format(**parts)

parts = {
    "role": "a friendly support writer for a small bakery",
    "task": "Turn the customer's message into a one-line summary for the kitchen.",
    "context": "The kitchen team reads these on a small screen between orders.",
    "format": "One line, under 12 words, no greetings.",
    "example_in": "Hi! Could I get the birthday cake ready by Friday noon? It must be nut free.",
    "example_out": "Birthday cake, Friday noon, nut free.",
    "user_input": "Hello, please add two extra loaves of rye to my Saturday order.",
}

print(build_prompt(parts))
print("---")
print(len(build_prompt(parts).split()), "words in the prompt")

Output

You are a friendly support writer for a small bakery.

Task: Turn the customer's message into a one-line summary for the kitchen.

Context: The kitchen team reads these on a small screen between orders.

Format: One line, under 12 words, no greetings.

Example:
Input: Hi! Could I get the birthday cake ready by Friday noon? It must be nut free.
Output: Birthday cake, Friday noon, nut free.

Now do the same for this input:
Hello, please add two extra loaves of rye to my Saturday order.
---
86 words in the prompt

The prompt came out as 86 words. Every part is on its own labelled line, so you can read it, change one line, and see what changed. If the bakery adds a new rule, you edit one value in the dictionary and every prompt gets it.

Seeing the difference with a stand-in

Now let's see why format and clarity matter. We need a model to answer, but a real one needs a paid account, a key and a network. So the code below uses a stand-in: a tiny function named fake_model that returns fixed text based on a few words in the prompt.

Be clear about what this is. The stand-in is rigged on purpose to make the point. It answers well when the prompt has a format line, and wanders when it does not. A real model is not like that. A real model is slower, costs money for every token it reads and writes, can give a different answer each time you ask, and can fail or refuse. Its behaviour is far less predictable than a couple of if-lines.

Python
def fake_model(prompt):
    """A stand-in, rigged on purpose: it only reacts to a few words."""
    text = prompt.lower()
    answer = "Sure. Here are some thoughts about your order, and the bakery is lovely."
    if "format:" in text and "under 12 words" in text:
        answer = "Add 2 rye loaves, Saturday order."
    elif "summary" in text:
        answer = "The customer wants extra bread on the order."
    return answer

vague = "Summarise this: Hello, please add two extra loaves of rye to my Saturday order."
clear = (
    "Task: Summarise the customer message for the kitchen.\n"
    "Format: One line, under 12 words, no greetings.\n"
    "Message: Hello, please add two extra loaves of rye to my Saturday order."
)

for name, prompt in [("vague", vague), ("clear", clear)]:
    answer = fake_model(prompt)
    print(name, "->", answer, "(", len(answer.split()), "words )")

Output

vague -> Sure. Here are some thoughts about your order, and the bakery is lovely. ( 13 words )
clear -> Add 2 rye loaves, Saturday order. ( 6 words )

The vague prompt got a chatty reply of 13 words that has nothing the kitchen can use. The clear prompt got 6 words: "Add 2 rye loaves, Saturday order." The only real change was a task, a format line and a labelled message.

Quick check

In our demo, why did the clear prompt get a better reply?

Testing prompts

A prompt is not finished when it looks good. It is finished when it works on many inputs. OpenAI recommends building "tests and evaluation suites that measure prompt behavior so you can monitor performance as you iterate, or when you change and upgrade model versions".

Here is a simple way to do it.

  1. 1

    Collect inputs

    Gather several real examples, including odd ones: very short, very long, and messy.

  2. 2

    Write a check

    Decide what a good reply looks like in a way a program can test, such as "under 12 words".

  3. 3

    Change one thing

    Edit one part of the prompt only. If you change three things, you will not know which one helped.

  4. 4

    Run and compare

    Run both versions on every input and count how many replies pass.

  5. 5

    Keep the winner

    Save the better prompt, and run the test again whenever the model or the prompt changes.

The code below does exactly that, with the stand-in model again. Version A is a bare instruction. Version B is the same instruction plus one added phrase, "One line, under 12 words." That is the single change.

Python
def fake_model(prompt):
    """A stand-in, rigged on purpose. A real model is far less predictable."""
    text = prompt.lower()
    message = text.split("message:")[-1]
    if "under 12 words" in text:
        return "Short note: " + " ".join(message.split()[:5])
    return "Thanks for writing. " + message.strip()

version_a = "Summarise for the kitchen.\nMessage: {msg}"
version_b = "Summarise for the kitchen. One line, under 12 words.\nMessage: {msg}"

inputs = [
    "Please add two extra loaves of rye to my Saturday order.",
    "Can you make the birthday cake nut free? I need it by Friday noon, thank you so much.",
    "Cancel my Monday delivery.",
]

for name, version in [("A", version_a), ("B", version_b)]:
    passed = 0
    for msg in inputs:
        answer = fake_model(version.format(msg=msg))
        if len(answer.split()) < 12:
            passed += 1
    print("version", name, "passed", passed, "of", len(inputs))

Output

version A passed 1 of 3
version B passed 3 of 3

Version A passed 1 of 3 inputs. Only the short "Cancel my Monday delivery." came back short enough. Version B passed 3 of 3. Because we changed one thing, we know the format phrase caused the gain.

The stand-in is rigged again. With a real model the score can wobble between runs, so test on several inputs, more than once.

  • Use many inputs

    One input proves little. Include easy, hard and strange ones.

  • Change one thing

    Edit one part at a time, so you can name the cause of any change.

  • Write the check first

    Say what a good reply is before you look at replies, so you do not grade on a hunch.

A prompt is not a guarantee

Even a well-built prompt is a request, not a command. The model reads it and produces likely text. It may still miss a rule, make something up, or answer in a shape you did not ask for.

OpenAI says so directly: "Because the content generated from a model is non-deterministic, prompting to get your desired output is a mix of art and science." Non-deterministic means the same prompt can give different replies.

The same guide adds a warning for builders: "Even different snapshots of models within the same family could produce different results." A snapshot is one fixed version of a model. It recommends pinning production applications to specific snapshots for consistent behaviour.

So treat every reply as a draft to check. If a program reads the reply, validate it: is it the right length, the right shape, one of the allowed words? If a person acts on it, let a person review the important parts. Never send a model's output straight into a payment, a delete or a medical note.

Tip

Put a check after the model. Prompts raise the odds of a good reply. Checks catch the replies that still go wrong.

Check yourself

Quick check

You edit a prompt in three places, and the replies improve. What is the weakness of that test?

What to take with you

Three things we only touched on get their own lessons: system prompts, zero-shot and few-shot prompting, and chain-of-thought.

Next: system prompts, the standing instructions behind every chat.

Sources

How is this lesson?

Last updated on 16 November 2026

Learn with me

Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.