Phase 2 — Working with LLMs · Lesson 23 · 20 XP
Tool use (function calling)
A tool definition tells the model a function exists: its name, a description, and a JSON schema for its parameters. The model never executes anything itself — when it decides a tool is needed, it returns a structured request to call it, and your code is the one that actually runs it and sends the result back.
The loop: send messages + tool definitions → model responds, possibly requesting a tool call → your code executes that tool → you append the tool's result as a new message → send again → model continues, now with the tool's output available. This is the exact mechanism that turns into an agent in Phase 3.
The tool's description matters as much as its implementation — it's the only information the model has to decide when and how to call it. A vague description gets called at the wrong times or with wrong arguments, no matter how correct the underlying code is.
Exercise
Define one real tool (e.g. a calculator or a lookup function), wire it into an API call with its schema, and implement the loop that executes it when requested and returns the result to the model.
Check yourself
1. Who actually executes the tool call — the model, or your code?
2. Why does the tool's description matter as much as its implementation?
Structured output and validation
Answer the check-yourself questions to unlock this