Skip to content
Part 5 14 min read

Agents and Agentic Systems

An agent is an LLM running in a control loop: it observes the world, decides what to do, calls tools, observes the results, and repeats until the goal is reached. This part covers the common agent loops, several agents working together, memory, human checkpoints, and recovery from errors.

Go to quiz
On this page

Section 1 of 10

What an agent actually is

A working definition, the boundary between "agent" and "workflow," and the minimum set of components any production agent must have.

The minimal agent

Strip away the marketing and every agent is the same shape: a loop that takes the current state, asks an LLM what to do next, executes the answer (a tool call, a structured response, or a final answer), and feeds the result back in. Where agents diverge from each other is how the loop is structured, what the state contains, and how failure is handled.

# The minimal agent in 12 lines
def agent_loop(goal, llm, tools, max_steps=20):
    history = [{"role": "user", "content": goal}]
    for step in range(max_steps):
        response = llm.complete(history, tools=tools)
        if response.finish_reason == "stop":
            return response.text
        result = tools.invoke(response.tool_call)
        history.append({"role": "tool", "content": str(result)})
    raise AgentTimeout("loop did not terminate")

Agent vs workflow

A workflow is a deterministic graph of LLM calls and tool calls: every step is authored, every branch is explicit. An agent is a workflow where at least one node's behaviour is decided at runtime by the LLM itself. The line is blurry in practice — most production systems are "agentic workflows" that combine both.

Rule of thumb: if a human could write a flowchart of the system's behaviour from the prompt alone, it's a workflow. If the LLM picks the next node at runtime, it's an agent. Most shipped systems are a mix — deterministic scaffolding around a few agentic decisions.

What every production agent needs

  • A bounded loop with a hard step cap and a wall-clock timeout. Unbounded loops are how agents burn $10k overnight.
  • A tool surface that's narrow, well-typed, and namespaced (so the model can't accidentally call a destructive tool without going through a guardrail).
  • A scratchpad — some persistent state across turns that survives the LLM's context window. Either the model writes to a file, the framework keeps it, or you stuff it into the next prompt.
  • An exit predicate: at minimum, "I think I have enough; here is the answer." Without one the agent will keep acting forever.
  • Observability: every step recorded with prompt, tool call, result, cost, and latency. Without it you cannot debug.

Practice · 1 task

Open any free AI chat: ChatGPT · Claude · Gemini

How practice works
  • A new chat is a fresh conversation with no history. In ChatGPT, Claude, or Gemini, click New chat. It is usually at the top left.
  • Same chat means: keep typing in the chat that is already open. The AI remembers what you sent before.
  • To use a prompt: click Copy, click in the message box of the chat, paste it (Ctrl+V, or press and hold on a phone), and press Enter.
  • AI answers are a little different every time. Your answer does not need to match the example word for word. Look for the same idea.
  • Use made-up data only. Do not paste private information into a chat.

Task 1

Watch an agent loop until it finishes

Why this matters

When you build an AI that checks prices or books tickets, it must work step by step and know when to stop. If it never stops, it keeps calling tools and you pay for every call.

  1. Step 1
    New chat

    Copy this prompt, paste it, and press Enter.

    Prompt
    You are an agent that works in a loop. You can use two tools:
    - get_price(item): returns the price of one item in BDT
    - calculator(expression): returns the result of a math expression
    
    Rules:
    1. Each turn, reply with exactly ONE line. Either:
       ACTION: tool_name(argument)
       or
       FINAL: your answer
    2. After an ACTION line, stop and wait. I will reply with a line that starts with OBSERVATION.
    3. You have at most 6 steps. If you reach step 6 without an answer, write FINAL: step limit reached.
    
    Goal: Find the total cost of 3 notebooks and 4 pens.

    You should see: One line only, for example ACTION: get_price(notebook). Then the model waits for you.

    Example answer
    ACTION: get_price(notebook)

    Your answer will be worded differently.

  2. Step 2
    Same chat

    Now you are the tool. Copy this price list, paste it, and press Enter.

    Prompt
    OBSERVATION: notebook = 120 BDT, pen = 25 BDT

    You should see: A new ACTION line. Most chats ask for the calculator, for example ACTION: calculator(3 * 120 + 4 * 25). If the model asks for a price again, send the same price list again.

    Example answer
    ACTION: calculator(3 * 120 + 4 * 25)

    Your answer will be worded differently.

  3. Step 3
    Same chat

    Work out the sum on a phone calculator. Type OBSERVATION: and the number, then press Enter. For 3 * 120 + 4 * 25 the answer is 460.

    You should see: One line that starts with FINAL, for example FINAL: The total cost is 460 BDT.

    Example answer
    FINAL: The total cost of 3 notebooks and 4 pens is 460 BDT.

    Your answer will be worded differently.

  4. Step 4
    Same chat

    Copy this question, paste it, and press Enter.

    Prompt
    Which of your lines told the loop to stop? Who chose that moment: you or me?

    You should see: The model points to its FINAL line and says it chose to stop by itself.

    Example answer
    The FINAL line told the loop to stop. I chose that moment myself, because I had the total and needed no more tools.

    Your answer will be worded differently.

What just happened

You never told the model which tool to call first. It chose each action, read your result, and chose the next one. It stopped when it wrote FINAL, so the model decided when the work was done. The rule at most 6 steps is a step limit: it stops a loop that never ends.

You are done when You have seen 2 or 3 ACTION lines and then one FINAL line with the total 460 BDT.

Check yourselfWhat makes this an agent and not a fixed list of steps?Show answer

The model decides the next step and decides when to stop. In a fixed list of steps, a person writes every step in advance.

Try a harder version

Change the rule to at most 2 steps and use the same goal in a new chat. What does the agent write when 2 steps are too few?

Watch · 3 videos