Loading lesson…
The model calls a function you defined, you run it, you return the result. Learn the loop and the common pitfalls.
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.
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}`); }Tool definitions are JSON Schema. The model only sees what you describe — keep descriptions precise.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"); }Cap the loop at N turns. Pass tool_result blocks with the same tool_use_id so the model stitches them together.The big idea: tools turn a chat model into a doer. Cap turns, parallelize when you can, and keep tool descriptions surgical.
6 questions · take it digitally for instant feedback at tendril.neural-forge.io/learn/quiz/end-progx-tool-use-patterns-creators
What is the main idea of "Tool-Use Patterns"?
Which concept is most central to "Tool-Use Patterns"?
What should a careful learner remember about "Parallel tool calls are free"?
You want to use AI after this lesson. What is the safest next step?
How should AI output about tool use be treated?
Name one way to verify an AI answer about tool use.