Loading lesson…
Anthropic's SDK in 20 lines. Learn messages, streaming tokens, and basic error handling.
Claude's API takes a list of messages and returns a reply. Streaming yields tokens as they are generated so users see output immediately.
npm install @anthropic-ai/sdk
export ANTHROPIC_API_KEY=sk-ant-...SDK + env var. That is the setup.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;
}messages.create returns content blocks. Narrow on type before accessing text.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);
}Stream events are typed. Filter for text_delta and write tokens as they arrive.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.
The big idea: messages.create for batch, messages.stream for UI. Narrow on block types and handle 529 like a grown-up.
15 questions · take it digitally for instant feedback at tendril.neural-forge.io/learn/quiz/end-progx-claude-api-streaming-creators
What two things does the Claude API process as part of its core functionality?
What is the primary advantage of using streaming when calling the Claude API?
What parameter and value should you add to a system prompt block to enable caching across multiple API calls?
What does HTTP error code 529 indicate when calling the Claude API, and how should it be handled?
Why is retrying with exponential backoff preferred over using a tight loop after receiving a 529 error?
Which Claude API method should you use when building a real-time chat interface where users want to see responses appear word-by-word?
What does the lesson mean by 'narrow on block types' when processing Claude API responses?
Approximately what percentage of cost savings can prompt caching provide on the cached portion of a system prompt?
When should you use messages.create instead of messages.stream for Claude API calls?
What is a 'content block' in the context of Claude API responses?
In the context of the Claude API, what does 'streaming tokens' mean technically?
What is the primary purpose of the 'ephemeral' cache type in prompt caching?
What happens when you use a 'tight loop' to retry after receiving overloaded (529) errors from the Claude API?
Which scenario best demonstrates the 'Messages In, Tokens Out' concept from the API?
What type of application would benefit most from using messages.stream over messages.create?