anthropic.messages.stream({ … }, options?): MessageStream
anthropic.messages.stream()
returns a MessageStream
, which emits events, has an async
iterator, and exposes helper methods to accumulate stream events into a convenient shape and make it easy to reason
about the conversation.
Alternatively, you can use anthropic.messages.create({ stream: true, … })
which returns an async
iterable of the chunks in the stream and uses less memory (most notably, it does not accumulate a message
object for you).
If you need to cancel a stream, you can break
from a for await
loop or call stream.abort()
.
See an example of streaming helpers in action in examples/streaming.ts
.
The first event that is fired when the connection with the Anthropic API is established.
The event fired when a stream event is received from the API. Not fired when it is not streaming. The snapshot
returns an accumulated Message
which is progressively built-up over events.
The event fired when a text delta is sent by the API. The second parameter returns a textSnapshot
.
The event fired when a json delta is sent by the API. The second parameter returns a jsonSnapshot
.
The event fired when a message is done being streamed by the API. Corresponds to the message_stop
SSE event.
The event fired when a content block is done being streamed by the API. Corresponds to the
content_block_stop
SSE event.
The event fired for the final message. Currently this is equivalent to the message
event, but is fired after
it.
The event fired when an error is encountered while streaming.
The event fired when the stream receives a signal to abort.
The last event fired in the stream.
Aborts the runner and the streaming request, equivalent to .controller.abort()
. Calling .abort()
on a
MessageStream
will also abort any in-flight network requests.
An empty promise which resolves when the stream is done.
Returns the current state of the message that is being accumulated, or undefined
if there is no such
message.
A promise which resolves with the last message received from the API. Throws if no such message exists.
A promise which resolves with the text of the last message received from the API.
A mutable array of all messages in the conversation.
The underlying AbortController
for the runner.