Agentic patterns: ReAct, planning and memory

  • Advanced
  • 30 min
  • Lesson 2 of 3 in Tools and agents
  • Last verified

Before you start

You will be able to

  • Describe the ReAct loop (thought, action, observation) and how it turns single tool calls into an agent.
  • Compare reactive step-by-step planning with plan-then-execute, stating the failure mode and cost of each.
  • Design stopping controls for an agent: a done criterion, step, time and cost budgets, loop detection and escalation to a human.
  • Distinguish short-term from long-term agent memory and name one privacy and one staleness risk of long-term memory.

Wick is loading the late express. Five jobs: check the tags, clear the aisle, weigh the sacks, seal the hold, sign the manifest. He has done three, and the person who wrote the list has gone to bed.

He looks at the sack barrow, then at the corridor. Then he starts again at the tags.

How does Wick know where he is on the list, and how will he know when it is finished?

Concept

An agentAgentA system in which the model decides its own steps and tools in a loop until a goal is met. It costs more than a fixed workflow and errors can compound.Open the glossary entry is a language model in a loop. On each turn it decides what to do next, calls a tool (see Function and tool calling), reads the result, and decides again, until it judges the task finished. The ReActReActAn agent loop of thought, action (a tool call) and observation (the result), repeated until done or a stopping control fires.Open the glossary entry pattern (Yao et al., 2022) names this loop: interleave a reasoning step (Thought), an Action (a tool call) and an Observation (the tool result), and repeat.

Why It Matters

Coding assistants, research agents and support agents that can look things up are all this loop with different tools and guard rails. The loop is simple. The engineering is in keeping it from running forever, running up a bill, repeating a mistake, or doing something irreversible.

Anthropic’s guidance on building agents draws a useful line. A workflowWorkflowAn LLM system that follows a path predefined in code. Prefer it when the path is predictable.Open the glossary entry orchestrates the model and tools through predefined code paths. An agent lets the model direct its own process and tool use. Agents suit open-ended problems where you cannot predict the number of steps, and they cost more and carry a risk of compounding errorsError compoundingIf each step succeeds with probability p, independently, n steps all succeed with probability p to the power n, so long chains fail more than single steps suggest.Open the glossary entry. Start with the simplest design that works: often a single model call with retrieval is enough.

Mental Model

A detective working a case. Think about what is known, take one action to learn more, observe the result, update the theory, repeat. The case ends when it is solved, when the budget runs out, or when the detective admits they need to ask the client. A detective with no budget and no stopping rule never closes the case.

Technical Deep Dive

The ReAct loop

The ReAct paper reports that interleaving reasoning traces with actions helps the model form and revise plans and handle surprises, and that it reduced hallucination and error propagation compared with chain-of-thoughtChain-of-thought (CoT)Having the model write intermediate reasoning steps before the answer. It can help multi-step problems, but it costs tokens and latency, and the written reasoning may not be what drove the answer.Open the glossary entry alone on question answering and fact verification (from the abstract, checked 2026-10-09). The paper’s prompts spelled out literal “Thought:” and “Action:” lines. Today many APIs implement the same loop through native tool calling, and reasoning may be hidden or returned in a separate field. Learn the loop as a structure, not as a prompt format to copy.

Every pass through the loop is another model call that carries the growing history, so latency and token cost grow with the number of steps. That is the main cost of the pattern, and the reason for the budgets below.

Planning: two ways to choose the next step

  • Reactive (ReAct-style): decide the next step from the latest observation. It adapts well when the world surprises you. The cost is more model calls, a risk of wandering, and a trace that is harder to predict or audit in advance.
  • Plan-then-executePlan-then-executeAn agent pattern that writes a plan first and then carries it out. It is predictable and reviewable, but brittle when the world deviates.Open the glossary entry: write a plan of subtasks first, then work through it. It is more predictable and easier to review or approve up front. It is brittle: if step 3 returns something the plan did not expect, blindly continuing makes things worse. Add checkpoints and let the agent replan when an observation contradicts the plan.

Many production systems blend them: plan briefly, execute reactively, and replan on surprise. Which to choose depends on how predictable the task is and how much a wrong step costs.

Stopping controls

An agent will not reliably end itself gracefully. Build the exits yourself.

ControlWhat it prevents
A clear definition of done (a checkable condition, not “when it feels finished”)Stopping too early or never
Step capUnbounded loops
Time and cost budgets (tokens or money)A slow, expensive run on a task that is not converging
Loop detection (the same tool with the same arguments again, or no new information across several steps)Repeating a failing call forever
Escalate to a human when ambiguous or blockedGuessing on an irreversible action
Tracing of every thought, call and resultDebugging after the fact

When a limit is hit, return a useful partial result and say why you stopped. A silent timeout teaches the user nothing.

Memory

  • Short-term: the current conversation and scratchpad, held in the context window. It is cheap and exact, and it is limited by context length, so long runs need trimming or summarising (see Context engineering and memory).
  • Long-term: facts and past interactions stored outside the model, usually in a vector store (Vector databases), and retrieved only when relevant, using the same retrieval ideas as RAG architecture and chunking.

Long-term memory brings risks. Privacy: it can store personal data, so decide what may be saved, who can read it, and how a user deletes it. Staleness: a remembered fact can become false (an address, a price, a policy), so store when it was learned and prefer fresh sources for facts that change. A retrieved memory is also untrusted text: if it was written from a web page, it can carry an injected instruction.

Diagram

Diagram: The agent cycles through thought, action and observation.

no

no

yes

yes

Start: goal

Thought: what should I do next?

Action: call a tool

Observation: tool result

Goal achieved?

Budget exhausted
or stuck in a loop?

Stop: return partial result and ask the user

Return final answer

Diagram summary: The agent cycles through thought, action and observation. After each observation it checks whether the goal is met; if not, it checks the step, time and cost budgets and loop detection, and either continues or stops, returning a partial result and asking the user.

Real-World Application

Coding agents (read a file, edit, run tests, observe the failure, fix) and research agents (search, read, search again, synthesise) are direct instances. In both, the practical work is the surrounding controls: sandboxed tools, approval before risky actions, budgets, and traces you can replay.

Interview Perspective

Expect “design an agent that books a flight” and “what happens if the agent calls the same tool repeatedly”. A strong answer names step and cost caps, loop detection, a clear done condition, escalation, and tracing, rather than assuming the model will work it out. Also be ready to say when you would not use an agent at all.

Practice Questions

These are generated practice questions, not reported from any company.

  • Basic: What are the three steps in one ReAct cycle?
  • Intermediate: What is the difference between plan-then-execute and reactive planning, and when does each fail?
  • Advanced: How would you detect that an agent is stuck in a loop, and what should happen then?
  • Product-Company: Design the stopping conditions for an agent that researches a topic and writes a summary. When should it stop searching?

Activity

Trace a ReAct loop by hand for the goal “find out if it will rain tomorrow in Mumbai and tell me”. Write each thought, action and observation. Then change one observation (the weather tool returns an error) and write what the agent should do next and which stopping control applies.

Homework

Design on paper the tool inventory and stopping controls for an “expense report assistant” that reads receipts and files a claim. Mark which tools are read-only and which need human approval, set a step cap and a cost budget (state your reasoning, the numbers are your choice), and say what long-term memory, if any, it should keep.

Quick Revision

  • Agent: a model in a thought, action, observation loop until done.
  • Reactive adapts but costs more calls and can wander; plan-then-execute is predictable and auditable but brittle.
  • Always build the exits: done criterion, step, time and cost caps, loop detection, human escalation, tracing.
  • Short-term memory is the context; long-term memory is retrieved from a store and brings privacy and staleness risks.
  • Start with the simplest design; use an agent only when the path cannot be predicted.

Check yourself

  • Checkpoint question: Compare ReAct and plan-then-execute for a customer-support agent. State when each fails and how you would cap a runaway loop.
  • Independent re-test: Add long-term memory. What do you store, how do you retrieve it, and what are the privacy and staleness risks?
  • Common misconception to avoid: An agent with no step cap, no tracing and no definition of done will end itself sensibly.

Practice

Test what you just read. Quizzes and cards are grouped by module: Tools and agents.

Check yourself9 questions on what this lesson set out to teach

Checking answers needs JavaScript. You can still read every question and option below.

0 of 9 answered

Keyboard: arrow keys move between options. Space or Enter selects the focused option.

ObjectiveDescribe the ReAct loop (thought, action, observation) and how it turns single tool calls into an agent.

  1. Question 1 of 9In the ReAct pattern, what does the Observation step contain?

    Choose one answer.

  2. Question 2 of 9A teammate says each extra agent step is nearly free because only the new tool call is billed. What is wrong with that?

    Choose one answer.

ObjectiveCompare reactive step-by-step planning with plan-then-execute, stating the failure mode and cost of each.

  1. Question 3 of 9A flight-booking agent has a fixed 5-step plan. At step 3 the airline API returns "sold out". What fails in a plain plan-then-execute design, and what fixes it?

    Choose one answer.

  2. Question 4 of 9An internal tool must follow an approved sequence of steps and be easy to audit before it runs. Which approach fits best, and what is the main cost?

    Choose one answer.

ObjectiveDesign stopping controls for an agent: a done criterion, step, time and cost budgets, loop detection and escalation to a human.

  1. Question 5 of 9In production an agent keeps calling search with the same query and gets the same empty result. Which control should catch this?

    Choose one answer.

  2. Question 6 of 9Which two belong in an agent's stopping controls?

    Choose all that apply.

  3. Question 9 of 9A filing agent must complete five steps: scan tags, clear the aisle, weigh sacks, seal the hold and sign the manifest. It reports "finished" after three, and nobody notices until the train leaves, with most of its step budget unused. Which change best prevents a repeat?

    Choose one answer.

ObjectiveDistinguish short-term from long-term agent memory and name one privacy and one staleness risk of long-term memory.

  1. Question 7 of 9Your support agent stores facts from past chats in a vector store. Which pair of risks does the lesson say you must design for?

    Choose one answer.

  2. Question 8 of 9Which statement correctly separates short-term from long-term agent memory?

    Choose one answer.