A real agent isn't one tool call — it's a loop of reasoning, acting, and observing that repeats until the model has enough information to answer.
1ReAct Is Just a Loop With a Parser
Stripped of its impressive-sounding name, a ReAct agent is a plain loop: call the model, check if it's done, if not extract the requested action and run it, feed the result back in, repeat. The 'intelligence' is entirely in the model's reasoning at each step — the surrounding control flow is ordinary, inspectable Python.
2Why a Hard Step Limit Is Non-Negotiable
A model can get stuck reasoning in circles, never quite reaching a Final Answer — this isn't a hypothetical edge case, it happens in real agent deployments. Every real agent loop caps the number of iterations, guaranteeing termination and a bounded cost even in the failure case where the model never resolves the task cleanly.
3Step-by-Step Breakdown
A single tool call answers one question. A real LangChain AgentExecutor runs a loop: the model reasons (Thought), requests a tool (Action), your code runs it and reports back (Observation), and the model reasons again — repeating until it has enough to give a Final Answer.
This Thought/Action/Observation cycle is called ReAct (Reasoning + Acting). The control flow is just a loop: check if the model gave a Final Answer yet — if not, run the requested tool, feed the result back in, and let the model reason again.
Build the Agent Loop Yourself. llm_responses is a scripted 2-step reasoning trace, standing in for a real model's replies so this exercise is fully deterministic. Finish the loop body: when the response isn't a Final Answer yet, extract the ticker after "Action Input:", call get_stock_price with it, and print the observation.
Why does the agent loop check for max_steps as a hard limit, rather than looping until Final Answer appears with no bound at all?
- →Without a hard step limit, a model stuck in a reasoning loop that never produces a Final Answer would run (and cost money on) indefinitely — the cap guarantees the agent eventually stops even in that failure case.
- →It's required to make the loop execute faster.
You've built the exact control flow behind AgentExecutor: reason, act, observe, repeat, with a safety limit. Next: giving a real agent a choice between two different tools, and watching it pick the right one for the question asked.
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 Agent Reasoning Steps for Debuggability
Logging or displaying each Thought/Action/Observation step (as this exercise's print statements do) is essential for debugging agent behavior — treat this trace as first-class output, not something to suppress in production logs.
print(f'Step {i}: {thought}')SEO Implications
- 1
Target 'LangChain AgentExecutor loop explained' and 'ReAct agent pattern' as distinct searches
Developers specifically search for how the agent control flow actually works once they've used @tool and want to understand what orchestrates multiple tool calls.
Best Practices
Always Cap Agent Loops With a Hard Maximum Step Count
An agent loop with no iteration limit can run indefinitely (and rack up real API costs) if the model never produces a clean Final Answer — always enforce a maximum, and handle the case where it's reached gracefully rather than crashing.
Frequent Bugs
An agent loop with no step limit gets stuck reasoning in circles on an ambiguous question, running (and billing) far longer than intended.
Always set a max_steps cap and return a clear 'could not complete' message when it's reached, rather than looping unboundedly or crashing.
Real-World Examples
Multi-Step Research Task
An agent answering 'compare the stock prices of ACME and Widget Corp' runs the loop twice — once per lookup — accumulating both observations before producing a Final Answer that references both, exactly the reasoning pattern this exercise's loop generalizes to.
for step in range(max_steps): ... if done: return answer