Streaming Events
Named server-sent events emitted by POST /v1/messages when stream: true is enabled.
When stream: true is set on POST /v1/messages, Aivene keeps the
connection open and pushes the reply as it is generated using
server-sent events.
Unlike Chat Completions streaming,
every event is named and the stream has no [DONE] sentinel: it simply
closes after message_stop.
Enabling streaming
Set stream: true in the request body. The response arrives as
Content-Type: text/event-stream:
{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [{ "role": "user", "content": "Hi" }],
"stream": true
}Consuming the stream
The simplest path is the official Anthropic SDK:
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.AIVENE_API_KEY,
baseURL: 'https://api.aivene.com'
});
const stream = client.messages.stream({
model: 'claude-sonnet-4-6',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hi' }]
});
stream.on('text', (text) => process.stdout.write(text));
const message = await stream.finalMessage();
console.log(message.usage);import os
from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["AIVENE_API_KEY"],
base_url="https://api.aivene.com",
)
with client.messages.stream(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "Hi"}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
message = stream.get_final_message()
print(message.usage)Wire format
Each event is an event: line with the event name, a data: line with the
JSON payload, and a blank line. The payload's type always equals the
event name:
event: message_start
data: {"type":"message_start","message":{"id":"avn_...","type":"message","role":"assistant","model":"claude-sonnet-4-6","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":12,"output_tokens":1}}}
event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
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":" there!"}}
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":6}}
event: message_stop
data: {"type":"message_stop"}The message id is stable for the whole stream.
Stream lifecycle
message_start- amessageobject with emptycontentand the initialusage.- One or more content blocks, each made of:
content_block_start- the block'sindexand its initial shape.content_block_delta- zero or more incremental updates.content_block_stop- the block is complete.
message_delta- the finalstop_reason,stop_sequenceand cumulativeusage.message_stop- the stream closes.
ping events can appear anywhere in the stream. Ignore them.
Event reference
| Event | Payload |
|---|---|
message_start | message: id, type, role, model, empty content, stop_reason: null, usage. |
content_block_start | index, content_block (text, tool_use, thinking or redacted_thinking). |
content_block_delta | index, delta (see Delta types). |
content_block_stop | index. |
message_delta | delta.stop_reason, delta.stop_sequence, usage. |
message_stop | No extra fields. |
ping | No extra fields. |
error | error (see Error events). |
Delta types
delta.type | Block | Field |
|---|---|---|
text_delta | text | text - a text fragment. |
input_json_delta | tool_use | partial_json - a fragment of the tool input JSON. |
thinking_delta | thinking | thinking - a fragment of thinking text. |
signature_delta | thinking | signature - the block's signature, sent once before content_block_stop. |
citations_delta | text | citation - a citation for the current text block. |
Concatenate the deltas of each index to rebuild its block. Blocks are
emitted in order and their index matches their position in the final
content array.
Stop reasons
| Value | Meaning |
|---|---|
end_turn | The model finished its turn. |
max_tokens | Hit max_tokens. |
stop_sequence | One of your stop_sequences was generated. |
tool_use | The model wants to call one or more tools - run them and send the results back in a new request. |
refusal | The model declined to answer. |
Tool use streaming
A tool call starts with a tool_use block whose input is empty, then
streams its input as JSON fragments:
event: content_block_start
data: {"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"toolu_01A09q90qw90lq917835lq9","name":"get_weather","input":{}}}
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"city\":"}}
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\"Tokyo\"}"}}
event: content_block_stop
data: {"type":"content_block_stop","index":1}Join the partial_json fragments of one index and parse the result once
content_block_stop arrives. The message_delta of that turn carries
stop_reason: "tool_use".
Thinking streaming
With thinking enabled, thinking blocks come before the answer. Their
text arrives as thinking_delta events and their signature as a single
signature_delta right before content_block_stop. Keep the full block,
signature included, and send it back unchanged in the next assistant turn.
See Reasoning.
Token usage in a streamed response
message_start carries the input side of usage. message_delta carries
the final, cumulative counts, including output_tokens. Read usage from
message_delta, or from the final message the SDK assembles.
| Field | Meaning |
|---|---|
input_tokens | Input tokens that were not read from or written to the prompt cache. |
cache_creation_input_tokens | Input tokens written to the prompt cache this turn. |
cache_read_input_tokens | Input tokens read from the prompt cache. |
output_tokens | Tokens generated, thinking and tool input included. |
Error events
If the model fails after the stream has started, you receive an error
event and the stream closes without message_stop:
event: error
data: {"type":"error","error":{"type":"api_error","message":"The model is temporarily unavailable. Please try again later.","code":"upstream_error"}}error.code | error.type | Meaning |
|---|---|---|
upstream_error | api_error | The model failed or the stream was cut. Safe to retry. |
content_filter | invalid_request_error | The model's safety filter blocked the request or output. |
context_length_exceeded | invalid_request_error | The request is too large for the model's context window. |
request_too_large | invalid_request_error | The request has more bytes of text than the model accepts. Shorten the conversation, attachments, or tool output. |
The Anthropic SDKs raise these as errors. If you parse the stream yourself,
handle the error event alongside the normal ones.