Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Agentic AI Notes
Topic #203

Agent Loop

The agent loop is the iterative cycle at the heart of every agent: decide, act, observe, repeat — continuing until the goal is met, a stopping condition is hit, or a safety limit is reached. The observation from each step is what feeds back into the next decision, which is why this single loop covers what's sometimes described separately as a "feedback loop."

The Loop, Explicitly

iteration = 0
max_iterations = 10   # safety limit — never omit this in production

while iteration < max_iterations:
    decision = llm.decide_next_step(goal, context, history)

    if decision.type == "final_answer":
        return decision.content

    result = execute_tool_safely(decision.tool, decision.arguments)
    history.append({"action": decision, "observation": result})
    iteration += 1

# Loop exited without a final answer — this needs explicit handling,
# not a silent failure
return escalate_or_report_incomplete(history)

Why the Iteration Cap Is Not Optional

Without a hard maximum, a model that gets stuck — repeating a failing tool call, oscillating between two unhelpful actions — can loop indefinitely, burning cost and time with no progress. See Infinite Agent Loops. A production agent loop always has an explicit exit condition beyond just "the model said it's done."

What "Feedback" Means Here

Each observation becomes part of the context for the next decision — this is what lets an agent adapt: if a tool call fails or returns unexpected data, that information is available to the LLM when deciding its next move, rather than the agent blindly continuing a fixed plan regardless of what actually happened.

Practical Use Case

A data-analysis agent's second query is only decided after seeing the first query's results — the loop is what makes that adaptive, multi-step behavior possible, as opposed to a fixed workflow that runs the same two queries regardless of what the first one returns.

Common Mistakes

  • No maximum iteration count, or one so high it doesn't meaningfully protect against runaway cost
  • Not persisting the loop's history/state incrementally, so a crash mid-loop loses all progress instead of allowing resumption from a checkpoint (see State Checkpoints)
  • Silently returning nothing useful when the iteration cap is hit, instead of surfacing that the agent failed to complete the task

Interview Relevance

"How do you prevent an agent from looping forever?" is a very common practical question — a strong answer covers hard iteration limits, detecting repeated/unproductive actions, and timeouts, not just "hope the model stops on its own."

Practice Question

Write pseudocode for detecting that an agent has called the exact same tool with the exact same arguments three times in a row, and should stop instead of continuing.

Related Notes

Want to go beyond the notes?

Join CodingNow's Agentic AI course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →