AI Hints
Home›Generative AI Tutorial›Hands-On Workshops
Generative AI · Lesson 65 of 77

Workshop: A Tool-Using Agent

Think, act, observe in code.

⏱ 2 min read▶ Video at 2:25:35🎓 Free
Watch this lesson in the Generative AI courseWatch on YouTube ↗Full Generative AI course

In this workshop, you will build the skeleton of an agent. It will have two tools, and a loop. Because we want the code to run anywhere, we will use a pretend model, that follows a fixed script, so that you can see the loop clearly. Swapping in a real model is a small change.

Workshop: A Tool-Using Agent
From the video: Workshop: A Tool-Using Agent
  • Two simple tools
  • The agent loop in code
  • A pretend model, so it runs anywhere

Step one: the tools

Step one, the tools. A tool is just a Python function. Here we have two. A calculator, that evaluates a simple sum. And a lookup, that returns the price of an item, from a small dictionary. We put them in a dictionary, by name, so that the agent can find the one it asks for.

tools.pypython
def calculator(expression):
    return str(eval(expression, {"__builtins__": {}}))

PRICES = {"fern": 9.5, "ivy": 7.0}

def lookup_price(item):
    return str(PRICES.get(item, "unknown"))

TOOLS = {"calculator": calculator, "lookup_price": lookup_price}
print(TOOLS["lookup_price"]("fern"))
print(TOOLS["calculator"]("3 * 9.5"))
Output
9.5
28.5

Step two: the agent loop

Step two, the loop. In each round, the model returns an action. Either, call a tool, with an argument, or finish, with the final answer. If it is a tool call, we run the tool, and record the observation. The loop stops at finish, or after a safety limit of five steps. Here, a scripted pretend model plays the part of the real one.

agent.pypython
def calculator(e): return str(eval(e, {"__builtins__": {}}))
def lookup_price(i): return {"fern": "9.5"}.get(i, "unknown")
TOOLS = {"calculator": calculator, "lookup_price": lookup_price}

script = iter([("lookup_price", "fern"), ("calculator", "3 * 9.5"), ("finish", "3 ferns cost $28.5")])

for step in range(5):
    action, arg = next(script)
    if action == "finish":
        print("Answer:", arg)
        break
    obs = TOOLS[action](arg)
    print(f"Step {step + 1}: {action}({arg}) -> {obs}")
Output
Step 1: lookup_price(fern) -> 9.5
Step 2: calculator(3 * 9.5) -> 28.5
Answer: 3 ferns cost $28.5

From pretend to real

To make it real, replace the script with a call to a real model. Describe the tools to the model, with function calling. Send the history, including each observation, and ask for the next action. Keep the step limit, and add human approval, for any risky tool. Log every step. The loop you just saw is the same loop, inside every agent product.

Describe tools to the modelfunction calling
Send the historyincluding each observation
Keep the step limitand log every step
Approve risky actionsa human decides

Summary

Excellent. You built the heart of an agent, tools as functions, a loop that acts and observes, a step limit, and a final answer. Replacing the script with a real model, is a small step. Next, a prompt lab, with ready to use prompts, for study, work, and creativity.

Key takeaways
  • Tools are just functions
  • Loop: act, observe, repeat, finish
  • Step limits and approval keep it safe
Next up: Prompt Lab — ready-to-use prompts