> ## Documentation Index
> Fetch the complete documentation index at: https://docs.elumenta.ru/llms.txt
> Use this file to discover all available pages before exploring further.

# Streaming

> Get real-time token streaming for text generation using Server-Sent Events

## Overview

Elumenta supports streaming for text generation via Server-Sent Events (SSE). Instead of waiting for the full response, tokens are delivered as they're generated.

Enable streaming by setting `"stream": true` in your request.

## Basic Example

<CodeGroup>
  ```python Python theme={null}
  import requests

  def stream_text(prompt: str, model: str = "gpt-5"):
      response = requests.post(
          "https://elumenta.ru/api/v2/generate",
          headers={
              "Authorization": "Bearer nb_YOUR_API_KEY",
              "Accept": "text/event-stream"
          },
          json={
              "model": model,
              "messages": [{"role": "user", "content": prompt}],
              "stream": True
          },
          stream=True
      )
      
      full_text = ""
      for line in response.iter_lines():
          if line:
              line = line.decode("utf-8")
              if line.startswith("data: "):
                  data = line[6:]
                  if data == "[DONE]":
                      break
                  import json
                  chunk = json.loads(data)
                  if chunk.get("type") == "content_delta":
                      token = chunk["delta"]
                      full_text += token
                      print(token, end="", flush=True)
      
      print()  # newline at end
      return full_text

  stream_text("Write a short story about a robot learning to paint")
  ```

  ```javascript JavaScript theme={null}
  async function streamText(prompt) {
    const response = await fetch("https://elumenta.ru/api/v2/generate", {
      method: "POST",
      headers: {
        "Authorization": "Bearer nb_YOUR_API_KEY",
        "Content-Type": "application/json",
        "Accept": "text/event-stream"
      },
      body: JSON.stringify({
        model: "gpt-5",
        messages: [{ role: "user", content: prompt }],
        stream: true
      })
    });

    const reader = response.body.getReader();
    const decoder = new TextDecoder();

    while (true) {
      const { done, value } = await reader.read();
      if (done) break;

      const lines = decoder.decode(value).split("\n");
      for (const line of lines) {
        if (line.startsWith("data: ")) {
          const data = line.slice(6);
          if (data === "[DONE]") return;
          
          const chunk = JSON.parse(data);
          if (chunk.type === "content_delta") {
            process.stdout.write(chunk.delta);
          }
        }
      }
    }
  }
  ```

  ```bash cURL theme={null}
  curl -X POST https://elumenta.ru/api/v2/generate \
    -H "Authorization: Bearer nb_YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -H "Accept: text/event-stream" \
    -d '{
      "model": "gpt-5",
      "messages": [{"role": "user", "content": "Count to 10 slowly"}],
      "stream": true
    }'
  ```
</CodeGroup>

## SSE Event Format

Streaming responses are sent as SSE events:

```
data: {"type": "generation_start", "id": "gen_01j9x2abc123", "model": "gpt-5"}

data: {"type": "content_delta", "delta": "Here"}

data: {"type": "content_delta", "delta": " are"}

data: {"type": "content_delta", "delta": " the"}

data: {"type": "content_delta", "delta": " numbers"}

data: {"type": "generation_end", "id": "gen_01j9x2abc123", "tokens_used": 3, "balance_remaining": 297}

data: [DONE]
```

### Event Types

| Event              | Description                                     |
| ------------------ | ----------------------------------------------- |
| `generation_start` | Generation has begun, includes `id` and `model` |
| `content_delta`    | A token chunk, check `delta` field              |
| `generation_end`   | Generation complete, includes billing info      |
| `error`            | Something went wrong, includes `message`        |

## React Integration Example

```jsx theme={null}
import { useState } from "react";

function StreamingChat() {
  const [output, setOutput] = useState("");
  const [loading, setLoading] = useState(false);

  async function generate(prompt) {
    setLoading(true);
    setOutput("");

    const response = await fetch("/api/chat", {  // your proxy endpoint
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ prompt })
    });

    const reader = response.body.getReader();
    const decoder = new TextDecoder();

    while (true) {
      const { done, value } = await reader.read();
      if (done) break;

      const lines = decoder.decode(value).split("\n");
      for (const line of lines) {
        if (line.startsWith("data: ") && line !== "data: [DONE]") {
          const chunk = JSON.parse(line.slice(6));
          if (chunk.type === "content_delta") {
            setOutput(prev => prev + chunk.delta);
          }
        }
      }
    }

    setLoading(false);
  }

  return (
    <div>
      <button onClick={() => generate("Hello!")}>Generate</button>
      {loading && <span>Generating...</span>}
      <pre>{output}</pre>
    </div>
  );
}
```

## Which Models Support Streaming?

All text models support streaming. Image, video, and audio models do not — they return a URL when complete.
