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.
15 questions · take it digitally for instant feedback at tendril.neural-forge.io/learn/quiz/end-progx-tool-use-patterns-creators
What is the 'agentic loop' in AI systems that use tools?
Which of the following is NOT typically required when defining a tool for an AI model?
If an AI model outputs multiple tool_use blocks in a single response, what should you do?
Why is setting a hard cap on the number of turns in an agentic loop important?
What is the primary purpose of giving an AI model access to tools?
What does it mean for tool descriptions to be 'surgical'?
In the agentic loop, what is a 'tool_result'?
What is the main risk of running an agentic loop without any turn budget limit?
When should you execute multiple tool calls that arrive in the same turn?
What information does a JSON schema provide for a tool?
After you execute a tool in the agentic loop, what happens next?
What is the primary advantage of executing multiple tools in parallel rather than sequentially?
What turn budget does the lesson recommend for a typical agentic loop?
What should a tool's description primarily focus on?
Why might an AI model call multiple tools in a single turn?