Lesson 201 of 2116
Calling the Claude API With Streaming
Anthropic's SDK in 20 lines. Learn messages, streaming tokens, and basic error handling.
Lesson map
What this lesson covers
Learning path
The main moves in order
- 1Messages In, Tokens Out
- 2messages API
- 3streaming
- 4system prompt
Concept cluster
Terms to connect while reading
Section 1
Messages In, Tokens Out
Claude's API takes a list of messages and returns a reply. Streaming yields tokens as they are generated so users see output immediately.
SDK + env var. That is the setup.
npm install @anthropic-ai/sdk
export ANTHROPIC_API_KEY=sk-ant-...messages.create returns content blocks. Narrow on type before accessing text.
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();
export async function ask(prompt: string): Promise<string> {
const res = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 1024,
system: "You are a concise coding tutor.",
messages: [{ role: "user", content: prompt }],
});
const block = res.content[0];
if (block.type !== "text") throw new Error("expected text block");
return block.text;
}Stream events are typed. Filter for text_delta and write tokens as they arrive.
export async function askStreaming(prompt: string) {
const stream = client.messages.stream({
model: "claude-opus-4-7",
max_tokens: 1024,
messages: [{ role: "user", content: prompt }],
});
for await (const event of stream) {
if (
event.type === "content_block_delta" &&
event.delta.type === "text_delta"
) {
process.stdout.write(event.delta.text);
}
}
const final = await stream.finalMessage();
console.log("\nstop_reason:", final.stop_reason);
}Understanding "Calling the Claude API With Streaming" in practice: AI-assisted coding shifts work from syntax recall to design thinking — models handle boilerplate so you focus on architecture. Anthropic's SDK in 20 lines. Learn messages, streaming tokens, and basic error handling — and knowing how to apply this gives you a concrete advantage.
- Apply messages API in your ai-coding workflow to get better results
- Apply streaming in your ai-coding workflow to get better results
- Apply system prompt in your ai-coding workflow to get better results
- Apply model id in your ai-coding workflow to get better results
- 1Use AI to generate unit tests for an existing function
- 2Ask AI to refactor a messy function and explain the changes
- 3Have AI suggest a code review for a recent pull request
Key terms in this lesson
The big idea: messages.create for batch, messages.stream for UI. Narrow on block types and handle 529 like a grown-up.
End-of-lesson quiz
Check what stuck
15 questions · Score saves to your progress.
Tutor
Curious about “Calling the Claude API With Streaming”?
Ask anything about this lesson. I’ll answer using just what you’re reading — short, friendly, grounded.
Progress saved locally in this browser. Sign in to sync across devices.
Related lessons
Keep going
Creators · 50 min
Test-Driven AI Development
TDD was already the gold standard. Paired with an agent, it becomes the tightest feedback loop in software. Here's the full workflow and the pitfalls.
Creators · 50 min
Deploying an AI App to Vercel
Streaming AI chat to production takes one framework and three env vars. Learn the deploy path that actually ships.
Creators · 50 min
Installing and Using Claude Code CLI
Claude Code is Anthropic's terminal-native coding agent. Let's install it, wire it to a project, and use the features most engineers miss on day one.
