liveBook · Manning

Chain-of-thought prompting

As explained in Deep Learning with Python, Third Edition

A search-based inference approach that explores reasoning in natural language by generating intermediate thought steps before an answer.

Chain-of-thought prompting encourages a language model to write intermediate steps before giving an answer. A prompt can provide worked examples or simply ask the model to “think step by step”; the generated steps can help it reach a correct solution. section 16.4.4

At test time, systems can also search through possible reasoning steps in natural language, a form of test-time adaptation. This differs from fine-tuning, which updates model weights using generated answers. section 16.4.4 section 19.3.6

>>> gemma_lm.generate(PROMPT_TEMPLATE.format(prompt))
<start_of_turn>user
Judy wrote a 2-page letter to 3 friends twice a week for 3 months.
How many letters did she write?
Be brief, and add "ANSWER:" before your final answer.<end_of_turn>
<start_of_turn>model
Here's how to solve the problem:

* **Letters per week:** 3 friends * 2 letters/week = 6 letters/week
* **Letters per month:** 6 letters/week * 4 weeks/month = 24 letters/month
* **Letters in 3 months:** 24 letters/month * 3 months = 72 letters
* **Total letters:** 72 letters * 2 = 144 letters

ANSWER: 144<end_of_turn>
>>> gemma_lm.generate(PROMPT_TEMPLATE.format(prompt))
<start_of_turn>user
Judy wrote a 2-page letter to 3 friends twice a week for 3 months.
How many letters did she write?
Be brief, and add "ANSWER:" before your final answer.<end_of_turn>
<start_of_turn>model
Here's how to solve the problem:

* **Letters per week:** 3 friends * 2 letters/week = 6 letters/week
* **Letters per month:** 6 letters/week * 4 weeks/month = 24 letters/month
* **Total letters:** 24 letters/month * 3 months = 72 letters

ANSWER: 72<end_of_turn>

In the Judy letter problem, one generated response lays out its arithmetic but incorrectly counts the pages in each letter as additional letters. Intermediate steps make the mistake visible; they do not guarantee a correct answer. section 16.4.4

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains few-shot and zero-shot prompting, gives a Gemma example, and relates the method to fine-tuning without detailing model internals.
Examples
Uses worked math demonstrations, “Let’s think step by step,” and Judy’s letter-counting problem.
What you can do after
Apply it by adding worked reasoning examples or a step-by-step cue to a prompt, then check the final answer.
How it connects
Connects to supervised fine-tuning, reinforcement learning from human feedback, test-time adaptation, and program synthesis.

Also explained in

Compare how 18 books explain chain-of-thought prompting →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.