The Automated agent_loop() Controller
~20 min read
Replace the human controller with a Python function that parses the agent's Thought/Action/Answer output, runs the right tool via regex extraction, and keeps looping until a final Answer appears.
The Automated agent_loop() Controller is a Pro topic
Sign in, then upgrade to Pro or Power to unlock this topic and the full AI Engineering curriculum.
Key points
- •agent_loop() automates exactly what the human did in the manual trace: read output, act, feed back an Observation, repeat
- •current_prompt and previous_step are the loop's state — tracking what to send next and what stage was last seen
- •A regex extracts the tool name and argument from lines like 'Action: lookup_population: India'
- •The tools dictionary's keys must exactly match what the model writes in its Action lines, or the lookup fails
- •An 'Answer:' line in the response is the signal to stop looping and return the final result