Advanced prompting techniques

Few-shot examples, step-by-step reasoning you can check, structured output as tables and JSON, constraints, checklists, self-review and reusable templates — and the cases where no prompt can help.

  • Intermediate
  • 20 min
  • 12 questions
  • Lesson 5 of 16

In this lesson you will learn

  • Write few-shot prompts whose examples fix the format and cover tricky cases
  • Ask for step-by-step working and check every step yourself
  • Get structured output (tables, JSON) with a clear schema and validate it
  • Recognise common failure modes and the tasks that prompting alone cannot fix

Aysel edits her school's online newspaper. Every week about 60 messages arrive from readers, and she uses an AI assistant to sort them into categories. With a simple prompt the results change from week to week: sometimes the model invents a new category, sometimes it writes long explanations instead of one word, and the format is different every time. In the lesson “Writing good prompts” you learned the five building blocks of a prompt. Here you will learn the techniques that make answers reliable, repeatable and easy to check.

Few-shot: teaching with examples

Few-shot promptDefinition

A prompt that contains a few solved examples — an input and the desired output — before the new task. The model continues the pattern. A prompt without examples is called zero-shot; one with a single example, one-shot.

Examples work because a language model continues patterns: if three lines look like “message → category”, the most probable next line follows the same pattern. One good example fixes the format better than a paragraph of instructions. But examples are strong signals in another way too: the model copies everything in them, including accidental features.

  • 2–5 examples are usually enough: more examples cost more tokens and rarely help with a simple task.
  • Vary them. Cover different cases, including a tricky one (a message that mixes two topics).
  • Balance the answers. If all the examples have the same label, the model leans towards that label.
  • Keep one exact format in every example, and end the prompt with the new input in that same format.
  • Check that the examples are correct: the model copies mistakes as faithfully as good answers.

Zero-shot

Sort this reader message into a category:
"The pizza in the canteen was cold again."

Few-shot: three examples

Sort reader messages into ONE category:
canteen, events, sport, other.
Answer with the category only.

Message: "When does the chess club meet?"
Category: events

Message: "Our team lost 2:3, but what a match!"
Category: sport

Message: "Can the library stay open until 18:00?"
Category: other

Message: "The pizza in the canteen was cold again."
Category:
The examples fix the list of categories, the one-word answer and the format. The last line is left open for the model to complete.

Find the problem in the examples

Problem

Each few-shot prompt below has a flaw. Name it and fix it.
1) All four examples are labelled “complaint”.
2) Two examples answer “Category: sport”, one answers “It is about sport.”
3) The examples show only short, clear messages, but real messages are long and mix topics.

Show solutionHide solution

Solution

1) Unbalanced labels: the model will lean towards “complaint”. Use examples with different labels.
2) Inconsistent format: the model may answer in either style. Make every example use exactly “Category: …”.
3) The examples do not look like real inputs. Add one long, mixed message and show which category wins (for example, the category of the main request).

Sign up free to keep reading

The rest of the lesson, the quiz and your notes open with an Educora account. Signing up takes a minute, and your progress is saved on all your devices.

Step-by-step reasoning, and checking it

A model writes its answer token by token. If you ask for the answer straight away, its very first tokens are already the result — before any working has been written. When you ask it to work through the steps first, the intermediate results appear in the text and the model can build on them. For multi-step problems — arithmetic, logic, planning — this often reduces mistakes. Many current models have a built-in “thinking” mode that does this automatically; asking for the steps is still useful, because you can see them and check them.

Answer only

3 notebooks cost 1.20 manat each and 2 pens
cost 0.45 manat each. There is a 10% discount
on the total. How much do I pay? Answer only.

Steps, answer, check

3 notebooks cost 1.20 manat each and 2 pens
cost 0.45 manat each. There is a 10% discount
on the total. How much do I pay?

Work step by step, one calculation per line.
Then write the result on its own line as
"Answer: ... manat".
Finally, check the answer with a rough estimate.
Correct working: 3 × 1.20 = 3.60; 2 × 0.45 = 0.90; total 4.50; the 10% discount is 0.45; you pay 4.05 manat. Rough check: “a little less than 4.5” — it fits.

Check the model's working

Problem

A model wrote:
Step 1: 3 × 1.20 = 3.60
Step 2: 2 × 0.45 = 0.80
Step 3: 3.60 + 0.80 = 4.40
Step 4: 10% of 4.40 = 0.44; 4.40 − 0.44 = 3.96
Answer: 3.96 manat
Find the mistake and the correct answer.

Show solutionHide solution

Solution

Step 2 is wrong: 2 × 0.45 = 0.90, not 0.80. Every later step used the wrong number, so the error spread.
Correct: 3.60 + 0.90 = 4.50; 10% of 4.50 = 0.45; 4.50 − 0.45 = 4.05 manat.
Lesson: the steps did not make the answer right — they made the mistake easy to find.

Break big tasks into sub-tasks. A single prompt like “make me a revision plan for my exams” mixes planning, estimating and scheduling. Results are better when you split the work and check each part before the next one: the output of one prompt becomes the input of the next. This is called prompt chaining.

  1. Collect

    Give the model the list of topics from your syllabus and ask it to group them into 4–6 blocks. Check the grouping yourself.

  2. Estimate

    For each block, ask how long revising it usually takes, then adjust the times to how well you know each topic.

  3. Schedule

    Ask for a 3-week timetable as a table that uses only your free hours (list them).

  4. Review

    Ask the model to check the timetable against your rules (no more than 2 hours a day, one rest day a week) and to list every problem.

Structured output: tables and JSON

When people read the answer, a table is often the clearest format: say which columns you want, in which order, and what one row stands for. When a program reads the answer — for example, to save Aysel's 60 categories into a spreadsheet — ask for JSON.

JSONDefinition

A simple text format for data. An object is written in curly braces as "key": value pairs, a list in square brackets: {"title": "Cold pizza", "urgent": false}. Almost every programming language can read it.

  • the exact schema: field names, types (text, number, true/false) and allowed values;
  • a sample of one correct object;
  • what to do when information is missing — for example null, never a guess;
  • the instruction to return only the JSON, with no text before or after it.

No format

Give me information about
the messages from our readers.

Schema, sample, rule for missing values

For the messages below, return ONLY a JSON list.
Each item must have exactly these fields:
  "title":    text, at most 5 words
  "category": one of "canteen", "events",
              "sport", "other"
  "urgent":   true or false
If a value is unclear, use null. Do not guess.

Example item:
{"title": "Cold pizza", "category": "canteen",
 "urgent": false}

Messages:
###
[paste the messages here]
###
The delimiters (###) separate the messages from the instructions; the schema lets a program read the answer automatically.

Even with a perfect prompt, check the result before you use it: a model can add a sentence before the JSON, forget a field or invent a category. Many AI services offer a special JSON or “structured output” mode that forces the answer to follow a schema — when it is available, use it. A few lines of code catch the rest:

Python
import json

SCHEMA = {"title": str, "category": str, "urgent": bool}
CATEGORIES = {"canteen", "events", "sport", "other"}

def check(reply):
    try:
        data = json.loads(reply)
    except json.JSONDecodeError:
        return "not valid JSON"
    problems = [f"missing: {k}" for k in SCHEMA if k not in data]
    problems += [f"wrong type: {k}" for k, t in SCHEMA.items()
                 if k in data and not isinstance(data[k], t)]
    if data.get("category") not in CATEGORIES:
        problems.append(f"unknown category: {data.get('category')}")
    return problems or "OK"

print(check('{"title": "Cold pizza", "category": "canteen", "urgent": false}'))
print(check('{"title": "Cold pizza", "category": "food", "urgent": "no"}'))
print(check('{"title": "Chess club", "category": "events"}'))
print(check('Sure! Here is the JSON: {"title": "Cold pizza"}'))
Expected output
OK
['wrong type: urgent', 'unknown category: food']
['missing: urgent']
not valid JSON
The program accepts the first reply and catches three typical faults: a wrong type plus an invented category, a missing field, and extra text that breaks the JSON.

Constraints, checklists, self-review and templates

Role and audience do different jobs. The role sets the expertise and the tone (“You are an experienced chemistry teacher”); the audience sets the level (“for a grade 7 student”, “for parents who are not experts”). Change only the audience and the same explanation becomes simpler or deeper. A role does not give the model knowledge it lacks: writing “You are a doctor” does not make medical answers reliable.

Constraints are measurable limits: “at most 120 words”, “exactly 3 examples”, “use only the text between ###”, “do not mention brand names”, “if you are not sure, write unknown”. A constraint you can check is better than a wish: “short” is vague, “at most 120 words” is not.

Self-review. After the answer, give the model a checklist and ask it to check its own work: “Check your answer against these rules: … List every rule that is broken, then write a corrected version.” This often catches a broken format, a missed requirement or a slip in a calculation. It does not catch facts the model does not know: a model checking a date it invented will often “confirm” it. Facts are checked against real sources.

No check

Write 5 quiz questions about the water
cycle for grade 6.

Self-review with a checklist

Write 5 quiz questions about the water
cycle for grade 6.

Then check them against this list:
1. Each question has exactly one correct answer.
2. No word is too hard for a grade 6 student.
3. Together the questions cover evaporation,
   condensation, precipitation and collection.
List every problem you find, then give the
corrected questions.
Every item on the checklist can be checked. Use the same list afterwards to check the answer yourself.

Templates. When a prompt works, save it with placeholders and reuse it: “You are a {role}. Explain {topic} to a grade {grade} student in {count} bullet points.” Teachers, editors and programmers keep libraries of such templates, and a program can fill in the placeholders automatically.

Interactive

Loading simulation…
Turn this into a reusable template: keep the role and add a context (grade, purpose), a format (for example a table or 4 bullet points), a measurable constraint and one example. Nothing is sent anywhere.

When prompting is not enough — and a mini project

Good prompts make answers clearer and more consistent, but some problems cannot be fixed with wording:

  • Missing knowledge. A model does not know events after its knowledge cutoff or your private documents. Give it the documents or use a search tool (see the lesson “Retrieval-augmented generation (RAG)”).
  • Exact calculation on many numbers. For long sums, statistics or a whole table, use a spreadsheet or code — or a model that can run code.
  • Guaranteed facts. No prompt makes a model always right; “be accurate” in a prompt does not check anything.
  • A task that is unclear to you. If you cannot say what a good answer looks like, the model cannot either.
  • High-stakes decisions. Medical, legal and financial decisions and grades need a responsible person, not just a better prompt.

Mini project: Aysel's message sorter

Problem

Build a reliable prompt that sorts readers' messages for the school newspaper, and test it.

Show solutionHide solution

Solution

1. Role and audience: “You sort messages for the editors of a school newspaper.”
2. Rules: 4 categories (canteen, events, sport, other); a message with two topics gets the category of its main request; urgent = true only for safety problems or deadlines within 2 days.
3. Few-shot: 4 examples with different categories, one of them a mixed message.
4. Output: only a JSON list with the fields title, category, urgent; null if unclear.
5. Test: run the prompt on 10 old messages whose correct categories you know, validate the JSON with code and count the mistakes.
6. Iterate: for each mistake add a rule or an example, then test again on the same 10 messages — that is how you see whether the change helped. How to measure this properly is the subject of “Evaluating AI outputs”.

Exercise

Fill the reusable template for both topics in the list and print the two prompts. Use template.format(...) with the role “patient physics teacher”, grade 8 and 4 bullet points.

Exercise · Python
template = ("You are a {role}. Explain {topic} to a grade {grade} student "
            "in {count} bullet points.")
topics = ["inertia", "friction"]
# print the filled template for every topic
Expected output
You are a patient physics teacher. Explain inertia to a grade 8 student in 4 bullet points.
You are a patient physics teacher. Explain friction to a grade 8 student in 4 bullet points.

Remember

  • Few-shot examples fix the format; make them varied, balanced, correct and identical in layout.
  • Ask for the steps before the answer and check them yourself: fluent reasoning can still be wrong.
  • Split big tasks into a chain of smaller prompts and check each result before the next step.
  • For structured output give a schema, a sample and a rule for missing values, then validate the result.
  • Self-review against a checklist catches format slips, not unknown facts.
  • Prompting cannot add missing knowledge or guarantee facts: give documents, use tools, verify.
Spotted a mistake or something inaccurate in this lesson?

Check yourself

12 questions. Every correct answer earns XP.

Question 1 / 12

Question 1 / 12. What is a few-shot prompt?