All Concept Bites
15 views
AI & Machine Learningintermediate

What Makes an LLM an Agent: The Loop, Not the Model

An agent is a model wrapped in a loop that calls tools and feeds results back.

Reviewed by Abhijith N M · 24 October 2026 · Editorial standards

The Simple VersionNo jargon

You call a language model (LLM) with a message. It writes back text. That is all the model does. It does not run code, read files, or check its own answer. An agent is different because a small program, called a harness, sits around the model and repeats a cycle. Here is the cycle. You send a message. The model replies with a tool call, which is a request to run a function, like `read_file("notes.txt")`. The harness runs that function. It appends the result to the conversation. Then it calls the model again with the whole updated conversation. The model sees the result and decides what to do next. This repeats until the model replies with plain text and no tool call. Chat APIs like OpenAI's Chat Completions are stateless: they do not remember earlier messages. So the harness resends the entire conversation every time. That growing message list is the agent's state. The model only knows about a tool through its description, which includes a JSON schema for the arguments. [Real Python's tutorial](https://realpython.com/agentic-loop/) builds this in about ninety lines of Python with no frameworks. The important part is not the code size. It is where the loop lives. Most agent frameworks are built around this same loop, with extra bookkeeping on top. In my view, the rest is bookkeeping: logging, retries, permissions, formatting. Because the loop repeats until the model stops asking for tools, an agent can run forever if nothing stops it. You need a stop condition: a maximum number of turns, a time limit, or a rule that halts on a specific tool result. Put that check in the harness, not in the model.

What Makes an LLM an Agent: The Loop, Not the Model — Visual Breakdown

Click any step below to inspect
User message
Model emits tool call
Harness runs the tool
Result appended to messages
Model called again
Plain text reply, loop ends
Sequence of Events
Why It Matters in Production
Once you see the loop, agent bugs get easier to find. If an agent repeats the same tool call, the harness is probably not appending the result correctly, the model cannot see it, or the tool description is too vague for the model to know the call succeeded. If it runs forever, you are missing a stop condition. If it uses a tool wrongly, check the tool's description and JSON schema, because that is all the model sees. This affects anyone wiring an LLM to real actions: file edits, shell commands, database queries. The model is not the risky part. The harness decides which tools exist, what they can touch, and when to stop. Review that code like you would review any code that runs commands.
Implementation & Syntax
🐍Python
import json

# Define your tools: name -> callable
def read_file(path: str) -> str:
    with open(path) as f:
        return f.read()

TOOL_FUNCS = {"read_file": read_file}

def run_agent(client, model, messages, tools, max_turns=10):
    for _ in range(max_turns):
        # OpenAI SDK: client.chat.completions.create(...)
        reply = client.chat.completions.create(
            model=model, messages=messages, tools=tools
        ).choices[0].message

        # Append assistant message in Chat Completions format
        messages.append({
            "role": "assistant",
            "content": reply.content,
            "tool_calls": reply.tool_calls,
        })

        if not reply.tool_calls:
            return reply.content

        for call in reply.tool_calls:
            try:
                fn = TOOL_FUNCS[call.function.name]
                args = json.loads(call.function.arguments)
                result = fn(**args)
            except Exception as e:
                result = f"Error: {e}"
            messages.append({
                "role": "tool",
                "tool_call_id": call.id,
                "content": str(result),
            })

    return "Stopped: max turns reached"

Quick Concept Check

Test your understanding in 15 seconds

In the agentic loop, what does the harness do after the model emits a tool call?

Bite Progression34 / 36

Related Concepts

Complementary topics

Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.