Skip to main content

Anthropic Streaming

Streaming can improve time to first visible output; it does not inherently reduce token cost. Parse provider events, error events and terminal markers, and handle cancellation/disconnection. Do not treat initial HTTP 200 as terminal success.

Final usage/cost and incomplete-stream outcomes are verified in Request Logs after the stream ends. Final-cost HTTP headers are not promised. Use bounded network timeouts and avoid automatic retries after partial output unless your application can handle duplicates.

While a monthly token quota or spending cap applies (always on Founding), a stream must target a model in the Makas model catalog so its maximum usage can be bounded; otherwise it is rejected before dispatch with 422 UNENFORCEABLE_STREAM_USAGE. A 503 USAGE_SETTLEMENT_UNAVAILABLE is temporary; retry later with backoff.

Response header reference.

Start from the complete quickstart. This fragment changes that request to streaming; it is not a complete standalone program.

Set stream: true on Messages and consume provider events through message_stop.

Keep both auth headers from the quickstart. Provider-generated error events or an interrupted connection can occur after HTTP headers have been sent.