Lesson 2 of 20Track 1

What is an agent?

The agent loop: perceive, reason, act

Understanding the fundamental cycle every agent follows.

Video lesson ~10 min

Video coming soon

The simplest possible agent

Every agent follows the same fundamental cycle. Strip away all the abstractions and you get three steps repeated in a loop:

  1. Perceive. The model looks at the current state (the goal, conversation history, tool results).
  2. Reason. The model decides what to do next (call a tool, ask for clarification, or declare the task done).
  3. Act. You execute the model's decision and feed the result back.

That's it. This is the core loop that powers everything from a simple chatbot with tools to a multi-agent system managing a fleet of workers.

Building the loop

Let's build this from scratch. No frameworks, just Python and Ollama.

import ollama
 
def run_agent(goal, tools, max_iterations=10):
    messages = [
        {"role": "system", "content": f"You are a helpful assistant. Your goal: {goal}"},
    ]
 
    for i in range(max_iterations):
        # PERCEIVE + REASON: model sees everything and decides
        response = ollama.chat(
            model="llama3",
            messages=messages,
            tools=tools,
        )
 
        message = response.message
        messages.append(message)
 
        # Check if the model wants to call a tool
        if not message.tool_calls:
            # No tool call = the model is done reasoning
            return message.content
 
        # ACT: execute each tool call
        for tool_call in message.tool_calls:
            result = execute_tool(tool_call)
            messages.append({
                "role": "tool",
                "content": str(result),
            })
 
    return "Max iterations reached without completing the goal."

This is a real, working agent. It's 25 lines of code. Every framework you'll ever use is an abstraction over this same pattern.

What happens inside the loop

Let's trace through an example. Say the goal is "What's the weather in Tokyo?" and we have a get_weather tool:

IterationPerceiveReasonAct
1Sees the goal, no results yet"I should check the weather"Calls get_weather("Tokyo")
2Sees the goal + weather data"I have the answer now"Returns "It's 22°C and sunny in Tokyo"

Two iterations. The model perceived the state, reasoned about what to do, acted, and then recognized it was done. This is the pattern every agent follows. The only thing that changes is the complexity of the tools and the reasoning required.

The three components

Every agent has exactly three components:

The model (the brain)

The LLM that does the reasoning. In our case, a model running on Ollama. The model's job is to look at the current state and decide the next action. It never executes anything directly. It only decides.

The tools (the hands)

Functions the agent can call to interact with the world. Tools turn the model's decisions into actions: reading files, making API calls, querying databases, running code. You define the tools; the model chooses when and how to use them.

The orchestration loop (the nervous system)

The code that connects the model to the tools. It feeds state to the model, parses the model's decisions, executes tools, and feeds results back. This is what you (the developer) build and control.

┌──────────────────────────────────────────┐
│            Orchestration Loop             │
│                                          │
│   ┌─────────┐    decide    ┌──────────┐  │
│   │  State   │───────────→│  Model   │  │
│   │ (messages│←───────────│ (LLM)    │  │
│   │  + goal) │   action    └──────────┘  │
│   └────┬─────┘                           │
│        │ execute                         │
│        ▼                                 │
│   ┌──────────┐                           │
│   │  Tools   │                           │
│   └──────────┘                           │
└──────────────────────────────────────────┘

A useful mental model

Think of the agent like a person solving a problem at a desk. The LLM is their brain (reasoning), the tools are the things on their desk (calculator, phone, computer), and the orchestration loop is their working process: look at the problem, decide what to do, do it, check the result, repeat.

Why the loop matters

The power of agents isn't the LLM itself. It's the loop. A single LLM call is a one-shot prediction. An agent loop turns that into an iterative problem-solver that can:

  • Try something, see it fail, and try a different approach
  • Gather information across multiple tool calls before answering
  • Break complex tasks into smaller steps and tackle them one at a time
  • Self-correct based on intermediate results

In the next lesson, we'll look at when you actually need this loop (and when a simple prompt is enough).

Done with this lesson?