Unit 4 · Inference and Interaction
Lesson 14 · Available to read
Prompt Engineering: Communicating Effectively with LLMs
Practical techniques for steering models: clear instructions, context, examples, structure and step-by-step reasoning.
Everything an LLM does in a given request is conditioned on its context window. The weights are fixed at inference time, so the prompt is your main lever. Prompt engineering is less about magic phrases and more about clear communication — the kind you'd give a very capable colleague who knows nothing about your situation.
Why prompts matter so much
An instruction-tuned model tries to produce the most helpful continuation given what it can see. Anything you leave unsaid, it has to guess: who the audience is, how long the answer should be, what "good" looks like, what to do when information is missing. Better prompts remove guesswork.
Core techniques
1. Be clear, specific and direct. "Summarise this" invites a guess. "Summarise this incident report in 5 bullet points for an executive who has two minutes; lead with customer impact" does not. Say what you want rather than only what to avoid.
2. Give context and purpose. Explain who the output is for and why. Knowing that a summary is for a legal review or for a children's newsletter changes almost every choice the model makes.
3. Assign a role. A system prompt such as "You are an experienced data engineer reviewing pull requests" sets vocabulary, expertise level and priorities.
4. Show examples (few-shot prompting). A few input–output pairs teach format and judgement better than paragraphs of description. This ability to pick up a task from examples in the prompt — in-context learning — was one of the headline findings of GPT-3 in 2020. Use varied examples so the model learns the pattern rather than copying surface details.
5. Structure the input. Separate instructions from data with clear delimiters, such as headings or XML-style tags: <document>…</document>, <instructions>…</instructions>. This prevents the model from confusing your data with your instructions, and lets you refer to parts by name.
6. Specify the output format. Ask for exactly the structure you need — a table, JSON with given keys, a numbered list, a word limit. You can even begin the answer yourself (pre-filling the first characters, e.g. {) to lock in the format.
7. Give the model an out. "If the answer isn't in the document, say you don't know" reduces made-up answers (Lesson 18).
8. Place long documents first. With very long inputs, putting the documents at the top and the question at the end often works better — consistent with the position effects in Lesson 7.
Let the model think: chain-of-thought
For multi-step problems — arithmetic, logic, planning — asking the model to reason step by step before answering markedly improves accuracy. Research in 2022 showed this both with worked examples of reasoning (chain-of-thought prompting) and with a simple cue like "Let's think step by step".
Why does it help? Each generated token gets a fixed amount of computation. Writing out intermediate steps gives the model more computation per answer and lets it use its own output as working memory — it can read the partial results it just wrote.
Reasoning models take this further: they are trained (often with reinforcement learning against verifiable rewards, Lesson 12) to produce an extended internal chain of thought before replying, and to spend more of it on harder problems. With these models you usually don't need to say "think step by step"; it's more useful to state the goal and constraints clearly and let them plan.
Break big tasks apart
Complex jobs often work better as a chain of prompts: extract the relevant facts → analyse them → draft → critique and revise. Each step is simpler, easier to check and easier to debug. The same idea underlies agents (Lesson 16).
Treat prompting as an experiment
Prompts that look great on one example often fail on the next. Professionals work in a loop:
- Collect a small test set of realistic inputs, including hard and edge cases.
- Run the prompt on all of them.
- Read the failures and find the pattern.
- Change one thing and run again.
That's evaluation in miniature (Lesson 17). It also guards against regressions when you switch models: a prompt tuned for one model may need adjusting for another.
Common pitfalls
- Vague goals ("make it better") with no criteria for "better".
- Contradictory instructions buried in a long prompt.
- Examples that are all too similar, so the model copies their quirks.
- Mixing untrusted content (emails, web pages) with instructions — a security risk called prompt injection (Lesson 19).
- Over-stuffing the context with irrelevant material.
Further reading: Anthropic's prompt engineering guide at docs.claude.com covers these techniques with many examples.
The takeaway
A prompt is the model's entire world for that request: state the goal and context clearly, show examples of what you want, structure the input, specify the output, leave room for reasoning on hard problems — and treat prompting as an experiment you test and refine on real cases.
Practice
0 of 7 attempted
- 1Apply
Rewrite this weak prompt into a strong one: "Write something about our new app." Invent any details you need, and include at least four of the six prompt components from the lesson.
Saved in this browser
- 2Explain in your own words
Why does chain-of-thought prompting improve performance on multi-step problems? Give two reasons.
Saved in this browser
- 3Apply
Write a few-shot prompt (at least three examples) that classifies customer emails as Billing, Technical, or Other.
Saved in this browser
- 4Multiple choice
What is the main reason to wrap pasted documents in clear delimiters such as XML tags?
- 5Explain in your own words
How does prompting a reasoning model differ from prompting a standard chat model?
Saved in this browser
- 6Recall
Describe the four-step loop for improving a prompt.
Saved in this browser
- 7Explain in your own words
Why is 'give the model an out' (e.g. permission to say "I don't know") an effective guardrail?
Saved in this browser