Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Self-consistency and reflection prompting

After this lesson: I can decide when to trust a model's answer by voting over samples and checking drafts.

70 min lesson6 commandsAI coding agent lab: use opencode to explain and fix code, and check its work

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Self-consistency asks the same question several times

Self-consistency means asking a model the same question several times and keeping the answer most replies agree on. Reflection means checking a draft, saying what is wrong and writing it again.

They matter because one reply can be a slip. Asking five times shows whether the model is sure, and a check catches a draft that breaks a rule.

By the end of this lesson

Note

I can decide when to trust a model's answer by voting over samples and checking drafts.

Key words

WordWhat it means
Sampleone reply to a question asked again
Normaliseturn different spellings of an answer into one form
Agreementthe share of samples that give the top answer
Abstaingive no answer when agreement is too low
Reflectioncheck a draft, give feedback and write it again
Criticthe code or model that checks a draft

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A vote turns samples into a confidence

Ask the same question five times. Normalise each reply, so £8, 8 pounds and 8. all count as 8. Then count.

The top answer is your answer, and its share is the agreement. Four of five is 0.8. If the agreement is below a threshold, such as 0.6, abstain and send the question to a person. Low agreement is a sign the model is guessing.

Worked example: five samples, one vote

SamplesAfter normalisingTop answerAgreementDecision at 0.6
8, £8, 8 pounds, 6, 8.8, 8, 8, 6, 884 of 5 = 0.8answer 8
19, 21, 19, 27, 18the same192 of 5 = 0.4abstain

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Write the vote, part 1

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'const SAMPLES = [' '  [\"A bus ticket is 4 pounds and a return costs double. What does a return cost?\",[\"8\",\"£8\",\"8 pounds\",\"6\",\"8.\"]],' '  [\"A class of 23 has 4 students away. How many are in?\",[\"19\",\"21\",\"19\",\"27\",\"18\"]],' '  [\"A pizza has 8 slices and 5 are eaten. How many are left?\",[\"3\",\"3\",\"three\",\"3\",\"4\"]],' '];' 'const THRESHOLD = 0.6;' 'function normal(a) {' '  let digits = \"\";' '  for (const c of a) if (c >= \"0\" && c <= \"9\") digits = digits + c;' '  return digits === \"\" ? a.trim().toLowerCase() : digits;' '}' 'function vote(answers) {' '  const counts = {};' '  const order = [];' '  for (const a of answers) {' '    const k = normal(a);' '    if (counts[k] === undefined) { counts[k] = 0; order.push(k); }' '    counts[k] = counts[k] + 1;' '  }' '  let best = order[0];' > vote3.js"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write the vote, part 2

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' '  for (const k of order) if (counts[k] > counts[best]) best = k;' '  return { answer: best, share: counts[best] / answers.length };' '}' 'let calls = 0;' 'for (const [question, answers] of SAMPLES) {' '  calls = calls + answers.length;' '  const v = vote(answers);' '  const said = v.share >= THRESHOLD ? \"answer\" + \" \" + v.answer : \"abstain\";' '  console.log(question);' '  console.log(\"  \" + answers.join(\" | \") + \" -> \" + v.answer + \" \" + v.share.toFixed(1) + \" -> \" + said);' '}' 'console.log(\"Model calls\" + \": \" + calls);' >> vote3.js"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Vote over the samples

Command
docker exec -w /home/node/project agent node vote3.js

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

What the vote printed

The return ticket gets 8 at 0.8, so it answers 8. The class gets 19 at 0.4 and abstains, even though 19 is right: two of five is not enough to trust. The pizza gets 3 at 0.6 and answers 3.

Look at three in the pizza samples. It has no digits, so it stays as the word and is not counted with 3. The last line says Model calls: 15. Three questions cost five calls each.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Reflection checks a draft and tries again

Reflection is a loop. A writer makes a draft, a critic checks it and gives feedback, and the writer revises. The loop stops when the draft passes or after a few rounds.

The critic can be the model reading its own draft. Checks in code are more reliable: a length limit, a word that must appear, tests that must pass. Here the writer is a stand-in that gives its next draft each round.

Write the reflection loop, part 1

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'console.log(\"Task: an out of office line, at most 60 characters, that names Monday.\");' 'const DRAFTS = [' '  \"Thank you for your email. I am away this week and will reply soon.\",' '  \"Thanks for your email. I am away until Monday and will reply then.\",' '  \"Away this week. I will reply to you on Monday.\",' '];' 'const MAX = 60;' 'const MUST = \"Monday\";' 'const ROUNDS = 3;' 'function check(text) {' '  const notes = [];' '  if (text.length > MAX) notes.push(\"too long\" + \" (\" + text.length + \"/\" + MAX + \")\");' '  if (text.includes(MUST) === false) notes.push(\"missing\" + \" \" + MUST);' '  return notes;' '}' 'let done = false;' 'for (let r = 0; r < ROUNDS && r < DRAFTS.length && done === false; r = r + 1) {' '  const notes = check(DRAFTS[r]);' '  console.log(\"Round\" + \" \" + (r + 1) + \": \" + DRAFTS[r]);' > reflect3.js"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write the reflection loop, part 2

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' '  console.log(\"  \" + (notes.length === 0 ? \"passes every check\" : notes.join(\"; \")));' '  done = notes.length === 0;' '}' 'if (done === false) console.log(\"No draft passed: send it to a person.\");' >> reflect3.js"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Run the reflection loop

Command
docker exec -w /home/node/project agent node reflect3.js

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

What the reflection loop printed

RoundFeedback from the critic
1too long (66/60); missing Monday
2too long (66/60)
3passes every check

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think a model that checks its own work will always find its mistakes. A model can approve its own wrong draft. Use a check in code or a test wherever one exists, and cap the rounds.

Match each word to its meaning

Have a go

Loading…

Your answers are marked here and are not kept. An account keeps your marks.

Assignment: vote, decide and reflect

Write the vote and the reflection check from the lab in Python.

normalise(answer) keeps only the digits of an answer, in order. An answer with no digits is returned trimmed and in lower case. vote(answers) returns {"answer": ..., "share": ...}: the normalised answer given most often and its share rounded to 2 places. A tie goes to the answer seen first. No answers gives {"answer": "", "share": 0.0}.

decide(answers, threshold) returns the voted answer, or "abstain" when the share is below the threshold or there are no answers. first_passing(drafts, max_chars, must_have, rounds) returns the index of the first draft, among the first rounds, that is at most max_chars long and contains must_have, or -1.

Code: python
def normalise(answer):
    return None


def vote(answers):
    return None


def decide(answers, threshold):
    return None


def first_passing(drafts, max_chars, must_have, rounds):
    return None

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.