# Streaming (SSE)

> Receive responses incrementally with stream true on GoiAPI: SSE format for the OpenAI and Anthropic APIs, keep-alive pings and mid-stream errors.

Source: https://goiapi.com/en/docs/streaming

Add `"stream": true` to the body to receive the response incrementally over Server-Sent Events (SSE). Works with [`/v1/chat/completions`](https://goiapi.com/en/docs/openai-chat-completions), [`/v1/responses`](https://goiapi.com/en/docs/openai-responses) and [`/v1/messages`](https://goiapi.com/en/docs/anthropic-messages). The `/v1/responses` stream uses the standard Responses events (`response.created` … `response.completed`), and a mid-stream error is a `response.failed` event.

## How it works

- The `200` headers are sent right away (`content-type: text/event-stream`), before the model starts answering.
- Data chunks keep the SSE format of the API you call (OpenAI or Anthropic), so SDKs read the stream as usual.
- OpenAI format: the stream always ends with a chunk that contains `usage` (tokens used).
- If the model has not sent anything for 15 seconds, a `: ping` comment line is sent so the connection is not dropped. Standard SSE parsers ignore it.

## Mid-stream errors

If an error happens after the `200` headers have been sent, it arrives as an event in the stream.

Anthropic format:

```text
event: error
data: {"type":"error","error":{"type":"overloaded_error","message":"…"}}
```

OpenAI format:

```text
data: {"error":{"message":"…","type":"server_error","code":"service_unavailable","param":null}}
```

## Billing

- If you disconnect after receiving part of the response, the request stops. You are charged for the tokens used up to that point (input plus the output generated so far), subject to the minimum charge per request.
- If you cancel after the request has been sent to the model but before any content arrives, you are charged for the input tokens (including any reported cache tokens) or the minimum charge per request, whichever is higher. Output tokens are not charged.
- A request (streaming or not) runs for at most 15 minutes.
