Skip to the content

What is an AI agent? A model in a loop, in 15 lines of Python.

Carina Robinson24 NOV 2026 · 3 MIN READ

A chatbot answers once. An agent keeps going: it reads the result of what it just did and decides the next step. That's the whole difference, and it fits on one screen.

The loop, step by step

  1. 1 · ASK

    Send the task and the list of tools to the model.

  2. 2 · PICK

    The model answers, or asks for a tool.

  3. 3 · RUN

    Your code runs the tool and sends back the result.

  4. 4 · STOP?

    Answer ready, or too many steps: stop. Otherwise, back to 1.

The code

This is a complete agent with one tool, a word counter. It runs with the official anthropic package and an API key.

import anthropic

client = anthropic.Anthropic()
TOOLS = {"word_count": lambda text: len(text.split())}
SPEC = [{"name": "word_count", "description": "Count the words in a text.", "input_schema": {"type": "object", "properties": {"text": {"type": "string"}}, "required": ["text"]}}]

def run(task):
    messages = [{"role": "user", "content": task}]
    for step in range(10):  # never loop forever
        reply = client.messages.create(model="claude-haiku-5-5", max_tokens=1024, tools=SPEC, messages=messages)
        messages.append({"role": "assistant", "content": reply.content})
        calls = [block for block in reply.content if block.type == "tool_use"]
        if not calls:  # no tool needed: done
            return next(block.text for block in reply.content if block.type == "text")
        results = [{"type": "tool_result", "tool_use_id": call.id, "content": str(TOOLS[call.name](**call.input))} for call in calls]
        messages.append({"role": "user", "content": results})  # your code did the work
    return "Stopped after 10 steps."

agent.py · 15 lines · run it with print(run("How many words are in: the cat sat on the mat?"))

Read it top to bottom and you'll see the four steps above. SPEC tells the model which tools exist and what they take. TOOLS is the code that actually runs them. The model never runs anything itself: it asks, and your loop decides whether to do it.

Everything else in an agent framework, retries, memory, logging, is added around this loop.

Why the stop matters

Every pass through the loop is another model call, and every call costs tokens. Without the range(10), a confused model can keep calling tools until someone notices the bill. Ten steps is plenty for a word counter. For real work you set the limit from what the task needs, and you log every step so you can see where it went round in circles.

Agents you'd use at work

  • Answering from company documents: one tool searches your files, the model writes the answer with sources.
  • Checking stock or orders: one tool queries the system, the model replies to the customer.
  • Sorting tickets: the model reads each ticket and calls a tool to label and route it.

Each of these is the same loop with a different tool. The hard part is never the loop. It's choosing what the tool is allowed to do, and what needs a human to say yes first.

Want to build this yourself?

Build Your First Agent. Write this loop yourself, then make it use real tools. The first chapter is free, in your browser.

Start the first chapter
BUILD

What is a token? Why AI reads your text in pieces.

Models don't read words, they read tokens. Here's what a token is, how to count them in seven lines of Python, and why they decide your bill, your limits and your wait.

CARINA · 3 MIN READ · 25 NOV 2026

BUILD

Same question, different answer: why AI isn't a calculator.

A language model picks every next word with some chance involved, so the same question can get a different answer. Here's why, a script to see it, and what to do about it at work.

CARINA · 2 MIN READ · 25 NOV 2026