Sarghaby Araghatta
Home / Knowledge Base / Agents & the Agentic Loop
Agents & orchestration · AI Engineering

Agents & the Agentic Loop

An LLM given tools and a goal, run in a loop where it repeatedly chooses an action, sees the result, and decides the next step, rather than answering in one shot.

Set GoalModel ReasonsAbout StateEmit Tool CallExecute Tooland ReturnObservationUpdate Contextwith ObservationCheck Completionor Stop Condition
Agentic Loop Process Overview

How it works

In the agentic loop, the process starts with the model analyzing the current state of the task at hand. It generates a tool call based on this analysis, which is then executed by your runtime environment. The result of this execution, or observation, is crucial because it provides feedback that the model uses to inform its next action. This cycle continues, with each observation being added to the context for the next iteration. The loop will keep running until the model indicates that it has completed the task or until a predefined stop condition, such as reaching a maximum number of iterations or exceeding a budget, is met.

This dynamic interaction allows the model to adapt its strategy based on real-time feedback, making it capable of tackling complex problems that require multiple steps. For example, if the model is tasked with debugging a piece of code, it might first analyze the error message, generate a query to check the relevant documentation, and then use the information from that documentation to refine its next action. Each iteration builds on the last, creating a chain of reasoning that is informed by the results of previous actions.

The key here is that the model's ability to reason and adapt in real-time is what sets the agentic loop apart from traditional single-shot prompts. Without this iterative process, the model would lack the context needed to make informed decisions about subsequent actions, leading to suboptimal outcomes. The loop's flexibility allows it to explore various paths and adjust its approach based on the evolving situation.

  • Define the goal clearly.
  • Model reasons about the current state.
  • Emit a tool call based on reasoning.
  • Execute the tool and capture the observation.
  • Update context and check for completion.

The software-engineering analogue

You can think of the agentic loop as similar to a REPL (Read-Eval-Print Loop) or an event loop that drives a state machine. In these systems, each iteration processes input, executes a function, and returns output, creating a continuous cycle of interaction. However, the key difference lies in how the agentic loop operates. In this case, the transition function is dictated by the language model's judgment. This means that the same input can lead to different outcomes depending on the model's reasoning at that moment, making the path through the loop unpredictable.

Why not the obvious alternative

Many newcomers might initially think that a single prompt-response cycle would suffice for tasks that require multiple steps. However, this approach falls short when later steps depend on insights gained from earlier steps. For instance, if the model needs to troubleshoot a system, it cannot know the follow-up questions until it sees the initial results. A single prompt-response setup lacks the flexibility to adapt to new information, which is critical for complex problem-solving.

The agentic loop addresses this limitation by allowing the model to adjust its actions based on real-time feedback. However, this adaptability comes with costs. The nondeterministic nature of the loop can lead to compounding errors, where mistakes in earlier iterations affect later outcomes. Without constraints, the model could consume an unbounded number of tokens, leading to excessive resource usage. Therefore, it's essential to implement caps on iterations and establish guardrails to maintain control over the process.

When to use which

The agentic loop is best suited for open-ended, multi-step tasks where the path forward is not predetermined. Scenarios like research, debugging, or executing complex commands across systems benefit from this approach. The model can explore various avenues and adjust its strategy as new information emerges. Conversely, for tasks with a known sequence of fixed steps, a scripted workflow is a more efficient choice. It is cheaper and more reliable, as it eliminates the unpredictability inherent in the agentic loop.

Beware of Compounding Errors In an agentic loop, each step's output can affect the next input. Small errors can accumulate, leading to significant deviations from the intended outcome.

In production: when it breaks

The scenario A user request results in ~12 LLM calls and ~8 tool calls, leading to a total response time of ~25 seconds and a cost of ~$30 per interaction.
Action Selection AccuracyFeedback InterpretationGoal ClarityTool UtilizationAdjust Action Strategy
Debugging the Agentic Loop

Why it happens. The agent is not optimized for simple requests, leading to excessive looping and redundant calls. This occurs when the execution budget is not set, causing the agent to repeatedly call the LLM and tools without a clear stopping criterion or optimization strategy.

  • Measure the total number of LLM and tool calls per request.
  • Log the execution time and costs associated with each request.
  • Identify patterns of repeated tool calls and look for opportunities to cache results.
The call Implement execution budgets to limit the number of calls and tokens. Route simple requests to smaller models to reduce latency and cost. Monitor the number of calls, tokens used, latency, cost, and success rates to ensure efficiency and effectiveness going forward.

What it delivers

The agentic loop provides a level of autonomy in addressing genuinely open problems. By enabling the model to handle branching paths and adjust its approach dynamically, it can tackle challenges that it wasn't explicitly programmed to solve. This capability enhances the model's effectiveness in complex scenarios, allowing it to generate solutions that are not only relevant but also contextually aware.