0 XP
L1
?
Lessons
Claude API and Anthropic SDK
exercise ⏱ 25m
1/3

Claude API and Anthropic SDK

Time to go from concepts to code. The Claude API is the foundation for building any agentic product. Understanding the Messages API, tool use flow, and streaming is essential.

The Messages API

Every interaction with Claude is a sequence of messages with roles:

const response = await client.messages.create({
  model: "claude-sonnet-4-6-20250514",
  max_tokens: 1024,
  system: "You are a helpful assistant that manages projects.",
  messages: [
    { role: "user", content: "What are my overdue tasks?" }
  ],
  tools: [/ tool definitions /]
});

Key concepts:

  • system: Sets personality, constraints, context. This is where your agent's behavior is defined.
  • messages: The conversation history. Alternating user/assistant turns.
  • tools: Available function definitions the model can call.
  • model: Which Claude model to use. Pick based on task complexity.

The Tool Use Dance

When Claude decides to use a tool, the response contains a tool_use content block instead of (or alongside) text:

// Response from Claude:
{
  content: [
    { type: "text", text: "Let me check your tasks." },
    {
      type: "tool_use",
      id: "toolu_01XFDUDYJgAACzvnptvVer6z",
      name: "get_tasks",
      input: { status: "overdue" }
    }
  ],
  stop_reason: "tool_use"
}

You execute the tool, then send the result back:

// Send tool result back:
messages: [
  ...previousMessages,
  { role: "assistant", content: response.content },
  {
    role: "user",
    content: [{
      type: "tool_result",
      tool_use_id: "toolu_01XFDUDYJgAACzvnptvVer6z",
      content: JSON.stringify([{ title: "Ship client report", due: "2026-03-28" }])
    }]
  }
]

The model processes the result and either calls another tool or returns a final response. This loop is the core of every agent.

Test yourself

❓ Quiz 1
When the API returns stop_reason: 'tool_use', what should your code do?
stop_reason: 'tool_use' means the model wants to call a tool. Your code executes it, sends the result as a tool_result message, and the model continues reasoning.
Answer to continue ↓

Your turn

🎮 Tool Use Dance
Put the Claude API tool use flow in the correct order.
Send message + tool definitions to the API
Your code executes the tool locally
Send tool_result back to the API
Model processes result and responds (or calls another tool)
Model returns response with tool_use content block
Complete to continue ↓

Extended Thinking

For complex reasoning, enable extended thinking:

const response = await client.messages.create({
  model: "claude-opus-4-6-20250514",
  max_tokens: 16000,
  thinking: { type: "enabled", budget_tokens: 10000 },
  messages: [{ role: "user", content: "Design a system architecture for..." }]
});

The model gets a "thinking budget" to reason before responding. This is the planning component from Module 2. More budget = deeper reasoning = higher cost.

Streaming

For real-time applications, stream the response:

const stream = await client.messages.stream({
  model: "claude-sonnet-4-6-20250514",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Explain RAG" }]
});

for await (const event of stream) {
  if (event.type === "content_block_delta") {
    process.stdout.write(event.delta.text);
  }
}

Streaming is essential for UX — users don't want to wait 10 seconds for a complete response.

Cost Management

Tokens cost money. Key strategies:

  • Use the cheapest model that works (Haiku for classification, Sonnet for most tasks, Opus for complex reasoning)
  • Cache system prompts (prompt caching reduces cost by up to 90% for repeated system prompts)
  • Limit max_tokens to what you actually need
  • Use streaming to reduce perceived latency without increasing cost

Review

Time to consolidate what you learned.

🛠 Exercise 1
Write a TypeScript skeleton for an agent loop: import the Anthropic SDK, create a client, define one tool (search_meetings with a query parameter), send a user message, and handle the response in a while loop that continues as long as stop_reason is 'tool_use'. Include the tool_result message construction. Don't worry about actual API keys — focus on the structure.
✓ Saved
advance · ? shortcuts 05.01
Claude — Tutor
select text for context
Ask me anything about this lesson.
I can see your quiz answers and decisions.

💡 Select text in the lesson to use it as context.
CONTEXT