Lesson 206 of 2116
Tool-Use Patterns
The model calls a function you defined, you run it, you return the result. Learn the loop and the common pitfalls.
Lesson map
What this lesson covers
Learning path
The main moves in order
- 1The Agentic Loop
- 2tool use
- 3function calling
- 4agentic loop
Concept cluster
Terms to connect while reading
Section 1
The Agentic Loop
You define tools with a name, description, and JSON schema. The model decides to call one, you execute it, you feed the result back, the model continues. Loop until it stops asking for tools.
Tool definitions are JSON Schema. The model only sees what you describe — keep descriptions precise.
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();
const tools: Anthropic.Tool[] = [
{
name: "get_weather",
description: "Return current temperature in Fahrenheit for a city.",
input_schema: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"],
},
},
];
function executeTool(name: string, input: any): string {
if (name === "get_weather") {
// real implementation would call an API
return `72F and sunny in ${input.city}`;
}
throw new Error(`unknown tool ${name}`);
}Cap the loop at N turns. Pass tool_result blocks with the same tool_use_id so the model stitches them together.
export async function runAgent(question: string) {
const messages: Anthropic.MessageParam[] = [
{ role: "user", content: question },
];
for (let turn = 0; turn < 5; turn++) {
const res = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 1024,
tools,
messages,
});
messages.push({ role: "assistant", content: res.content });
if (res.stop_reason !== "tool_use") {
const text = res.content.find((b) => b.type === "text");
return text?.type === "text" ? text.text : "";
}
const toolUses = res.content.filter((b) => b.type === "tool_use");
messages.push({
role: "user",
content: toolUses.map((t) => ({
type: "tool_result" as const,
tool_use_id: t.id,
content: executeTool(t.name, t.input),
})),
});
}
throw new Error("agent exceeded turn budget");
}Key terms in this lesson
The big idea: tools turn a chat model into a doer. Cap turns, parallelize when you can, and keep tool descriptions surgical.
End-of-lesson quiz
Check what stuck
15 questions · Score saves to your progress.
Tutor
Curious about “Tool-Use Patterns”?
Ask anything about this lesson. I’ll answer using just what you’re reading — short, friendly, grounded.
Progress saved locally in this browser. Sign in to sync across devices.
Related lessons
Keep going
Creators · 75 min
Build It: A Minimal AI Agent Loop From Scratch
An agent is a loop: model decides, tool runs, model reads result, decides again. You'll build one in 100 lines without a framework.
Creators · 40 min
Agents vs. Autocomplete — the Mental Model Shift
Autocomplete is a suggestion. An agent is an actor. The mental model you bring to each is different, and conflating them is the number-one reason teams trip over AI coding.
Creators · 55 min
MCP — Connecting External Tools to AI Coding Agents
Model Context Protocol is the USB-C of AI tools. Learn the protocol, wire up a server, and understand why this standard quietly changed the ecosystem.
