Lesson 1 of 3 Prompt Engineering

Writing Effective Prompts with Prompt Engineering Techniques

This lesson explains prompt engineering, designing the input to a foundation model so it produces better output. It is the fastest and cheapest way to improve results, so it should be tried first.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify the elements of a prompt: instruction, context, input data, output format and constraints.
  • Distinguish zero-shot, one-shot, few-shot and chain-of-thought prompting.
  • Apply good prompting practices to improve a prompt.

Elements of a prompt

A good prompt is built from distinct parts. The instruction says what to do, such as "Summarise this email in three bullet points". Context gives the model what it needs to do it well: a role, such as "You are a support agent for an airline", background information or retrieved documents. The input data is the text to work on. The output format says how the answer should look, such as JSON, a table or a word limit. A negative prompt, or constraint, says what to avoid, such as "Do not mention competitors". The following table summarises the elements.

ElementExample
Instruction"Summarise this email in three bullet points"
ContextA role ("You are a support agent for an airline"), background or retrieved documents
Input dataThe text to work on
Output formatJSON, a table, a word limit
Negative prompt (constraint)"Do not mention competitors"

The following list describes how system prompts and delimiters are used.

  • System prompts set persistent rules and a persona; user messages carry each request.
  • Delimiters such as XML tags around documents keep instructions separate from data.

The following diagram labels the elements of one prompt.

The elements of a prompt. Context sets a role and background, such as You are a support agent for an airline. The instruction says what to do, such as summarise this email in three bullet points. The input data is the text to work on, kept apart from the instructions with delimiters such as XML tags. The output format sets the shape of the answer, and a negative prompt or constraint says what to avoid.
Clear structure gives consistent answers.
Sort into groups

Parts of a prompt

Exam skills 3.2.1

Place each fragment of a prompt for an airline assistant in the element it belongs to.

Log in to try this activity Interactive activities are free with an account.

Prompting techniques

Prompting techniques differ in how many examples they give and how they guide the model. A zero-shot prompt asks with no examples, which works for simple, common tasks. A one-shot prompt includes one example, and a few-shot prompt includes several examples of input and desired output, which teaches the model a format or a set of labels without any training.

Chain-of-thought prompting asks the model to reason step by step before answering, which improves results on maths, logic and multi-step problems. Prompt chaining breaks a complex task into a sequence of prompts, each using the output of the one before. Prompt templates are reusable prompts with placeholders, such as {product} and {tone}, filled in for each request. The following table summarises the techniques.

TechniqueHow it works
Zero-shotAsk with no examples
One-shot (single-shot)Include one example
Few-shotInclude several examples of input and desired output
Chain-of-thought (CoT)Ask the model to reason step by step; improves maths, logic and multi-step problems
Prompt chainingBreak a task into a sequence of prompts, each feeding the next
Prompt templatesReusable prompts with placeholders such as {product} and {tone}

Examples teach a format and labels without any training, which is called in-context learning.

Sort into groups

Which technique?

Exam skills 3.2.2

Name the technique each prompt uses.

Log in to try this activity Interactive activities are free with an account.

Good practice

The quality of model responses depends heavily on how prompts are written. The following list describes recommended practices.

  • Be specific and clear; give context and describe the audience.
  • Provide examples and specify output format and length.
  • Ask the model to say "I do not know" when unsure.
  • Break complex tasks down.
  • Iterate, testing each change against sample inputs, and tune parameters such as temperature alongside the prompt.
Select all that apply

Better prompts

Exam skills 3.2.3

A team's prompt reads: "Write about our new product."

Log in to try this activity Interactive activities are free with an account.
Worked example

Improving a prompt step by step

A retailer wants to classify customer reviews. The first, zero-shot attempt is vague:

Classify this review: "Arrived late but the jacket is lovely."

The model replies with a paragraph of analysis. A better prompt adds a role, clear instructions, the allowed labels, few-shot examples and an output format:

System: You classify product reviews for an online clothing shop.

Classify the review into exactly one label: POSITIVE, NEGATIVE or MIXED.
Reply with the label only.

Review: "Terrible stitching, returned it."            Label: NEGATIVE
Review: "Perfect fit and fast delivery!"               Label: POSITIVE
Review: "Great colour, but the zip broke in a week."   Label: MIXED

Review: "Arrived late but the jacket is lovely."       Label:

The model now replies MIXED, consistently. The examples taught it the labels and format without any training (in-context learning). For a question needing several steps of reasoning, such as working out a refund amount, adding "think step by step" (chain-of-thought) would improve accuracy; for a long task, splitting it into a chain of smaller prompts would help.

Check yourself

What is few-shot prompting?

Show answer

Including a few examples of inputs and the desired outputs in the prompt, so the model follows the pattern without any training.

Exam Tips

Remember for the exam

  • A prompt with no examples is zero-shot, with one example one-shot, and with several examples few-shot.
  • To improve reasoning on multi-step problems, use chain-of-thought prompting.
  • A reusable prompt with variables is a prompt template.
  • Telling the model what not to do is a negative prompt, or constraint.
  • Persistent rules and a persona belong in the system prompt.
  • Try prompt engineering before RAG or fine-tuning.