liveBook · Manning

Reasoning and planning

As explained in AI Agents in Action, Second Edition

Reasoning and planning is the process of deriving intermediate conclusions and organizing actions to achieve a goal.

A one-shot task may need only an answer. A multistep goal needs decisions along the way: what to do first, which action to take next, and whether the result changes the plan. Reasoning and planning give an agent that ability. They are a core functional layer alongside persona and tools and actions, not a fixed stage that runs once before tool use. section 1.3 section 11.1.3

Choose a small control pattern that fits the task:

  • For a nontrivial task, use ReAct: think, take an action, inspect its observation, and then decide what follows. section 11.1.3
  • For a larger goal, draft a short checklist before executing it. A sequential thinking server can provide a place to record the plan. section 11.1.3
  • Set a turn limit and stop condition. If the limit is reached, fail safely with a helpful follow-up status; review the answer before finishing. section 11.1.3 section 11.5

Plan, calculate, then check

A time-travel problem requires several jumps and stays before the agent can total the days lived through. Its instructions call for a plan, date-calculation tools, and reflection after each tool result. A worked plan totals two stays—6 days and 20 days—to reach 26 days. The intermediate steps make the final answer checkable rather than leaving it as an unsupported guess. section 5.4.3

Listing 11.3 03_reasoning_planning_tips.py
from typing import Literal
from pydantic import BaseModel
from agents import Agent, Runner
from agents.mcp import MCPServerStdio

MAX_TURNS = 3  #1

thinking_srv = MCPServerStdio(    #2
        name="sequential-thinking",
        params={
            "command": "npx",
            "args": ["-y", "@modelcontextprotocol/server-sequential-thinking"],
        },
    )

class Answer(BaseModel):
    status: Literal["ok", "needs_followup"] = "ok"
    checklist: list[str] = []
    summary: str
    sources: list[str]

instructions = f"""
You are a planner-executor.
If the message starts with 'PLAN ONLY', 
return only the checklist (no actions).
1) Plan→Execute: draft a concise 3–5 item checklist, then execute.    #2
2) ReAct: Thought→Action(tool)→Observation;    #3
inspect each observation before continuing.
3) Limit: stop after at most {MAX_TURNS} tool calls;    #1
if reached, set status='needs_followup' and return best effort.
4) Self-review: before final, catch obvious mistakes,     #4
ensure the question is answered, and cite sources.
Use seq_think between actions to decide the next step. 
Return Answer JSON only.
"""

async def main():
    agent = Agent(
        name="Planner", 
        instructions=instructions, 
        mcp_servers=[thinking_srv], 
        max_turns=MAX_TURNS,
        output_type=Answer)

    async with thinking_srv:
        question = "Summarize our refund policy and cite relevant internal docs."
        plan = await Runner.run(agent, f"PLAN ONLY. Question: {question}")
        print(plan.final_output.checklist)

        result = await Runner.run(agent, question)
        print(result.final_output)

if __name__ == "__main__":
    import asyncio
    asyncio.run(main())

The planning-agent listing shows a bounded implementation: a 3–5 item checklist, a thought-action-observation instruction, a three-turn limit, and a typed answer with status and sources. section 11.1.3

Do not add elaborate reasoning to every task: simple one-shot work may not need it. Built-in model reasoning is not a substitute for checking observations, limiting loops, and handling failure. Persona instructions can shape choices, while tool results feed back into the next decision. section 11.1.3 section 1.3

How AI Agents in Action, Second Edition covers it

How deep it goes
Develops practical prompt patterns, checklists, turn limits, and self-review rather than model reasoning internals.
Examples
Uses a tool-assisted time-travel calculation and a planning agent with a checklist, turn cap, and typed answer.
What you can do after
Build it from scratch as a bounded planning loop that checks tool results and returns a helpful status when it cannot finish.
How it connects
Connects ReAct and chain-of-thought prompting with persona, tool use, and evaluation and feedback.

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.