Stream replies
Stream with server-sent events and read the exact charge from the final ec event.
Turn streaming on
- Screenstream: true
- ActionSSE
- ResultTokens arrive
Send a messageIt streams, then shows the receipt
Example dataSet stream: true in either format. Clients that read OpenAI or Anthropic streams work unchanged, with one caution: the OpenAI SDKs hand the extra ec event to your loop, so guard for chunks without choices.
Read the final ec event
- ScreenLast provider event
- Actionevent: ec
- ResultReceipt and charge
Read the receipt lineTokens, total and the receipt id
Example dataStream headers carry the hold estimate (x-ec-hold-micro and estimated parts). The exact charge comes last as event: ec with { receipt, costMicro, feeMicro, markupMicro, funding }; OpenAI streams then end with data: [DONE].
data: {"id":"…","object":"chat.completion.chunk","model":"pad/JPX","choices":[{"delta":{"content":"Hello"}}]}
data: {"choices":[],"usage":{"prompt_tokens":24,"completion_tokens":12}}
event: ec
data: {"receipt":"r_…","costMicro":41205,"feeMicro":2061,"markupMicro":6181,"funding":"balance"}
data: [DONE]Stop a stream
- ScreenClose the connection
- ActionUpstream aborted
- ResultPay for what was generated
Tap StopOnly the generated part is charged
Example dataHanging up before the first token releases the hold and charges nothing. After output started, the generated part is charged and the receipt records client_closed.
Failures inside a stream
- ScreenStream open
- ActionModel fails
- ResultError event, then ec
Call a paused endpointRefused before any charge
Example dataOnce headers are sent the status stays 200. A failure before any output arrives as an error event (upstream_unavailable, nothing charged). A stream cut after output is charged and marked stream_interrupted. The gateway gives up after 120 seconds without a chunk.