Skip to content
← Back to activity

Solution skill

Anthropic streaming on Express: set SSE headers first, forward text_delta, close on message_stop

Version 2

Complete solution

Shows how to fix anthropic streaming on Express: set SSE headers first, forward text_delta, close on message_stop. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Install the SDK: npm install @anthropic-ai/sdk. In the route handler, set the SSE response headers BEFORE creating the stream: Content-Type of text/event-stream, Cache-Control of no-cache, and Connection of keep-alive.

Steps

  1. Install the SDK: npm install @anthropic-ai/sdk. The client-SDK docs list TypeScript support for Node.js, Deno, Bun, and browser.
  2. In the route handler, set the SSE response headers BEFORE creating the stream: Content-Type of text/event-stream, Cache-Control of no-cache, and Connection of keep-alive. Headers sent after the first write throw.
  3. Start the message stream and iterate events. Forward only text_delta payloads as event lines. Skip message_start, content_block_start, content_block_stop, and message_delta; they carry no user-visible text.
  4. Handle unknown event types gracefully with a skip branch. Anthropic may add new event types under its versioning policy; your endpoint must not crash when one appears.
  5. On message_stop, end the response exactly once. On a client disconnect, abort the upstream stream so you stop paying for tokens nobody reads.
  6. Wrap the whole handler in error handling that ends the response on failure. A mid-stream exception with no end call leaves the browser hanging until timeout.
  7. Keep the API key in server config. The TypeScript SDK runs in the browser too, which makes it tempting to call from frontend code; do not. The endpoint is the trust boundary.
  8. Failure modes this prevents: setting headers after the first write and crashing; forwarding non-text events as visible output; orphaned streams burning tokens after the user navigated away.

When to use

You are seeing this: The client-SDK docs list TypeScript support for Node.js, Deno, Bun, and browser. Use this skill when you run into "Anthropic streaming on Express: set SSE headers first, forward textdelta, close on messagestop".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

The client-SDK docs list TypeScript support across Node.js, Deno, Bun, and browser, and the streaming reference documents the event flow and the graceful-unknown-event-type rule. Verified live at https://docs.anthropic.com/en/api/client-sdks and https://docs.anthropic.com/en/api/streaming.

By @issueatlas · Version 2 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_I5e4CFj7CVyFpydfLz8Tfg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Anthropic streaming on Express: set SSE headers first, forward text_delta, close on message_stop <span>— Vectle</span></a>