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

  1. message_start - a message object with empty content and the initial usage.
  2. One or more content blocks, each made of:
    • content_block_start - the block's index and its initial shape.
    • content_block_delta - zero or more incremental updates.
    • content_block_stop - the block is complete.
  3. message_delta - the final stop_reason, stop_sequence and cumulative usage.
  4. message_stop - the stream closes.

ping events can appear anywhere in the stream. Ignore them.

Event reference

EventPayload
message_startmessage: id, type, role, model, empty content, stop_reason: null, usage.
content_block_startindex, content_block (text, tool_use, thinking or redacted_thinking).
content_block_deltaindex, delta (see Delta types).
content_block_stopindex.
message_deltadelta.stop_reason, delta.stop_sequence, usage.
message_stopNo extra fields.
pingNo extra fields.
errorerror (see Error events).

Delta types

delta.typeBlockField
text_deltatexttext - a text fragment.
input_json_deltatool_usepartial_json - a fragment of the tool input JSON.
thinking_deltathinkingthinking - a fragment of thinking text.
signature_deltathinkingsignature - the block's signature, sent once before content_block_stop.
citations_deltatextcitation - 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

ValueMeaning
end_turnThe model finished its turn.
max_tokensHit max_tokens.
stop_sequenceOne of your stop_sequences was generated.
tool_useThe model wants to call one or more tools - run them and send the results back in a new request.
refusalThe 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.

FieldMeaning
input_tokensInput tokens that were not read from or written to the prompt cache.
cache_creation_input_tokensInput tokens written to the prompt cache this turn.
cache_read_input_tokensInput tokens read from the prompt cache.
output_tokensTokens 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.codeerror.typeMeaning
upstream_errorapi_errorThe model failed or the stream was cut. Safe to retry.
content_filterinvalid_request_errorThe model's safety filter blocked the request or output.
context_length_exceededinvalid_request_errorThe request is too large for the model's context window.
request_too_largeinvalid_request_errorThe 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.