← AI Agents Foundations
7 / 25
Lesson 7 — Tools as Python functions
Giving the model hands
A tool is just a normal Python function the agent is allowed to run: add numbers, read the clock, search notes. The model never runs code itself — it only asks for a tool by name, and your code runs it.
To find a tool by name, keep a registry: a dict from name to function.
TOOLS = {"add": add}
TOOLS["add"](2, 3) # looks up the function, then calls it -> 5
Notice add without brackets is the function itself; add(2, 3) calls it.
Why it matters: tools are what turn a chatbot into an agent. Models are bad at exact maths and do not know today's date — a tool fixes both.
Want it another way?
Task
Write word_count(text) that returns the number of words, and register it in TOOLS as "word_count".
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.