Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Decision Loops
The decision loop is the repeating cycle at the heart of every agent. It connects goal, state, action, and observation into a continuous process that runs until the goal is complete.
8 min•By Priygop Team•Updated 2026
The Decision Loop in Detail
The Decision Loop in Detail
# Complete decision loop
def run_agent(goal, tools, max_steps=15):
# Initialise state
state = {
"goal": goal,
"history": [],
"step": 0,
"status": "running"
}
while state["step"] < max_steps and state["status"] == "running":
# Controller reads state and decides next action
decision = controller.decide(state)
if decision["action"] == "FINISH":
state["status"] = "complete"
state["result"] = decision.get("result")
break
if decision["action"] == "ESCALATE":
state["status"] = "escalated"
break
# Execute the selected tool
tool = tools[decision["action"]]
observation = tool.run(decision["arguments"])
# Update state with the observation
state["history"].append({
"step": state["step"],
"action": decision["action"],
"arguments": decision["arguments"],
"observation": observation
})
state["step"] += 1
return state