liveBook · Manning

Chain-of-thought prompting

As explained in Coding with AI

Chain-of-thought prompting is a prompting technique that guides an AI model through a problem using a sequence of intermediate reasoning steps.

Chain-of-thought prompting guides an AI through a complex task as a sequence of smaller, logical steps instead of requesting a single final answer. A useful prompt supplies the problem context, asks what to check first, and continues through decisions or evidence needed for the next step. This approach aims for clearer, more accurate responses while leaving the developer in control of the result. section 9.5.1 section 9.0

Build a chain-of-thought prompt around three working parts:

  • Sequential reasoning: divide the problem into manageable steps that can be addressed in order. section 9.5.1
  • Contextual awareness: provide enough task-specific information for the model to interpret each step and its constraints. section 9.5.1
  • Iterative refinement: evaluate an answer, add new information or corrections, and ask for the next revision. section 9.5.1 section 9.7

Diagnose a Python application in Docker

Rather than asking why an application crashes, lead the investigation: ask whether the container is running with `docker ps -a`; if stopped, ask how to inspect errors with `docker logs`; if running, ask how to enter it and inspect application logs; then verify the Python version, dependencies, and Dockerfile configuration. Each answer determines the next check, turning a vague failure into a testable debugging path. section 9.5.1

Stage an implementation decision

For a caching system, request the cache interface first, an in-memory implementation next, and eviction policies last. For an authentication design, ask for reasoning about JWT versus session-based authentication before requesting code. Staging work this way makes it easier to inspect and revise individual decisions. section 9.6.1 section 9.6.1

Figure 9.6 A chain-of-thought prompt gets the model to reason out a problem like a human would. You break down the problem and experiment with answers until you get the results you desire.
A diagram of a diagram

Description automatically generated

The diagram reinforces the workflow: break down the problem, experiment with answers, and refine until the result is useful. section 9.5.1

How Coding with AI covers it

How deep it goes
Defines sequential reasoning, contextual awareness, and iterative refinement, then demonstrates them with practical prompt sequences.
Examples
Uses Docker failure diagnosis, staged cache construction, and JWT-versus-session authentication decisions.
What it assumes
Assumes engineering fundamentals and familiarity with prompt types while using stepwise prompts to structure complex work.
What you can do after
Apply it to break complex technical tasks into testable steps and refine AI responses iteratively.
How it connects
Connects prompt engineering with recursive prompting, context manipulation, instruction refinement, and output control.

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 Coding with AI. The section links point to the parts of the book it draws on.