29 — Applications

Agents & Planning#

ReAct · Tool Chains · MemoryPractice 1 min read
◆ The PatternAutonomous multi-step reasoning with tools and memory

An agent is an LLM that reasons about what to do, takes actions (tool calls), observes results, and iterates. The ReAct pattern (Reason + Act) interleaves thinking and tool use: Thought → Action → Observation → Thought → ...

ReAct loop: Thought → Action(tool, args) → Observation → Thought → ... → Final Answer
Each iteration: reason about what's needed, call a tool, process the result, decide next step.
Planning: Decompose task → subtasks → execute sequentially or in parallel
Complex tasks need planning: break "book a trip" into search flights, compare prices, book, confirm.

Memory types: (1) Short-term — conversation history in context window. (2) Long-term — vector store of past interactions, retrieved as needed. (3) Working memory — scratchpad for current task state. Key challenge: agents can be unreliable — they get stuck in loops, hallucinate tool calls, or lose track of the plan.

The best agent systems use simple, constrained loops — not complex multi-agent frameworks. Simple baselines are often hard to beat, and complex agent setups add cost (Kapoor et al. 2024).
Interactive — ReAct agent loop

Python — ReAct agent#

Pattern bridge: LLMs planning, tool-using, and looping is optimization made autonomous — each step refines the next. In markets, the sentiment cycle is an agent loop: observe, decide, act, observe again. Every tool an agent uses is reached through function calling.
Worked example

If each step of an agent is right 95% of the time, a 5-step task succeeds 77% of the time, 10 steps 60% and 20 steps 36%. Checking each step and retrying once on failure lifts 20 steps to 95%. Reliability compounds, so agents are built around verification, not just more reasoning.

Where it misleads
  • The retry calculation assumes failures can be detected; silent errors compound unchecked.
  • Agent benchmarks often ignore cost and over-fit to the benchmark (Kapoor et al. 2024); compare against simple baselines.
  • Every tool an agent can call widens what can go wrong; give it the least access that works.
In code
chain = {steps: round(0.95 ** steps, 3) for steps in (5, 10, 20)}   # each step right 95% of the time
retry = round((1 - 0.05 ** 2) ** 20, 3)          # 20 steps, each checked and retried once on failure
Python, in your browser — edit the code and run it again

Assumes import numpy as np. Configurations named after real models use their published shapes; other numbers are made up or simulated, as the comments say.

The same idea with a library
def react_agent(query, tools, max_steps=5):
    messages = [
        {"role": "system", "content":
         "You are a helpful agent. Use tools to answer questions. "
         "Think step by step."},
        {"role": "user", "content": query}
    ]
    for step in range(max_steps):
        response = client.chat.completions.create(
            model="gpt-4o", messages=messages, tools=tools)
        msg = response.choices[0].message

        if msg.tool_calls:
            messages.append(msg)
            for call in msg.tool_calls:
                result = execute_tool(call.function.name,
                                     json.loads(call.function.arguments))
                messages.append({
                    "role": "tool",
                    "tool_call_id": call.id,
                    "content": str(result)
                })
        else:
            return msg.content  # final answer
    return "Max steps reached"
Sources
  • S. Yao et al., “ReAct: Synergizing Reasoning and Acting in Language Models”, ICLR, 2023
  • N. Shinn et al., “Reflexion: Language Agents with Verbal Reinforcement Learning”, NeurIPS, 2023
  • S. Kapoor, B. Stroebl, Z. S. Siegel, N. Nadgir & A. Narayanan, “AI Agents That Matter”, arXiv:2407.01502, 2024

Last reviewed October 2026 · How this site checks its content · Report a mistake

Linked from 1
← Previous
Function Calling
Open in the full reader, with the topic sidebar