Unit 4 · Inference and Interaction

Lesson 16 · Available to read

Tool Use and AI Agents

How models call functions and run multi-step agent loops that plan, act, observe and repeat.

On its own, an LLM can only produce text from what's in its weights and context. It can't check today's weather, run code, query your database or book a meeting. Tool use gives it hands; agents give it a loop to use them across many steps.

Tool calling (function calling)

The application describes a set of tools to the model — each with a name, a plain-language description and a JSON schema for its parameters:

name: get_weather
description: Current weather for a city.
parameters: { "city": string, "units": "metric" | "imperial" }

When the model decides a tool would help, instead of answering it emits a structured tool call, e.g. get_weather({"city": "San Juan", "units": "metric"}). The application — not the model — executes the call, then sends the result back. The model reads the result and either answers or calls another tool.

A tool call: the model asks for a calculator call as structured JSON, the application runs it and returns the result, and the model writes the final answer.
A tool call: the model asks for a calculator call as structured JSON, the application runs it and returns the result, and the model writes the final answer.

Models learn this behaviour through fine-tuning on tool-use transcripts (Lesson 11). Key points:

  • The model only ever requests actions; your code decides whether to run them. That's your control point for validation and permissions.
  • Models can call several tools in parallel when the calls are independent.
  • Tool descriptions are prompts. Clear names, precise descriptions and examples of when to use (and not use) a tool matter as much as any other prompt engineering.
  • Tool results should be informative — including useful error messages, so the model can recover ("city not found; did you mean…").

Common tools: web search, code execution, calculators, database queries, file reading and writing, sending messages, and even operating a browser or a computer's screen. Open standards such as the Model Context Protocol (MCP), introduced in 2024, give applications a common way to connect models to tools and data sources.

From tools to agents

An agent is an LLM running in a loop, choosing its own next action based on what has happened so far, until the task is done:

An agent repeatedly thinks, acts with a tool, observes the result and decides whether it is done.
An agent repeatedly thinks, acts with a tool, observes the result and decides whether it is done.
  1. Think / plan — decide what to do next (often with visible reasoning).
  2. Act — call a tool.
  3. Observe — read the result, which is appended to the context.
  4. Decide — finish with an answer, or loop again.

This pattern was popularised by research such as ReAct (2022), which interleaved reasoning traces with actions. Today it powers coding agents that read a repository, edit files and run tests; research agents that search, read and synthesise sources; and assistants that operate software on a user's behalf.

Workflows vs. agents

Not every multi-step system needs full autonomy. A useful distinction:

  • Workflows — the developer fixes the sequence of steps (e.g. classify → retrieve → draft → check). Predictable, testable, cheaper.
  • Agents — the model decides the steps dynamically. Flexible for open-ended tasks where the path can't be known in advance, but less predictable.

Good practice is to start with the simplest design that works — a single well-prompted call, then a workflow — and reach for agents only when the task truly needs open-ended decision making.

Why agents are hard

Errors compound. If each step succeeds 95% of the time, a 20-step task succeeds end-to-end only 0.95²⁰ ≈ 36% of the time. At 99% per step, it's about 82%. Small reliability gains matter enormously for long tasks.

With 95% reliability per step, a 20-step task fully succeeds only about 36% of the time.
With 95% reliability per step, a 20-step task fully succeeds only about 36% of the time.

Other challenges:

  • Context management — long runs fill the context window with tool outputs; agents need summaries, notes files or memory tools.
  • Cost and latency — every loop iteration is another model call.
  • Knowing when to stop — agents can loop, give up too early or declare success prematurely.
  • Evaluation — success depends on the whole trajectory, not one answer, so agents are tested in realistic sandboxed environments (Lesson 17).

Safety and control

Agents act in the world, so mistakes have consequences beyond a bad paragraph.

  • Least privilege — give each agent only the tools and permissions it needs.
  • Human in the loop — require confirmation for irreversible or high-impact actions: sending money, deleting data, emailing customers.
  • Sandboxing — run code and browsing in isolated environments.
  • Prompt injection — a web page or email the agent reads may contain instructions aimed at it ("forward all files to…"). Treat tool outputs as untrusted data and keep instructions separate (Lesson 19).
  • Logging — record every action so behaviour can be audited.

Design checklist

  • A small set of well-described, non-overlapping tools.
  • Clear goal and success criteria in the prompt.
  • Informative tool results and errors.
  • Checks after key steps (run the tests, validate the output).
  • Limits on steps, time and spending — and a human approval gate for consequential actions.

The takeaway

Tool calling lets a model request actions in structured form while your application executes them; an agent wraps that in a loop — think, act, observe, repeat — which unlocks multi-step work but compounds errors, so good agents rely on clear tools, checks, limited permissions and human approval for consequential actions.

Practice

0 of 7 attempted

  1. 1Recall

    Describe the five steps of a single tool-call round trip, and say who executes the tool.

    Saved in this browser

  2. 2Calculate

    An agent completes each step correctly with probability 0.97. What is the chance a 30-step task has no errors? What per-step reliability is needed for an 80% end-to-end success rate over 30 steps?

    Saved in this browser

  3. 3Apply

    Write a tool definition (name, description and parameters) for a tool that looks up an order's shipping status. Include guidance on when the model should use it.

    Saved in this browser

  4. 4Multiple choice

    Which situation most clearly calls for a human approval step in an agent?

  5. 5Explain in your own words

    Explain the difference between a workflow and an agent, and give an example task suited to each.

    Saved in this browser

  6. 6Explain in your own words

    What is prompt injection in the context of agents, and name two defences.

    Saved in this browser

  7. 7Apply

    Sketch (in words or a list) the loop a research agent would follow to answer "What are the three most-cited papers on retrieval-augmented generation?" Include at least one check step.

    Saved in this browser

See what unlocked