CCDV-F Domain 5: Streaming Responses & Real-Time UX
Implementing real-time token streaming with Server-Sent Events (SSE), event lifecycle management, incremental JSON parsing, and SDK helpers.
1. Enabling Streaming via `stream: true`
By passing "stream": true in your API payload, Anthropic immediately begins streaming tokens as Server-Sent Events (SSE) over an open HTTP connection. This eliminates perceived latency and reduces Time-to-First-Token (TTFT) from seconds to milliseconds.
2. Anthropic SSE Event Stream Lifecycle
| Event Name | Payload Content | Client Handling Action |
|---|---|---|
message_start |
Initial message metadata (id, model, role, empty usage). | Initialize message state and UI container. |
content_block_start |
Indicates the start of a text block or tool_use block. | Prepare text buffer or tool caller accumulator. |
content_block_delta |
Contains the actual incremental text: delta.text. |
Append text to UI stream in real-time. |
content_block_stop |
Indicates completion of the current content block. | Finalize current block formatting. |
message_delta |
Final metadata, stop_reason, and final usage output tokens. | Record token billing and finish stream. |
message_stop |
Final termination marker for the entire stream. | Close connection safely. |
3. Using Official SDK Stream Helpers
In TypeScript and Python, Anthropic SDKs provide high-level stream abstractions:
const stream = await anthropic.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Explain quantum entanglement.' }],
stream: true,
});
for await (const messageStreamEvent of stream) {
if (messageStreamEvent.type === 'content_block_delta' &&
messageStreamEvent.delta.type === 'text_delta') {
process.stdout.write(messageStreamEvent.delta.text);
}
}