Master

Reasoning techniques

Some tasks reward the model for thinking out loud. A few well-placed nudges, step by step, then check yourself, push accuracy on anything that needs logic.

What you will see

Few-shot reasoning: 2 + 3 = 5 -> 5 5 + 4 = 9 -> 9 9 + 1 = ? -> 10 (learned the pattern) Needs reasoning (math/logic): True Needs reasoning (lookup): False

The full result is shown at the end of the exercise below.

About 1 min of reading

Try It

Reason with zero-shot or few-shot techniques

1

Reason zero-shot, thinking step by step.

print() print("Reasoning techniques") print("Zero-shot: think step by step") def reason_step_by_step(steps): result = 0 for i, step in enumerate(steps, 1): result += step print("Step %d: %d" % (i, result)) return result total = reason_step_by_step([2, 3, 5]) print("Total: %d" % total)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Reasoning techniques Zero-shot: think step by step Step 1: 2 Step 2: 5 Step 3: 10 Total: 10
2

Reason few-shot from worked examples, and decide when reasoning is worth it.

examples = [ ("2 + 3 = 5", 5), ("5 + 4 = 9", 9), ("9 + 1 = ?", None), ] print() print("Few-shot reasoning:") for prompt, answer in examples[:-1]: print(" %s -> %d" % (prompt, answer)) print(" %s -> %d (learned the pattern)" % (examples[-1][0], examples[-2][1] + 1)) def needs_reasoning(task): return task in ("math", "logic") print() print("Needs reasoning (math/logic): %s" % needs_reasoning("math")) print("Needs reasoning (lookup): %s" % needs_reasoning("lookup")) print("Reason when it matters: True")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Few-shot reasoning: 2 + 3 = 5 -> 5 5 + 4 = 9 -> 9 9 + 1 = ? -> 10 (learned the pattern) Needs reasoning (math/logic): True Needs reasoning (lookup): False Reason when it matters: True
Reason when it matters, skip when it doesn't.

Data Flow

The model is prompted to generate reasoning, sometimes several times, before committing to an answer.
1 Ask the model to reason step by step
↓
2 Optionally run it several times
↓
3 Compare or vote on the answers
↓
4 Pick the most consistent result
Why this works
When to reason

Reasoning helps hard tasks but costs tokens on easy ones. Use it when:

  • The task needs logic or math.
  • Wrong answers are expensive.

Skip it for simple formatting or lookups.

Reason when it matters. Skip when it doesn't.
When you have a choice

Zero-shot vs few-shot reasoning

You can ask a model to reason in two ways:

Zero-shot:  "Think step by step."
Few-shot:   give worked examples that reason.

Few-shot reasoning examples are usually stronger because the model sees the exact reasoning style you want.

Examples

Think step by step

"Work through this one step at a time before answering."

Telling a model to reason improves math, logic, and planning tasks.

Why it works: generating intermediate steps gives the model more to check.

Self-consistency

answers = [prompt(input, temperature=0.8) for _ in range(5)]
winner = most_common(answers)

Running the same prompt several times and taking the majority answer reduces lucky mistakes.

Five thoughtful answers beat one quick guess.

Chain of thought

First list what you know,
 then what you need,
 then solve it.

Breaking the request into reasoning stages helps the model avoid jumps.

Why it works: explicit reasoning surfaces mistakes before the final answer.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Optimizing prompts
Previous Lesson
Optimizing prompts