BuildBot

The Agent Loop

Tool use

Lesson 2 of 3

What you'll learn

  • Describe a tool with a name, description, and input schema
  • Dispatch a tool call by name to your real function
  • Return the result so the model can continue

A tool is a function you let the model call. You don't give it your code — you give it a description: a name, a plain-language summary, and a JSON schema for the inputs. Claude uses that to decide when and how to call it.

const weatherTool = {
  name: "get_weather",
  description: "Get the current temperature for a city.",
  input_schema: {
    type: "object",
    properties: { city: { type: "string" } },
    required: ["city"],
  },
};

You pass the tool definitions with your request. When the model wants the tool, it replies with a tool_use block containing the name and an input object that matches your schema.

Dispatching

Your job is to map that name to a real function, run it, and send back a tool_result:

const handlers = {
  get_weather: ({ city }) => lookupWeather(city),
};

function dispatch(toolUse) {
  const fn = handlers[toolUse.name];
  if (!fn) throw new Error("Unknown tool: " + toolUse.name);
  return fn(toolUse.input);
}

The description is the interface

The model only knows what your description and schema say. Vague descriptions cause wrong or missing tool calls. Treat them like documentation for a careful but literal collaborator.

The challenge wires up a tiny tool registry and dispatches a call. Run it and add another tool.

A tool registry

dispatch() looks up the tool by name and runs it. Run it, then register a 'multiply' tool.

Loading editor…

Next: getting clean, structured data back instead of prose.

Sign in to save your progress across devices.