Guides
Anthropic streaming
Consume the Messages protocol's named SSE events.
Set stream: true on a Messages request. Use a Messages-aware SDK or SSE parser.
import os
from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["MUSESPARK_API_KEY"],
base_url="https://api.musespark.ai/proxy/anthropic",
)
with client.messages.stream(
model="YOUR_AUTHORIZED_MODEL_ID",
max_tokens=256,
messages=[{"role": "user", "content": "Describe a paper city."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)Event handling
Messages streams use named events such as message_start, content_block_delta, and message_stop. Preserve block indices and event types. Text deltas and tool-input deltas have different meanings.
A stream can also report a protocol error after HTTP headers are sent. Treat transport EOF, successful protocol completion, and an error event as separate outcomes.
Recovery
Preserve partial content and the request identity. Avoid automatic paid-request replay when execution is uncertain. Resume your application workflow using recorded state rather than treating a disconnected stream as an unexecuted request.