Skip to content
AI Engineering
Build Projects

Project: an agent with tools

Build an agent that uses two or three tools to answer questions it could not answer alone, such as checking the health of a GitHub repository or finding recent news on a topic.

Chapter 13: Build ProjectsLesson 3 of 5Level: StarterAbout 300 min
Words on this page (7)
agent
: A model run in a loop that chooses its own tools and steps until the task is done.
tool
: A function the model can ask your program to run, described by a name, description and argument schema.
repo
: Repository: a project folder tracked by git, usually hosted on GitHub.
tool use
: The model asks your program to run a function and reads the result.
logarithm
: The power you raise a base to in order to get a number. log base 2 of 8 is 3 because 2 to the power 3 is 8.
API
: Application programming interface: a way for one program to call another, usually over HTTP.
JSON
: A text format for structured data: keys and values in braces, like {"name": "Ada"}.

The brief

The user asks "Is this open-source project well maintained?" The calls for recent commits, open issues and the latest release, then writes a verdict with numbers.

Steps

  1. Define the tools with clear descriptions and schemas.
  2. Write the loop by hand: call the model, run , append results, repeat until the model answers, with a step limit.
  3. Validate every argument before running a tool.
  4. When a tool fails, return an instructive error as the result.
  5. every call and result.
  6. Save the conversation so it can resume.

Check yourself

Run ten different projects, including one that does not exist and one with no recent activity. Read the logs. Count how often the agent picks the wrong tool or loops. Fix descriptions and limits, then rerun the same ten.

Done when

The agent answers five test questions using its tools. A that does not exist produces a recovered answer, not a crash. A step limit stops it when you force a tool to keep failing. Every tool call is logged with its arguments and result.

Stretch

Add a second tool for the project's documentation and let the agent decide which to use. Try a smaller model for the tool steps and compare cost and accuracy.

Starter

Python. Reuse the loop from the agent loop lesson and give it two real tools backed by the GitHub (no key needed for light use). Install pip install requests.

import requests

def recent_commits(repo: str) -> str:
    r = requests.get(f"https://api.github.com/repos/{repo}/commits", params={"per_page": 5}, timeout=10)
    if r.status_code == 404:
        return f"No repository named '{repo}'. Use the form owner/name, for example pallets/flask."
    r.raise_for_status()
    return "\n".join(f"{c['commit']['author']['date'][:10]}  {c['commit']['message'].splitlines()[0]}" for c in r.json())

def run_tool(name, args):
    if name == "recent_commits":
        return recent_commits(args["repo"])
    raise ValueError(f"unknown tool {name}")

Add a second tool for open issues and one for the latest release. Write the tool descriptions carefully: they are what the model sees.

Check yourself

What stops an agent loop from running forever?

A tool receives a repository name that does not exist. What should it return?

In ten test runs, the agent picks the wrong tool in four. Where do you look first?

Go deeper

Spotted a mistake or something unclear? Report it on GitHub.

On this page