streaming

Streams the response token by token as it is generated, reducing time-to-first-token for a better user experience.

Syntax

claude-api
client.messages.stream({ ... }).on("text", callback)

Example

claude-api
const stream = client.messages.stream({
  model: "claude-opus-4-5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Tell me a story" }]
});

for await (const chunk of stream) {
  if (chunk.type === "content_block_delta") {
    process.stdout.write(chunk.delta.text);
  }
}