← Back to Labs

Complete the Agentic Loop

agentic architectureFill in Blank

Complete the agentic loop that calls Claude, executes tools when requested, and terminates when done. Fill in the three key values that control the loop lifecycle.

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();

let response = await client.messages.create({
  model: "claude-sonnet-4-20250514",
  max_tokens: 4096,
  system: "You are a helpful customer support agent.",
  messages,
  tools,
});

// Continue looping while Claude wants to use tools
while (response.stop_reason === ) {
  const toolBlocks = response.content.filter((b) => b.type === "tool_use");
  const toolResults = await Promise.all(
    toolBlocks.map(async (block) => ({
      type: "tool_result" as const,
      tool_use_id: block.id,
      content: await executeTool(block.name, block.input),
    }))
  );

  // Append assistant response and tool results to conversation
  messages.push({ role: "assistant", content: response.content });
  messages.push({ role: , content: toolResults });

  response = await client.messages.create({
    model: "claude-sonnet-4-20250514",
    max_tokens: 4096,
    system: "You are a helpful customer support agent.",
    messages,
    tools,
  });
}

// Loop exits when stop_reason === 
console.log("Final response:", response.content);
Next Exercise →