liveBook · Manning

Chain-of-thought prompting

As explained in AI Agents in Action, Second Edition

Chain-of-thought prompting is a technique that elicits or guides a model to produce or follow intermediate reasoning steps before a final answer.

Chain-of-thought (CoT) prompting asks a language model to work through intermediate steps before giving its answer. Instead of requesting only a result, give it a short reasoning procedure: identify the relevant facts, work through them in order, then check the result. This is useful when a plausible first answer may overlook part of a multistep problem. section 5.1.1 section 5.1

Count the time spent in the past

Suppose a traveler spends six days in one past year, jumps forward 50 years and spends another 20 days there, then jumps back to witness an event’s end. A quick answer might count only the first visit. A CoT prompt instead asks the model to map each jump, decide which stays count as being in the past, add their durations, and verify what it included. Both stays count, giving 6 + 20 = 26 days. section 5.1.1

Listing 5.2 01_demonstrating_CoT_reasoning.md (continued)
#With reasoning - append to prompt
CoT Reasoning Strategy
Step 1: Define present day as Year 0, past as negative years.
Step 2: Map traveler's journey: where he goes and how long stays.
Step 3: Identify which time periods count as "in the past." from the travelers perspective.
Step 4: Track days spent during each visit to past time periods.
Step 5: Add up all days spent in past before event ends.
Step 6: Verify calculation includes all past time, excludes present/future time.

A reusable six-step prompt for the time-travel problem, from defining the timeline through checking which days belong in the total. section 5.1.1

For a small agent, the instruction can be as simple as “Work out the solution step by step, then give the final answer.” In a year-jump example, the resulting steps subtract ten years from 2025, then add five, reaching 2020. The steps make the calculation inspectable; they do not guarantee it is correct. section 5.2.1 section 5.6

Listing 5.5 02_CoT_agent.py
cot_agent = Agent(
    name="TimeTravelerCoT",
    instructions=(    #1
        "You are a time travel problem solver. "
        "Work out the solution step by step, then give the final answer."
    ),
)

question = (    #2
    "Starting in 2025, you travel 10 years to the past, then 5 years to the future. "
    "What year do you end up in?"
)

result = asyncio.run(   #3
    Runner.run(    #4
        cot_agent, 
        input=question))
print(result.final_output)

OUTPUT
Let's solve this step by step:
1. **Starting Year**: 2025
2. **Travel 10 Years to the Past**:
   - 2025 - 10 = 2015
3. **Travel 5 Years to the Future**:
   - 2015 + 5 = 2020

A minimal OpenAI Agents SDK example showing where the CoT instruction goes and what the agent returns. section 5.2.1

Choose the amount of reasoning structure to match the task:

  • Use CoT for multistep questions with intermediate results. It can discourage shallow shortcuts, but it does not add reasoning ability to an underlying model. section 5.1 section 5.3
  • Skip explicit CoT for a reliable single-step lookup or fixed-format response; the extra steps cost tokens and latency. section 5.3 section 5.6
  • When the agent must call tools and react to their results, extend the approach with ReAct. Validate reasoning output before relying on it in later steps. section 5.6

How AI Agents in Action, Second Edition covers it

How deep it goes
Defines CoT, works through a six-step time-travel calculation, and implements a minimal agent instruction without exploring model internals.
Examples
Uses a 26-day time-travel riddle, a year-jump agent, and an exercise that requests numbered reasoning steps.
What it assumes
Introduces CoT in plain terms and supplies an agent example; assumes no prior familiarity with reasoning patterns.
What you can do after
Build it from scratch as a step-by-step prompt or agent instruction, then inspect and validate the answer.
How it connects
Connects CoT to agent planning, ReAct tool use, tree-of-thought search, Reflexion feedback, and sequential thinking.

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 AI Agents in Action, Second Edition. The section links point to the parts of the book it draws on.