When creating a Message, you can set "stream": true
to incrementally stream the response using server-sent events (SSE).
Streaming with SDKs
Our Python and Typescript SDKs offer multiple ways of streaming. The Python SDK allows both sync and async streams. See the documentation in each SDK for details.
import anthropic
client = anthropic.Anthropic()
with client.messages.stream(
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}],
model="claude-3-opus-20240229",
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic();
await client.messages.stream({
messages: [{role: 'user', content: "Hello"}],
model: 'claude-3-opus-20240229',
max_tokens: 1024,
}).on('text', (text) => {
console.log(text);
});
Event types
Each server-sent event includes a named event type and associated JSON data. Each event will use an SSE event name (e.g. event: message_stop
), and include the matching event type
in its data.
Each stream uses the following event flow:
message_start
: contains aMessage
object with emptycontent
.- A series of content blocks, each of which have a
content_block_start
, one or morecontent_block_delta
events, and acontent_block_stop
event. Each content block will have anindex
that corresponds to its index in the final Messagecontent
array. - One or more
message_delta
events, indicating top-level changes to the finalMessage
object. - A final
message_stop
event.
Ping events
Event streams may also include any number of ping
events.
Error events
We may occasionally send errors in the event stream. For example, during periods of high usage, you may receive an overloaded_error
, which would normally correspond to an HTTP 529 in a non-streaming context:
event: error
data: {"type": "error", "error": {"type": "overloaded_error", "message": "Overloaded"}}
Other events
In accordance with our versioning policy, we may add new event types, and your code should handle unknown event types gracefully.
Raw HTTP Stream response
We strongly recommend that use our client SDKs when using streaming mode. However, if you are building a direct API integration, you will need to handle these events yourself.
curl https://api.anthropic.com/v1/messages \
--header "anthropic-version: 2023-06-01" \
--header "anthropic-beta: messages-2023-12-15" \
--header "content-type: application/json" \
--header "x-api-key: $ANTHROPIC_API_KEY" \
--data \
'{
"model": "claude-3-opus-20240229",
"messages": [{"role": "user", "content": "Hello"}],
"max_tokens": 256,
"stream": true
}'
event: message_start
data: {"type": "message_start", "message": {"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY", "type": "message", "role": "assistant", "content": [], "model": "claude-3-opus-20240229", "stop_reason": null, "stop_sequence": null, "usage": {"input_tokens": 25, "output_tokens": 1}}}
event: content_block_start
data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}
event: ping
data: {"type": "ping"}
event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Hello"}}
event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "!"}}
event: content_block_stop
data: {"type": "content_block_stop", "index": 0}
event: message_delta
data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence":null}, "usage": {"output_tokens": 15}}
event: message_stop
data: {"type": "message_stop"}