The Essence of AI Agents: One Loop, Five Core Steps

The core of an AI Agent is a loop of no more than five lines of code: the model calls tools and iterates until it reaches an answer.
The article reveals that the essence of an AI Agent is a simple loop: send the conversation history to the large model and check whether it requests a tool call; if not, return the answer; if so, execute the tool, append the result to the conversation history, and loop again. Using the example of "calculating 15% of an order," it demonstrates a three-turn loop: retrieve the order amount, call the calculator, and output the result directly. The loop may run indefinitely if the model gets confused, so frameworks provide a max turn setting to limit the number of rounds. The three elements of a true Agent are: a model, some tools, and a loop. This simplicity makes Agents easy to implement, debug, and extend, while the max turn mechanism reflects a consideration for reliability.
Demystifying AI Agents
AI Agents sound like magic, but their core is astonishingly simple: one loop, and the core logic is no more than five lines of code. According to an analysis video from YouTube, this loop works as follows: send the conversation to the model; if there is no tool request, return the answer directly; otherwise, run the tool, add the result to the conversation, and loop again. This simple mechanism underpins every system called an "Agent."
The video illustrates this process with a concrete example: suppose you ask, "What is 15% of my last order?" On the first turn, the Agent retrieves the order information and gets $240; on the second turn, it calls a calculator and gets $36; on the third turn, no tools are needed, and it returns the answer directly, ending the loop.

AI Agent refers to an intelligent system capable of autonomously perceiving its environment, making decisions, and taking actions to achieve specific goals. In the era of LLMs (large language models), an Agent usually refers to a system centered on a model that extends its capabilities and completes complex tasks by invoking external tools (such as APIs, databases, calculators, etc.). Unlike traditional chatbots, Agents possess autonomy and tool-use capabilities, and can dynamically plan steps according to task requirements rather than merely generating text responses. The core idea is to treat the model as a reasoning engine that solves problems step by step through iterative looping—this is precisely the foundation of the "five lines of code loop" described in the article.
The Core of the Loop: The Logic of Five Lines of Code
The logic of this loop can be summarized in five steps:
- Send the current conversation history to the large model.
- Check whether the model requests a tool call.
- If there is no tool request, return the model's answer and end the loop.
- If there is a tool request, execute the corresponding tool.
- Append the tool execution result to the conversation history, then return to step 1.
These five steps constitute a complete Agent behavior framework. No matter how complex an Agent appears, this simple loop drives everything underneath.

Tool Calling / Function Calling is a key capability of large language models, allowing the model to output structured requests when generating text, specifying the name and parameters of the tool to be called. The model itself does not execute tools; instead, an external system (the Agent framework) parses these requests, actually runs the tools (such as querying a database or calling a calculator), and appends the results as text to the conversation history for the model to use in the next loop. This mechanism combines the model's reasoning ability with the execution capability of external systems, enabling the Agent to break through the limitations of pure text generation and complete tasks that require real-time data or precise computation. The accuracy of tool calling depends on the model's understanding of the task and the clarity of the tool descriptions.
Understanding the Loop Process Through an Example
Let's follow the example in the video to see how the loop advances step by step. The user's question is "What is 15% of my last order?"
First turn: After receiving the question, the model determines that it needs to retrieve order information and requests a call to the order query tool. The system executes the tool and returns an order amount of $240. This result is added to the conversation history.
Second turn: Based on the new conversation history, the model realizes it needs to calculate 15% and requests a call to the calculator tool. The system performs the calculation and returns $36. The result is again added to the conversation history.
Third turn: The model now has sufficient information (the order amount and the calculation result), no longer needs to call any tools, and directly generates the final answer "$36." The loop ends.

Potential Risks and Countermeasures
Although this loop is powerful, it has one obvious flaw: a confused model may fall into an infinite loop, repeatedly calling tools or making requests without ever reaching a final answer. The video notes that this is why Agent frameworks typically allow you to limit the number of loops through a setting called "max turn."
For example, you can set max turn to 5, meaning the Agent can perform at most 5 loops, after which it will be forcibly stopped regardless of whether an answer has been reached. This prevents resource waste and infinite loops.

Max turn is a common protective parameter in Agent frameworks, used to limit the maximum number of iterations in a single task. Because the model may fall into a loop of repeated tool calls due to misunderstanding, abnormal tool returns, or ambiguous tasks, max turn prevents resource exhaustion (such as API call costs and computation time) and system freezes by forcing termination. Typically, when the maximum number of turns is reached, the framework returns the information obtained so far or throws an error indicating that the task was not completed. Setting max turn reasonably requires balancing task complexity and reliability: too small a value may prevent complex tasks from being completed, while too large a value may waste resources. Some advanced frameworks also combine timeout mechanisms and loop detection (such as repeated identical tool calls) to further enhance robustness.
How to Identify a True Agent
Next time you see a product marketed as an "Agent," simply look for three components: a model, some tools, and a loop. If any one of these is missing, it may not be a true AI Agent.
This simple framework helps us understand the essence of Agents and enables us to more rationally evaluate the various products on the market that claim to be Agents. A true Agent does not require complex magic; it is simply a carefully designed loop that allows the model to solve problems step by step with the help of tools.
From an engineering perspective, this simplicity is precisely what makes Agents powerful: they are easy to implement, debug, and extend. The mechanism for limiting the number of loops reflects the consideration of reliability in practical applications. Understanding this allows both developers and users to see the capabilities and boundaries of AI Agents more clearly.
Related articles

world2vec: Building Mechanistic World Models with Latent Dynamics Learning
world2vec proposes a reward-free method for learning mechanistic world models via latent dynamics, faithfully reproducing systems over long horizons with at least an order of magnitude fewer parameters than DreamerV3, TD-MPC2, and LeWM, and achieving accurate system identification on 71 attractor benchmarks.

A New KL-DRO Paradigm: Adaptive Scenario Generation Around a Learned Density Model
KL-DRO research proposes centering the KL ball on a learned density model instead of the empirical distribution, with adaptive scenario generation guiding sampling toward the worst case. Experiments show lower cost under distribution shift when records are scarce and the model can extrapolate, with comparable in-distribution cost.

ODE Samplers for Discrete Diffusion Models: Exponential Residual Flows Accelerate Generation
Discrete diffusion models lack deterministic ODE sampling? This article introduces Exponential Residual Flows (ERFs), which introduce continuous residual variables per token to construct piecewise ODEs for deterministic sampling and support consistency distillation acceleration. Superior to prior methods on code and text generation tasks.