1. Open a stream
stream returns tokens as they are generated, instead of waiting for the whole response.
const stream = client.messages.stream({Typorithm · medium
Step 1 of 8 · intro card
Context & Tokens
Streaming returns tokens as they are generated, giving responsive UIs and a lower time-to-first-token. You iterate the event stream — content_block_delta events carry the text. The final message is also available when the stream completes.
A non-streaming request waits for the entire response before returning anything. Streaming instead emits tokens as the model produces them, so the user sees output almost immediately — a much lower time-to-first-token.
You open the stream and iterate it with for await. The stream emits typed events; content_block_delta events carry the incremental text, which you append to the growing response as each chunk arrives.
When the stream finishes, the SDK still gives you the complete final message — so you can render live deltas for responsiveness and read the assembled result and usage once it is done.
Streaming emits tokens as they are generated.
Lower time-to-first-token means a responsive UI.
Iterate events; content_block_delta carries the text.
const stream = client.messages.stream({
model: "claude-opus-4-8",
max_tokens: 1024,
messages,
});
for await (const event of stream) { /* token deltas */ }Connect each part of the code to the concept before you type.
stream returns tokens as they are generated, instead of waiting for the whole response.
const stream = client.messages.stream({max_tokens still caps the response length; streaming just delivers those tokens incrementally.
max_tokens: 1024,You pass the same messages as a normal request — only the delivery is incremental.
messages,You iterate the event stream; content_block_delta events carry the text as it arrives.
for await (const event of stream) { /* token deltas */ }Each delta is a small chunk of text; concatenating them rebuilds the full response.
/* token deltas */Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.