AI Agents · ReAct loop and core patterns
Why this matters
An LLM that can call tools is an LLM that can do work in the real world: search, write code, send emails, hit APIs. The pattern that makes this reliable is called ReAct (Reason → Act → Observe). It's the foundation of every agent framework, and you should understand it before reaching for one.
The ReAct loop
flowchart LR
Start([Start]) --> R[Reason
What do I do next?]
R --> A[Act
Call a tool]
A --> O[Observe
Read result]
O --> D{Done?}
D -->|no| R
D -->|yes| End([Answer])
The model produces a reasoning step ("I need to find the user's location to answer the weather question"), then a tool call (get_user_location), observes the result, then reasons again. Loop until the model decides it has enough to answer.
Failure modes of ReAct agents
- Infinite loops. Model keeps calling the same tool, or alternates between two without progress. Always cap loop count (e.g., max 10 iterations).
- Hallucinated tools. Model invents a tool that doesn't exist. Catch this server-side and return an error message — the model will recover.
- Wrong tool selection. Names too similar (chapter 6). Add disambiguating examples in the system prompt.
- Premature stopping. Model decides it's done before it actually has the answer. System prompt: "Before answering, verify you have all required information."
- Drift in long contexts. After 10+ iterations, context grows long; model forgets early instructions. Periodic re-prompting helps.
Memory: short-term vs long-term
- Short-term = the conversation history. Trimmed to fit context window. Often summarised when long.
- Long-term = persistent across sessions. Usually stored in a vector DB; the agent retrieves relevant memories at the start of each turn.
- Episodic = specific events ("on March 5th, user said X"). Useful for personalisation; needs careful privacy thinking.
- Semantic = facts the user has stated ("user prefers vegetarian food"). Often built up incrementally with an LLM call extracting facts after each turn.
Multi-agent patterns
| Pattern | Setup | Use case |
|---|---|---|
| Supervisor / worker | One agent plans + delegates; specialised workers execute | Complex tasks with distinct sub-skills (research, write, code) |
| Parallel workers | Spawn N agents in parallel, merge results | Map-reduce style work (analyse 100 docs) |
| Critic / reviewer | Generator produces; critic reviews and sends back fixes | Quality-critical generations (code review, content) |
| Debate | Multiple agents argue different positions; final synthesis | Reducing single-model bias; not yet widely deployed |
Frameworks: when to reach for one
You don't need a framework for simple ReAct loops — ~50 lines of Python do it. Reach for one when:
- You have multi-agent orchestration with shared state
- You need streaming + interruption + human-in-the-loop
- You need persistence + checkpointing of agent state across restarts
| Framework | Strengths | Best for |
|---|---|---|
| LangGraph | State machine for agents; checkpointing; human-in-the-loop primitives | Production agents with clear flows |
| CrewAI | Crew + roles abstraction; readable for product teams | Multi-agent collaboration patterns |
| AutoGen | Microsoft; conversation-driven multi-agent | Research and complex agent debates |
| smolagents | Hugging Face; minimal, code-first | When you want to understand every line |
When NOT to use agents
Agents are slow and expensive (one task = many LLM calls). If your problem is single-shot — "extract these fields from this PDF", "summarise this article" — a single prompt is faster, cheaper, more reliable. Agents are for tasks that require multi-step reasoning with tools, not for tasks where you'd like fancier output.
Takeaway
Agents are powerful for tasks that need real-world interaction and branching reasoning. They're overkill for single-shot work. Frameworks help when you have multi-agent state + checkpointing needs; for simple ReAct, a 50-line loop is clearer than any framework. Cap your iterations, return errors as data, and treat tool descriptions as your most important documentation.