Forget frameworks. Build one from scratch to truly understand agents.
┌────────────────────────────────────┐ │ THE MINIMAL AGENT LOOP │ ├────────────────────────────────────┤ │ 1. Send messages + tools to LLM │ │ 2. Get response │ │ 3. If response has tool_use: │ │ a. Execute the tool │ │ b. Add result to messages │ │ c. GOTO step 1 │ │ 4. If response has text: │ │ a. Return the text (DONE) │ └────────────────────────────────────┘
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();
const tools = [{
name: "get_weather",
description: "Get current weather for a city",
input_schema: {
type: "object",
properties: {
city: { type: "string", description: "City name" }
},
required: ["city"]
}
}];
async function runAgent(userMessage: string) {
const messages = [{ role: "user", content: userMessage }];
while (true) {
const response = await client.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
tools,
messages
});
if (response.stop_reason === "tool_use") {
const toolBlock = response.content.find(b => b.type === "tool_use");
const result = executeWeather(toolBlock.input.city);
messages.push({ role: "assistant", content: response.content });
messages.push({
role: "user",
content: [{ type: "tool_result", tool_use_id: toolBlock.id,
content: JSON.stringify(result) }]
});
} else {
return response.content[0].text; // Done!
}
}
}