Skip to navigation

Streaming

Send "stream": true and Meadow relays the provider’s server-sent events to you as they arrive, unchanged. Each format keeps its own event shapes, so your existing stream parser works.

Next step: run one streamed call and check that it ends with the format’s final event.

curl -sS -N "$MEADOW_BASE_URL/v1/chat/completions" \
-H "Authorization: Bearer $MEADOW_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3.1-flash-lite-preview",
"messages": [{"role": "user", "content": "Say hello in five words."}],
"max_tokens": 32,
"stream": true
}'
data: {"choices":[{"delta":{"content":"Hello, it is nice meeting.","role":"assistant"},"index":0}],"created":1795472116,"id":"87oEa82U...","model":"gemini-3.1-flash-lite-preview","object":"chat.completion.chunk"}
data: {"choices":[{"delta":{"extra_content":{"google":{"thought_signature":"EqkBCqYB..."}},"role":"assistant"},"finish_reason":"stop","index":0}],"created":1795472116,"id":"87oEa82U...","model":"gemini-3.1-flash-lite-preview","object":"chat.completion.chunk"}
data: [DONE]

The response has content-type: text/event-stream and the same x-meadow-provider, x-meadow-model, and x-meadow-attempts headers as a blocking call. The headers arrive before the first event.

Each format’s stream

FormatEventsFinal event to check for
Chat Completionsdata: lines with chat.completion.chunk objectsdata: [DONE]
ResponsesTyped events: response.created, response.output_text.delta, and so onresponse.completed (or response.incomplete)
MessagesNamed events: message_start, content_block_delta, message_delta, and so onevent: message_stop
Gemini streamGenerateContentdata: lines, each a GenerateContentResponse chunkA chunk with finishReason on its candidates. There is no end marker.

Meadow does not check that a stream reached its final event. Your client must.

Chat Completions

Usage arrives in a stream only if you ask for it, as at OpenAI: send "stream_options": {"include_usage": true}. Meadow asks the provider for the count on every stream and removes the usage-only chunk when you did not ask.

Responses

curl -sS -N "$MEADOW_BASE_URL/v1/responses" \
-H "Authorization: Bearer $MEADOW_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "grok-4.7", "input": "Say hello in five words.", "max_output_tokens": 64, "stream": true}'
event: response.created
data: {"sequence_number":0,"type":"response.created","response":{"id":"8aebadcd-...","model":"grok-4.7","object":"response","output":[],"status":"in_progress", ...}}
event: response.in_progress
data: {"sequence_number":1,"type":"response.in_progress", ...}
event: response.output_item.added
data: {"sequence_number":2,"type":"response.output_item.added","item":{"id":"rs_00002b34-...","summary":[],"type":"reasoning","status":"in_progress"},"output_index":0}
...
event: response.completed
data: {"sequence_number":21,"type":"response.completed","response":{"id":"8aebadcd-...","status":"completed","usage":{"input_tokens":1248,"output_tokens":93,"total_tokens":1341}, ...}}

Providers differ in detail. OpenRouter sends the same events as bare data: lines with no event: line. Read the type field inside the data.

Messages

curl -sS -N "$MEADOW_BASE_URL/v1/messages" \
-H "x-api-key: $MEADOW_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-haiku-5-5",
"max_tokens": 64,
"stream": true,
"messages": [{"role": "user", "content": "Say hello in five words."}]
}'
event: message_start
data: {"type":"message_start","message":{"model":"claude-haiku-5-5","id":"msg_0000f89e...","type":"message","role":"assistant","content":[],"stop_reason":null,"usage":{"input_tokens":16,"output_tokens":8}}}
event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
event: ping
data: {"type": "ping"}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello there"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":", friend! How are you?"}}
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":{"input_tokens":16,"output_tokens":14}}
event: message_stop
data: {"type":"message_stop"}

The output is trimmed. With the mdw CLI (Clients), mdw models call --stream prints the text as it arrives:

mdw models call --model claude-haiku-5-5 --format messages --max-tokens 64 --stream "Say hello in five words."
served by anthropic (claude-haiku-5-5) after 1 attempt(s)
Hello there, friend! How are you?

Before the first event

Meadow holds a stream until the provider’s first complete event has arrived (up to 64 KiB). Until then the call can still fall back to the next provider, and a failure is a normal JSON error with a 4xx or 5xx status, not a stream.

Three things follow:

  • You see no bytes until the first event is complete. The first event arrives in one piece.
  • A provider that opens a 200 stream with its own error event counts as failed, and Meadow tries the next provider.
  • On a Responses stream the opening response.created, response.queued, and response.in_progress events are held too, until the event after them arrives, because a provider reports its error there.

A streamed call that fails before the first event:

curl -sS -i "$MEADOW_BASE_URL/v1/chat/completions" \
-H "Authorization: Bearer $MEADOW_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "gemini-3.1-flash-lite-preview", "provider": {"only": ["openrouter"]}, "messages": [{"role": "user", "content": "hi"}], "max_tokens": 32, "stream": true}'
HTTP/2 502
content-type: application/json
x-request-id: req_00009af3
{"error":{"message":"all providers in route failed","type":"server_error","param":null,"code":"all_providers_failed","attempts":[{"detail":"provider returned HTTP 404","duration_ms":99,"outcome":"unsupported","provider":"openrouter"}]},"request_id":"req_00009af3"}

So check the status before you parse events. A fallback before the first event is invisible apart from x-meadow-attempts: the same request with "provider": {"order": ["openrouter", "google"]} streamed from Google with x-meadow-attempts: 2.

After the first event

Once the first event has been sent to you there is no fallback. If the provider’s connection breaks, goes quiet for 5 minutes, or ends in the middle of an event, Meadow writes one last event in your request’s format and ends the response. The status is still 200. A provider event that arrived only in part is dropped, so every event you receive is whole.

FormatThe last event of a broken stream
Chat CompletionsA chunk with a top-level error and finish_reason: "error", as on OpenRouter. No data: [DONE] follows.
Responsesevent: response.failed, with response.status: "failed" and response.error.code: "server_error".
Messagesevent: error, with {"type":"error","error":{"type":"api_error","message":"..."}}.
GeminiA data: line with Google’s error object: {"error":{"code":503,"message":"...","status":"UNAVAILABLE"}}.

A Chat Completions example:

data: {"choices":[{"delta":{"content":""},"finish_reason":"error","index":0}],"created":1795492591,"error":{"code":502,"message":"the provider's stream broke before the response was complete; nothing more will arrive, so send the request again","metadata":{"error_type":"provider_unavailable"},"type":"provider_error"},"id":"chatcmpl-77","model":"grok-4.7","object":"chat.completion.chunk","provider":"xai"}

The OpenAI and Anthropic SDKs raise an API error when they read these events. The example comes from the server’s tests. No provider broke a stream midway on the hosted beta, so this event has not been seen there.

Do: treat the error event, or a stream that ends without its final event, as a failed call, and send the whole call again if you want the response. Meadow cannot resume a stream. A provider’s own error event in the middle of a stream is relayed like any other event, and Meadow adds nothing after it. If the event names the key or token Meadow sent to that provider, Meadow replaces each copy with [REDACTED] first. To catch a copy split across two reads, Meadow holds back bytes that could be the start of that credential until the next read or the end of the stream. It holds at most one byte fewer than the credential, and holds nothing when the bytes cannot start it.

What a dropped stream costs

If you close the connection before the stream ends, Meadow closes its connection to the provider. Two costs remain.

The provider may bill you for what it generated before it noticed. That is between you and the provider.

On a key with a token limit, Meadow charges the key the tokens it reserved for the call, not the smaller number generated so far. The reservation is the output token limit you sent plus about a third of the request’s size in bytes. This run used two keys with max_tokens: 1000 each. Both sent the same streamed request with an output token limit of 800, which reserves 848 tokens. The first client stopped after 2 seconds:

curl -sS -N --max-time 2 -o /dev/null "$MEADOW_BASE_URL/v1/chat/completions" \
-H "Authorization: Bearer $LIMITED_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"gemini-3.1-flash-lite-preview","messages":[{"role":"user","content":"Write 400 words about rivers."}],"max_tokens":800,"stream":true}'
curl: (28) Operation timed out after 2006 milliseconds with 4723 bytes received

The provider had sent only part of the response by then. GET /v1/key showed tokens_used: 848 on that key. The next request on it reserved 343 tokens and was rejected, because 848 + 343 is more than 1000:

{"error":{"message":"api key max_tokens limit would be exceeded: 848 of 1000 tokens used and 0 held by calls in flight on this key, and this request reserves 343 tokens; lower its output token cap or shrink it","type":"invalid_request_error","param":null,"code":"key_token_limit_exceeded"},"request_id":"req_0000c499"}

The second client read its stream to the end. The provider reported 466 tokens, Meadow charged 466, and the same follow-up request was served.

Do: read streams to the end on limited keys, and keep output token limits close to what you need. A key with no limit is not charged anything by Meadow. Token and request limits has the full rule.

Time limits

These are the values on the hosted beta. You cannot change them.

LimitValueWhat happens when it passes
Your request body must arrive60 secondsThe connection is closed.
First byte of a stream from one provider3 minutesMeadow tries the next provider. A keep-alive comment counts as a byte.
First complete event, or the full answer of a blocking call, from one provider10 minutesMeadow tries the next provider.
All attempts together, on a key with a request, token or spend limit8 minutes to the first event or full answer502. The key is charged if a provider had the request.
Gap between bytes in a running stream5 minutesMeadow cuts your connection.
Your client stops reading60 seconds without progressMeadow cuts your connection.
Whole request, stream included60 minutesThe load balancer in front of Meadow answers 504 with the text stream timeout. That response is not JSON and has no x-request-id. Seen on a request that had sent nothing back yet. A stream that has already started cannot get a new status, so expect its connection to be cut. That case was not tried.

Three more things affect long calls:

  • A blocking call sends nothing until the whole answer is ready, so a slow model can sit for the full 10 minutes on one provider before Meadow tries the next. A route of three providers can take 30 minutes. Stream long answers.
  • When Meadow is updated, a stream that is still open after a few seconds is ended with one last error event in the format of the stream: a chunk with a top-level error and finish_reason: "error" on Chat Completions, an error event on Responses and on Messages. The event’s message says the server is restarting. Nothing follows it, so there is no [DONE]. The call may have been billed by the provider.
  • A stream counts as an open request of your account until it ends. See Token and request limits.

None of the time limits was reached while testing this page, and no provider broke a stream midway: those rows and the section on what happens after the first event are read from the server’s code and settings. The dropped stream and the fallback before the first event were run for real.

More in Known limits.

API reference

Operations on this page: createChatCompletion, createResponse, createMessage.