When making an API request, you can set "stream": true
to incrementally stream the response using server-sent events (SSE). If you are using our client libraries, parsing these events will be handled for you automatically. However, if you are building a direct API integration, you will need to handle these events yourself.
Example
curl --request POST \
--url https://api.anthropic.com/v1/complete \
--header "anthropic-version: 2023-06-01" \
--header "content-type: application/json" \
--header "x-api-key: $ANTHROPIC_API_KEY" \
--data '
{
"model": "claude-2",
"prompt": "\n\nHuman: Hello, world!\n\nAssistant:",
"max_tokens_to_sample": 256,
"stream": true
}
'
event: completion
data: {"completion": " Hello", "stop_reason": null, "model": "claude-2.0"}
event: completion
data: {"completion": "!", "stop_reason": null, "model": "claude-2.0"}
event: ping
data: {}
event: completion
data: {"completion": " My", "stop_reason": null, "model": "claude-2.0"}
event: completion
data: {"completion": " name", "stop_reason": null, "model": "claude-2.0"}
event: completion
data: {"completion": " is", "stop_reason": null, "model": "claude-2.0"}
event: completion
data: {"completion": " Claude", "stop_reason": null, "model": "claude-2.0"}
event: completion
data: {"completion": ".", "stop_reason": null, "model": "claude-2.0"}
event: completion
data: {"completion": "", "stop_reason": "stop_sequence", "model": "claude-2.0"}
Events
Each event includes a named event type and associated JSON data.
Handling unknown event types
We currently send
completion
,ping
, anderror
event types, but this list may expand over time, and your integration must ignore any unexpected event types.
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: completion
data: {"completion": " Hello", "stop_reason": null, "model": "claude-2.0"}
event: error
data: {"error": {"type": "overloaded_error", "message": "Overloaded"}}
Older API versions
If you are using an API version prior to 2023-06-01
, the response shape will be different. See versioning for details.