AlgoMaster Logo

Exercise: What Are AI Agents?

3 min readUpdated June 22, 2026
Listen to this chapter
Unlock Audio

Exercise 1: Build a Single-Agent Loop

Build the core agent loop: the model is given a tool and a goal, and it keeps thinking and calling the tool until it can answer. You run the loop, execute each tool call, and feed results back until the model is done.

What separates an agent from a one-shot prompt is the loop. The model does not produce the answer in one step; it takes an action, observes the result, and decides what to do next, repeating until the goal is met. Building this loop once shows you that an "agent" is mostly a model plus tools plus a while loop with a stopping condition.

Requirements

  • Give the agent a calculator tool that evaluates a simple arithmetic expression.
  • Pose a goal that needs the tool, such as a multi-part calculation.
  • Loop: call the model, run any tool it requests, append the result, and continue until the model returns a final answer.
  • Print each tool call and the final answer.
main.py
Loading...

Expected Output

The agent uses the calculator to do the arithmetic rather than computing it itself, then reports the result.

Solution

main.py
Loading...

The loop is the agent. Each pass lets the model see the conversation so far, decide whether to act or answer, and either request a tool or finish. Appending both the model's tool request and the tool's result keeps the history consistent so the next decision is informed. The two stopping conditions, no tool call and the turn limit, are what keep the loop both responsive and safe; everything more sophisticated (more tools, planning, memory) is built on this skeleton.

Exercise 2: Give the Agent a Choice of Tools

An agent with one tool has no decision to make. Give it two (a calculator and a word counter) and the interesting behavior appears: it must read the question and pick the right tool. Choosing among tools is the core agent skill that scales to dozens of tools.

Requirements

  • Provide two tools: calculator and word_count.
  • Pose a question that only one of them can answer.
  • Run the agent loop, letting the model select the tool, and print each tool call and the final answer.
main.py
Loading...

Expected Output

The agent recognizes a counting question and calls word_count, not the calculator.

Solution

main.py
Loading...

With more than one tool, the model's job includes routing: it matches the question to the tool whose description fits. This is why clear tool names and descriptions matter, they are what the model reads to choose. The loop is unchanged from the single-tool version; the new capability comes entirely from offering a choice, which is the same mechanism that lets a real agent select among many tools.