1import Anthropic from "@anthropic-ai/sdk";
2import { tools, runTool } from "./tools";
3
4const client = new Anthropic();
5
6type Msg = Anthropic.MessageParam;
7
8export async function agent(task: string) {
9 const messages: Msg[] = [{ role: "user", content: task }];
10
11 // the loop: think → act → observe → repeat
12 while (true) {
13 const res = await client.messages.create({
14 model: "claude-sonnet-5-5",
15 max_tokens: 1024,
16 tools,
17 messages,
18 });
19
20 messages.push({ role: "assistant", content: res.content });
21
22 if (res.stop_reason !== "tool_use") {
23 return res; // the model is done talking
24 }
25
26 const results = await Promise.all(
27 res.content
28 .filter((b) => b.type === "tool_use")
29 .map(runTool),
30 );
31 messages.push({ role: "user", content: results });
32 }
33}