Claude Node.js Streaming API (2026)

Node.js code examples for streaming Claude API responses. Covers basic SSE streaming, Express integration, error handling, and token-by-token output using @anthropic-ai/sdk.

🔥 Launch tonight — Power Prompts PDF 50p (just 50p tonight)30 battle-tested Claude Code prompts · 8 pages · paste into CLAUDE.md · price reverts to £5

Streaming Claude responses in Node.js lets you start displaying output before generation completes — cutting perceived latency from several seconds to near-zero. The official Anthropic SDK handles SSE parsing transparently.

Install

npm install @anthropic-ai/sdk
# Set ANTHROPIC_API_KEY in your environment or .env

Basic stream (print tokens to stdout)

import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();

const stream = client.messages.stream({
  model: "claude-sonnet-4-6",
  max_tokens: 2048,
  messages: [{ role: "user", content: "Write a Node.js HTTP server from scratch." }]
});

stream.on("text", (text) => process.stdout.write(text));
await stream.finalMessage();

Express streaming endpoint

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

const app = express();
app.use(express.json());
const client = new Anthropic();

app.post("/chat", async (req, res) => {
  res.setHeader("Content-Type", "text/event-stream");
  res.setHeader("Cache-Control", "no-cache");
  res.setHeader("Connection", "keep-alive");

  const stream = client.messages.stream({
    model: "claude-sonnet-4-6",
    max_tokens: 1024,
    messages: req.body.messages
  });

  stream.on("text", (text) => res.write(`data: ${JSON.stringify({ text })}

`));
  await stream.finalMessage();
  res.end();
});

app.listen(3000);

Accumulate the full response

const stream = client.messages.stream({ /* ... */ });
let fullText = "";

stream.on("text", (text) => { fullText += text; });
const final = await stream.finalMessage();

console.log("Complete response:", fullText);
console.log("Total tokens:", final.usage.input_tokens + final.usage.output_tokens);

Timeout and cancellation

const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 30_000);

try {
  const stream = client.messages.stream(
    { model: "claude-sonnet-4-6", max_tokens: 1024, messages },
    { signal: controller.signal }
  );
  stream.on("text", (text) => process.stdout.write(text));
  await stream.finalMessage();
} finally {
  clearTimeout(timeout);
}

Multi-turn streaming conversation

const history = [];

async function chat(userMessage) {
  history.push({ role: "user", content: userMessage });

  const stream = client.messages.stream({
    model: "claude-sonnet-4-6",
    max_tokens: 1024,
    messages: history
  });

  let assistantText = "";
  stream.on("text", (t) => { assistantText += t; process.stdout.write(t); });
  await stream.finalMessage();

  history.push({ role: "assistant", content: assistantText });
  return assistantText;
}

await chat("Hello!");
await chat("Now write a haiku about Node.js.");

Estimate streaming costs

Streaming and non-streaming use identical token pricing. Use the Claude Cost Calculator to estimate Node.js app costs, and the Prompt-Pricing Recommender to choose the right model tier.

Frequently asked questions

How do I stream Claude responses in Node.js?
Use client.messages.stream() from the @anthropic-ai/sdk package. Listen to the 'text' event for each token, or iterate with for await over the stream. Call stream.finalMessage() to get usage stats after streaming completes.
Can I stream Claude responses in an Express API?
Yes. Set the response headers for SSE (Content-Type: text/event-stream, Cache-Control: no-cache) and write each token chunk via res.write(). Call res.end() after stream.finalMessage() resolves.
How do I handle streaming errors in Node.js?
Wrap the stream in a try/catch block. The SDK throws an APIError with a .status property for HTTP errors (429 rate limit, 500 server error). For timeout, use an AbortController and set a timeout before calling abort().
Does streaming work with CommonJS (require)?
Yes. The Anthropic SDK ships CommonJS and ESM builds. Use const Anthropic = require('@anthropic-ai/sdk').default for CommonJS, or import Anthropic from '@anthropic-ai/sdk' for ESM/TypeScript.

Free tools

Cost Calculator → Prompt-Pricing Recommender → Diff Summarizer → Skills Browser →

Related

Claude Opus 4.7 vs Sonnet 4.6 Pricing (2026 Comparison)How Much Does Claude Cost? (2026 API Pricing Guide)Claude Prompt Caching: 90% Cost Savings Explained (2026)Claude API Cost Calculator: Estimate Your Anthropic BillClaude vs GPT-4 Pricing: 2026 API Cost Comparison