Streaming
Server-sent events in both formats — the event shapes differ, and mixing them up reads as silence.
Set stream to true and the response becomes text/event-stream. Both endpoints stream, but they emit different event shapes.
Anthropic format
curl -N https://aiprimetech.io/v1/messages \
-H "x-api-key: $CLAUDEAPIKEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{"model":"claude-sonnet-4-6","max_tokens":512,"stream":true,
"messages":[{"role":"user","content":"Count to five"}]}'
event: message_start
data: {"type":"message_start","message":{"id":"msg_...","usage":{"input_tokens":9}}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"One"}}
event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":14}}
event: message_stop
data: {"type":"message_stop"}
Text arrives in content_block_delta events at delta.text. Final output token counts arrive in message_delta, not message_start.
OpenAI format
data: {"choices":[{"delta":{"content":"One"},"index":0}]}
data: {"choices":[{"delta":{},"finish_reason":"stop","index":0}]}
data: [DONE]
Text arrives at choices[0].delta.content, and the stream ends with a literal data: [DONE] sentinel that is not JSON.
This mismatch produces an empty response with no error: a client parsing
choices[].delta.content against a Messages stream finds nothing, every time, and reports success. If streaming appears to work but returns nothing, check which shape you are parsing.Python, Anthropic SDK
from anthropic import Anthropic
client = Anthropic(api_key=class="s">"sk-your-key", base_url=class="s">"https://aiprimetech.io")
with client.messages.stream(
model=class="s">"claude-sonnet-4-6",
max_tokens=512,
messages=[{class="s">"role": class="s">"user", class="s">"content": class="s">"Count to five"}],
) as stream:
for text in stream.text_stream:
print(text, end=class="s">"", flush=True)
Operational notes
- Use
curl -Nwhen testing by hand, or curl buffers the whole stream and it looks like nothing is happening. - Disable proxy buffering in front of your own app (
proxy_buffering offin nginx) or clients see the reply arrive in one lump at the end. - A dropped connection mid-stream still bills the tokens already generated.
- Streaming does not change the price — only when the bytes arrive.