REASONING FROM SCRATCH · CHAPTER 01

In-context learning &
chain of thought

A frozen model can reason in the prompt. Show it a pattern, or ask it to write the steps. No weight updates. This chapter is that cheap win.

~18 min read1 interactiveTested Python

What you'll learn

  • Why next-token models skip steps unless you make the steps visible
  • How in-context learning copies a pattern from the prompt
  • How few-shot and zero-shot chain of thought buy extra test-time compute
  • The four ways people build reasoners, and which ones need training

Before you start

  • What a decoder-only language model is
  • That generation is sampling, not a single argmax
  • The Training Ground if you want the pre-train / SFT backdrop

Write in the margins

Picture a clerk at a harbor office. You ask how many crates sit on three piers. If they blurt “38”, you cannot tell whether they added or guessed. If they write North, then East, then West, then the sum, you can catch a slip on the second line.

A language model is that clerk. It predicts the next token. It will give you a confident total either way. Chain of thought is the margin work: the model spends tokens on the steps so the final token is better conditioned.

Reasoning, in this series, means a visible intermediate trace that a checker could grade. The weights can stay frozen. Later chapters teach the model to write those traces on purpose. This chapter does it with the prompt alone.

Answering like a person is not thinking like one

Pre-training teaches a model to continue text. That is enough for “the capital of France is Paris”. It is not enough for a three-step crate count, a proof, or a program that has to pass tests. The model can sound sure and still skip a subtraction.

margins · ICL · CoT · frozen

MANIM ICL/CoT: margin notes in the prompt; weights stay frozen.

Here is the harbor puzzle this series will keep coming back to. Small numbers. You can do it in your head.

Pier North has 14 crates. Pier East has 6 fewer than North. Pier West has twice as many as East. How many crates in total?

  1. East is \(14 - 6 = 8\).
  2. West is \(2 \times 8 = 16\).
  3. Total is \(14 + 8 + 16 = 38\).

A standard prompt asks for the total and often gets a bare “38” — or a confident “42”. A reasoning prompt asks for the three lines first. The extra tokens are extra compute. That is test-time compute: you spend more decoding to raise the chance the last number is right.

What failedWhat it looks like
Hallucinated factInventing a fourth pier
Arithmetic slipWriting East as 20 instead of 8
Logic breakDoubling North instead of East
Premature lock-inNaming 38 in sentence one, then refusing to revise

The prompt can be the training set

In-context learning is pattern completion inside one forward pass. You put a few input–output pairs in the prompt. You do not touch the weights. The model continues the table.

A dock clerk keeps a log. We want three fields: ship, berth, cargo. Two worked lines, then a new one:

The prompt

Brig Aster left berth 4 at 05:10 with timber. → Aster | 4 | timber

Cutter Wren took berth 1 at 07:00 with ice. → Wren | 1 | ice

Barque Lark took berth 9 at 12:40 with salt. →

The completion

The model writes Lark | 9 | salt. Nobody fine-tuned it on dock logs. The format in the prompt is the format it continues.

That is few-shot in-context learning. Zero-shot is the same idea with no pairs — only an instruction. Both are frozen-weight tricks. They get sharper as models get larger, which is why 2022 papers called them “emergent”. Later work showed some of that sharpness is a metric artifact: a discontinuous plot can hide a smooth log-prob gain. Treat emergence as a 2022 snapshot, not a law of nature. Modern 1–8B models already ICL well when they are trained long enough.

Put the steps in the exemplars

Chain of thought is in-context learning where each exemplar includes the working, not just the final number. The 2022 few-shot recipe (Wei et al.) is: several question / reasoning / answer triples, then the new question. The model copies the shape of the working.

One exemplar, different numbers from the test puzzle so the model has to generalize:

A shed holds 9 tins. A cart holds 3 fewer. A crate holds twice the cart. Total?

Working: cart \(9 - 3 = 6\). Crate \(2 \times 6 = 12\). Total \(9 + 6 + 12 = 27\). Answer: 27.

Then you ask the harbor puzzle. The model should write East 8, West 16, total 38 — not 27. The exemplar taught the shape, not the answer.

Interactive · Same puzzle, two replies

How much extra text does the working actually add?

38 crates.

Count the words. The standard reply is 2 words. The working is 21 words — about ten times more generation. That is the compute you are buying. Three things make it help:

Decompose

One hard question becomes three easy ones.

Spend tokens

Each step is another forward pass. The model gets room to compute.

Isolate errors

A bad East is visible. A bare “42” is not.

On GSM8K, PaLM 540B went from 17.9% with a standard prompt to 56.9% with few-shot chain of thought. That is the number to remember. Smaller 2022-era models barely moved. That “scale gate” is history: today’s small open models already write steps. Do not quote “CoT needs 100B” as a current fact.

The code behind the puzzle

The same three lines the model is supposed to write. Change NORTH and the total follows.

harbor.py14, 8, 16 sum to 38
"""Harbor crate puzzle — the running example for chapters 01 and 02.

A reader can do every step by hand. The numbers are small and round.
North starts at 14. East is 6 fewer. West is twice East.
"""

# The three piers. Change these and the total changes with them.
NORTH = 14
EAST_LESS = 6


def east_crates(north=NORTH, less=EAST_LESS):
    """East is a subtraction away from North."""
    return north - less


def west_crates(east):
    """West is a doubling of East."""
    return 2 * east


def total_crates(north=NORTH, less=EAST_LESS):
    """Sum of the three piers. This is the answer we want the model to reach."""
    east = east_crates(north, less)
    west = west_crates(east)
    return north + east + west


# Frozen few-shot facts for the page. Standard prompting names the total.
# Chain-of-thought writes the three intermediate numbers first.
STANDARD_ANSWER = 38
COT_STEPS = (14, 8, 16, 38)

# The two replies shown on the page. Word counts are whitespace splits.
# Real tokenizers split some of these further; the ratio stays similar.
STANDARD_REPLY = "38 crates."
COT_REPLY = (
    "North 14. East 14 minus 6 is 8. "
    "West twice 8 is 16. Add 14, 8, and 16 to get 38."
)
STANDARD_WORDS = len(STANDARD_REPLY.split())   # 2
COT_WORDS = len(COT_REPLY.split())             # 21


if __name__ == "__main__":
    east = east_crates()
    west = west_crates(east)
    total = total_crates()
    assert east == 8 and west == 16 and total == 38
    assert COT_STEPS == (NORTH, east, west, total)
    print(f"North={NORTH} East={east} West={west} total={total}")
  • east_crates is the step models skip when they double North by accident.
  • The 21-word reply is the chain of thought. The 2-word reply is the standard answer.

One sentence can stand in for the exemplars

Few-shot CoT needs carefully written exemplars. Kojima et al. (2022) showed a shorter trick: append “Let’s think step by step.” The model then writes a trace, and a second prompt extracts the answer (“Therefore, the answer is”). Two stages. The first is allowed to ramble. The second is only allowed to name the number.

Question + “Let’s think step by step.”
A free-form trace (stage 1)
“Therefore, the answer is” + the number (stage 2)

Zero-shot CoT is brittle. Wording matters. It still fails when the model cannot do the underlying arithmetic. And a fluent trace is not proof the model used those steps — that is the faithfulness problem (Turpin et al. 2023, Lanham et al. 2023). The written reason can be a story it tells after it already picked an answer.

Prompting is only the first door

Once you have seen CoT work, the rest of the series is about making it reliable. Four families:

FamilyWhat you changeWhere it lives here
Inference-timePrompt, sample many, verify, searchThis chapter and chapter 02
Pure RLA verifier reward, no reasoning SFTGRPO, R1-Zero
SFT then RLA few traces, then RLR1, the capstone
DistillationTrain a small student on a teacher’s tracesThe last act of chapter 11

Chapters 03–09 are the RL tools that families 2 and 3 need. You can skip them if you only want prompting. You will want them the first time a verifier starts grading samples.

Mistakes learners make

“The model thought, so the answer is justified.” The trace can be unfaithful. Grade the final answer with a checker, not the story.

“CoT is an emergent cliff at 100B.” That was a 2022 picture of under-trained models. Small modern models write steps. Emergence plots are also sensitive to the metric (Schaeffer et al. 2023).

Reusing the test numbers in the few-shot exemplar. Then you have not tested reasoning. You have tested copy-paste. The shed uses 9 and 3. The harbor uses 14 and 6.

Counting only the last token as compute. A 21-word trace is roughly ten times the 2-word reply. Attention cost grows faster than that, so the real bill is higher.

What to remember

  • A reasoning model is one that writes a checkable trace, not just a fluent final sentence.
  • In-context learning is few-shot pattern completion with frozen weights. Chain of thought puts the working in those shots.
  • Zero-shot CoT is the same idea with one trigger sentence and a second extraction step.
  • PaLM 540B on GSM8K: 17.9% → 56.9% with few-shot CoT. The harbor total is 38.

Check yourself

1. Pier North has 14. East is 6 fewer. West is twice East. What is the total, and what are the two intermediate numbers?

East 8, West 16, total 38.

2. Why put a different puzzle in the few-shot exemplar?

So the model has to copy the method, not the number. The shed totals 27. The harbor totals 38.

3. What does “frozen weights” mean here?

No gradient step. The only new information is text in the prompt.

4. Name one reason a written trace can still be wrong as an explanation.

Faithfulness: the model can pick an answer and then invent steps that do not match how it got there.

5. Which of the four families needs no training at all?

Inference-time: prompting, sampling, voting, search. That is this chapter and the next.

Go to the source

Keep going