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.
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:
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.
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.
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.
Tokens cost money. Key strategies:
Time to consolidate what you learned.