LESSON 8 OF 24 · AI AGENTS
Tool Calling in AI Agents
Tool calling allows an AI model to request an external function when it needs information or an action it cannot perform by itself.
PrerequisiteWhat is a large language model?Plain English, analogy and visual flow.
Tool calling lets an AI model ask another piece of software to do something for it.
01 / WHY IT MATTERS
A model cannot act on the world by itself.
A language model cannot directly check a live database, send an email, book a meeting or look up today's weather. Tools allow it to request those actions through software you control.
Think of the model as a manager.
The manager understands the request but asks trusted specialists to check inventory, calculate a total or send a payment. The manager decides what is needed; the specialist performs the action.
02 / VISUAL EXPLANATION
One request. Six visible stages.
The model does not perform the outside action itself. It creates a structured request that your application validates and executes.
03 / HOW IT WORKS
Step by step.
- 01
Your application describes the available tool.
It gives the model a name, a clear description and a schema for valid arguments.
- 02
The model decides whether it needs the tool.
If the answer requires current data or an external action, it produces a structured request.
- 03
Your application validates and executes.
Never trust generated arguments automatically. Check them, confirm permission and call the real function.
- 04
The result returns to the model.
The tool output becomes new context so the model can produce a grounded final response.
04 / INTERACTIVE CODE
Run the smallest working example.
const tools = [{
name: "get_weather",
description: "Get current weather for a city",
parameters: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"]
}
}];
const response = await agent.run({
prompt: "What is the weather in Delhi?",
tools
});Run the example to see the output.
05 / WHAT HAPPENS INTERNALLY
Trace the execution.
Final response
The model turns the verified result into a natural-language response.
06 / COMMON MISTAKES
Why tool calling fails.
Trusting generated arguments
Validate every field against a strict schema before execution.
Allowing unrestricted access
Give each user and agent only the tools they are permitted to use.
Repeating tools indefinitely
Set an action budget and detect identical repeated calls.
Returning too much output
Summarize or paginate large tool results before adding them to context.
Ignoring failed calls
Handle timeouts, retries and safe user-facing errors explicitly.
07 / PRODUCTION VERSION
What changes when real users depend on it?
08 / KNOWLEDGE CHECK
Apply the idea.
A model needs the latest order status. Why should it call a tool instead of answering from memory?
09 / PRACTICAL EXERCISE
Add argument validation.
Before the weather tool runs, verify that the city argument exists and is a non-empty string. Return a safe error when validation fails.
10 / SUMMARY
Five things to remember.
- The model requests a tool; your application executes it.
- Tool definitions need a clear name, description and argument schema.
- Generated arguments are untrusted input and must be validated.
- Returned observations ground the model's final response.
- Production tool use needs permissions, limits, tracing and evaluation.
