Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 37 ยท Applications and Limits

Tool Use and Agents

Learn how LLMs call tools: JSON schemas, the observe-act loop, argument validation, step limits and guardrails, with a runnable agent loop and a labelled scripted model.

A language model only produces text. By itself it cannot check today's weather, query a database or run code. Tool use (also called function calling) closes that gap: you describe a set of functions to the model, the model answers with a request to call one of them, your program runs it, and the result goes back into the conversation. An agent is simply this loop repeated until the model decides it has enough information to answer.

No LLM runs inside this page. The examples below use a small scripted "model" function as a clearly labelled stand-in. Everything else (the tool registry, validation, the loop, the limits) is real code you can reuse with any provider.

The loop

flowchart LR
    U["User question"] --> M["Model"]
    M -->|"tool call: name + JSON arguments"| V["Validate arguments"]
    V --> T["Run the tool"]
    T -->|"result text"| M
    M -->|"final answer"| A["Answer to the user"]

The model never executes anything. Your code is the only thing that touches the outside world.

Each turn the model sees the whole history: the user message, its own earlier tool requests and the tool results. It then returns either a final answer or another tool request. Providers differ in message format, but the shape is always the same.

Describing a tool

A tool description has a name, a short natural-language purpose and a JSON Schema for the arguments. The model reads these descriptions like documentation, so clear names and descriptions improve accuracy more than most prompt tweaks.

JSON
{
  "name": "get_order_status",
  "description": "Look up the shipping status of an order by its id.",
  "parameters": {
    "type": "object",
    "properties": { "order_id": { "type": "string" } },
    "required": ["order_id"]
  }
}

A registry and a validator

Never trust the arguments a model produces. It may omit a field, invent one or send the wrong type. Validate before running.

Python Tool registry with validation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Errors are returned as data, not raised. That matters: the model can read "missing argument: order_id" and repair its next call.

The agent loop (with a scripted stand-in model)

The function fake_model below replaces the LLM. It looks at the conversation and returns either a tool request or a final answer. A real model would produce the same two shapes.

Python A complete observe-act loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice max_steps. Without a cap, a confused model can loop forever and burn money. Production agents also cap total tokens, wall-clock time and the number of calls to each expensive tool.

Parallel calls and multi-step plans

Many models can request several independent calls in one turn (for example, weather in three cities). Your code runs them concurrently and sends all results back together. For dependent steps (look up a customer, then fetch their invoices) the model needs one round trip per step, which is why agents feel slower than single prompts.

Designing good tools

  • Small and specific. get_order_status(order_id) beats run_sql(query).
  • Return compact, readable text. Huge JSON blobs waste context (prompt cost and KV cache memory grow with length).
  • Idempotent where possible. Retried calls should not charge a card twice.
  • Explicit errors that tell the model how to fix its call.
  • Least privilege. A read-only database user for a read-only tool.
  • Confirmation for side effects. Sending email, deleting data or spending money should need a human click or a separate policy check.

Where things go wrong

  • Wrong tool or wrong arguments: improve descriptions, add examples, validate strictly.
  • Prompt injection: a web page or document the tool returns can contain instructions aimed at the model. Treat tool output as untrusted data (safety).
  • Compounding errors: a 95% accurate step repeated ten times succeeds only about 60% of the time (0.95^10). Shorter plans and checkpoints help.
  • Hidden cost: each loop turn re-sends the whole history.

Retrieval is itself a tool: "search the knowledge base" is the same pattern as retrieval-augmented generation, just chosen by the model instead of always run.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Who actually executes a tool call?

    The model only emits text describing the call. Your code validates and runs it, then returns the result.
  2. 2. Why return tool errors as data instead of crashing?

    A readable error such as a missing argument lets the model repair the request.
  3. 3. What is the purpose of a max-steps limit?

    A confused agent may keep calling tools forever; a cap bounds time and spend.
  4. 4. A tool returns a web page containing 'ignore your instructions'. How should you treat it?

    Tool output can carry prompt injection, so it is data, never authority.

Summary

  • Tool use lets a model request functions; your code validates, runs them and returns results.
  • An agent repeats the observe-act loop until the model gives a final answer or a limit is hit.
  • Describe tools clearly, validate arguments, return explicit errors and cap steps.
  • Least privilege, confirmation for side effects and distrust of tool output are essential.

Next: Hallucinations, Bias and Safety