Knaph

Tools and the Loop16 of 375 min read

Asking for a Tool

In A Model With Hands, Leila worked out that an agent's model writes requests like "run this command", and the program around it carries them out. That's the right idea. This lesson looks at exactly how it's done, because once you've seen it, a lot about agents stops being mysterious.


Leila Here's what still bothers me. The model writes text. How does the program know that one piece of text is a request to do something, and not just part of an answer?

Omid Good question. Suppose you were writing the program. How would you make sure you could tell the difference?

Leila I'd agree a special format with the model. Like: "if you want me to do something, write it exactly like this, and nothing else".

Omid That's exactly what happens. And the model has to know which actions it may ask for. How would it know that?

Leila I'd tell it. In the request, I suppose, since that's all it sees. A list: "you can ask for these things".

Omid Right again. So: a list of actions in the request, and a fixed format for asking. That's the whole mechanism.


Tools, described

An action the model can ask for is called a tool. The program sends a list of them in every request, as part of the context. Each tool has three parts:

  • a name, like get_weather;
  • a description in plain words: what it does and when to use it;
  • the inputs it needs, and what type each one is.

A tool's description card. Name: get_weather. Description: Gets the current weather for a city. Use it when the user asks about weather now. Input: city, the city's name, as text.

The model reads these descriptions like everything else in its context. When it decides a tool would help, it doesn't answer in words. Instead it writes a tool call: the tool's name and its inputs, in a fixed format, and then stops. With Claude it looks like this:

{
  "type": "tool_use",
  "name": "get_weather",
  "input": { "city": "Tabriz" }
}
JSON

Notice three things: name says which tool; input holds the values, shaped exactly as the description asked; and there's no answer for Leila yet, because the model has stopped to wait.

The exchange, step by step

Here's the whole exchange when Leila asks "Should I take an umbrella in Tabriz today?":

  1. The program sends the request: the system prompt, the tool list, and Leila's question.
  2. The model writes a tool call, get_weather with city: Tabriz, and stops.
  3. The program sees the tool call, and runs the real code behind get_weather: perhaps it asks a weather service on the internet.
  4. The program adds the answer to the conversation as a tool result: light rain, 9 degrees. It sends a new request with everything so far.
  5. The model reads the result and writes its answer: "Yes, take one: light rain is expected."

The exchange as a sequence. The model writes a tool call; the program runs the tool and sends back a tool result; the model then writes the answer for Leila.

At no point did the model touch the internet. It wrote a request, and later read a result. Everything that happened in the world was done by the program. This is also called function calling, because each tool is usually a function in the program's code.


Leila So if the description is bad, the model might use the tool at the wrong time.

Omid Or not at all. The description is all the model knows about the tool. A tool described as "weather" with no detail gets used badly. One described as "gets the current weather for a city; use it when the user asks about weather now, not for forecasts" gets used well.

Leila And every tool's description is in every request?

Omid In every request. An agent with fifty tools carries fifty descriptions in its context, all the time. That costs tokens and attention, which is why good agents keep their tool list tidy.


A coding agent's tools

Coding agents like Claude Code come with a handful of tools that cover almost everything:

  • read a file (or part of one);
  • edit a file, or write a new one;
  • search files for a word or pattern;
  • run a shell command: any program on the computer, from listing a folder to running tests;
  • fetch a web page, or search the web.

The shell command tool is the powerful one: through it, the agent can use any program you can. Later lessons are about how you keep that power under control.

Try it

If you have an agent, ask it something that needs a tool, like "how many files are in this folder?", and watch the screen. Most agents show each tool call as it happens (in Claude Code it appears as a line like Bash(ls | wc -l)), then its result. In a chat assistant, ask about today's news: if it searches, you'll usually see a "searching" step before the answer.

Using it well

  1. Read the tool calls, not just the final answer. They show what the agent actually did, which is often more telling than what it says it did.
  2. Expect tools to fail sometimes. A wrong file name, a command that errors: the error comes back as a tool result, and a good agent reads it and tries again.
  3. If you build or add tools, write the descriptions with care. Say what the tool does, when to use it and when not to. The model can't see anything else about it.

Words to know

  • tool - an action the program offers the model, described by a name, a description and its inputs
  • tool call - text in a fixed format in which the model asks for a tool to be run, with its inputs
  • tool result - what the program sends back to the model after running a tool: the output, or an error
  • function calling - another name for tool use, since tools are usually functions in the program's code

Big idea

The model never acts. It reads tool descriptions, writes a tool call, and stops; the program runs the tool and sends back the result for the model to read. Everything an agent does to the world goes through that exchange.

Watch this lesson

3 minPart of episode 4 (9 min)Watch the whole episode

Test your understanding

Sign in to test your understanding of this lesson and track your progress.