Strip away the framework and an agent loop is a small, understandable piece of control flow — not magic.
1Why ReAct Specifically
ReAct (Reason + Act) interleaves the model's reasoning with real actions and their real observed results, rather than asking the model to plan every step upfront in one shot. Grounding each subsequent decision in a real observation — not a guess about what an action probably returned — is what keeps a multi-step agent from compounding an early wrong assumption through the rest of its run.
2The Step Cap Is a Safety Feature, Not an Afterthought
Nothing guarantees a model reliably produces a clean, parseable Final Answer every time — a bug in your parsing, an ambiguous prompt, or an unusual model response can all leave the loop with no exit condition. A hard max_steps cap is what turns 'this could theoretically run forever' into 'this fails safely and visibly after a bounded number of steps.'
3Step-by-Step Breakdown
Thought, Action, Observation. ReAct is the pattern behind most real agent loops: at each step, the model reasons about what to do next (Thought), names one real action to take (Action), and your code executes it and feeds the real outcome back in (Observation) — repeating until the model produces a Final Answer instead of another action.
Build the Real Loop Skeleton. fake_model_step stands in for a real model call, scripted to return one action step and then a final answer. Finish run_agent so that when it sees an Action line (not yet a Final Answer), it actually extracts the tool name and calls the real tool with it.
Why does run_agent cap its loop at max_steps instead of looping forever until it sees a Final Answer?
- →A model might never produce a clean Final Answer line — a hard step cap prevents a stuck agent from looping indefinitely and burning unbounded time and API cost.
- →Python's for loop syntax physically cannot run without an explicit upper bound.
One Tool Wired, a Whole Belt to Go. The loop can already run a tool and stop on a final answer — but it only knows about one tool so far. Next lesson: designing TriageAgent's full tool belt, the real actions it needs for this specific job.
Level Up 🚀
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1Surface the Step Count When an Agent Hits Its Cap
If an agent stops because it hit max_steps rather than reaching a real answer, report that distinctly ('stopped after 5 steps without a final answer') instead of presenting it identically to a successful completion.
"error: max steps reached" // distinct from a real answerSEO Implications
- 1
Target 'ReAct agent loop from scratch' and 'AI agent max steps limit' separately
Developers building their first loop search for the overall pattern and the specific safety-limit practice as distinct concerns.
Best Practices
Log Every Thought, Action, and Observation, Not Just the Final Answer
When an agent produces a wrong final answer, the intermediate steps are almost always where the actual reasoning error happened — without logging them, debugging becomes guesswork.
Frequent Bugs
Parsing the model's action line with a fragile, overly strict string match.
A real model's phrasing varies more than a scripted example — production agents typically use structured tool-calling (the exact mechanism covered in this platform's MCP Masterclass) instead of parsing free-form text, precisely to avoid this fragility.
Real-World Examples
Support Triage Loop
TriageAgent's real loop: check priority, decide whether to retrieve a policy document, decide whether to escalate — each step's real observation determines whether the next step is even needed, which a single completion can't replicate.
if priority == "high": retrieve_policy(); escalate()