Lesson 8 of 24

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.

Beginner12 minutesNo framework required
PrerequisiteWhat is a large language model?
EXPLANATION MODE

Plain English, analogy and visual flow.

THE ANSWER IN ONE SENTENCE

Tool calling lets an AI model ask another piece of software to do something for it.

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.

EVERYDAY ANALOGY

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.

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.

1User request
2Understand goal
3Choose tool
4Perform task
5Return result
6Final answer

Step by step.

  1. 01

    Your application describes the available tool.

    It gives the model a name, a clear description and a schema for valid arguments.

  2. 02

    The model decides whether it needs the tool.

    If the answer requires current data or an external action, it produces a structured request.

  3. 03

    Your application validates and executes.

    Never trust generated arguments automatically. Check them, confirm permission and call the real function.

  4. 04

    The result returns to the model.

    The tool output becomes new context so the model can produce a grounded final response.

Run the smallest working example.

tool-calling.ts
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
});
OUTPUT / LOGS

Run the example to see the output.

Trace the execution.

STAGE 06

Final response

The model turns the verified result into a natural-language response.

Why tool calling fails.

01

Trusting generated arguments

Validate every field against a strict schema before execution.

02

Allowing unrestricted access

Give each user and agent only the tools they are permitted to use.

03

Repeating tools indefinitely

Set an action budget and detect identical repeated calls.

04

Returning too much output

Summarize or paginate large tool results before adding them to context.

05

Ignoring failed calls

Handle timeouts, retries and safe user-facing errors explicitly.

What changes when real users depend on it?

Authenticate the user before tool access
Allow-list tools for each role
Validate arguments and outputs
Add timeouts, retries and circuit breakers
Trace every decision and execution
Measure latency, cost and success rate
Test prompt injection and permission boundaries
Evaluate final answers against tool results

Apply the idea.

A model needs the latest order status. Why should it call a tool instead of answering from memory?

SMALL EXERCISE · 10 MIN

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.

Five things to remember.

  1. The model requests a tool; your application executes it.
  2. Tool definitions need a clear name, description and argument schema.
  3. Generated arguments are untrusted input and must be validated.
  4. Returned observations ground the model's final response.
  5. Production tool use needs permissions, limits, tracing and evaluation.