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

# Streaming

> Parse streaming responses from the Tiny Talk API in real time.

The [Chat Completions](/api-reference/chat-completions) endpoint streams responses by default using **server-sent events (SSE)**. Each chunk is prefixed with `data: ` and the stream ends with `data: [DONE]`.

## Streaming

Parse the SSE stream in real time. Each chunk contains a `delta` object with incremental content.

<CodeGroup>
  ```javascript JavaScript theme={null}
  const response = await fetch("https://api.tinytalk.ai/v1/chat/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "api-key": "tiny_sk_your_key_here"
    },
    body: JSON.stringify({
      botId: "your-agent-id",
      messages: [
        { role: "user", content: "What are your business hours?" }
      ]
    })
  });

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

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

    const chunk = decoder.decode(value);
    const lines = chunk.split("\n").filter((line) => line.startsWith("data: "));

    for (const line of lines) {
      const data = line.replace("data: ", "");
      if (data === "[DONE]") break;

      const parsed = JSON.parse(data);
      const content = parsed.choices?.[0]?.delta?.content;
      if (content) process.stdout.write(content);
    }
  }
  ```

  ```python Python theme={null}
  import requests
  import json

  response = requests.post(
      "https://api.tinytalk.ai/v1/chat/completions",
      headers={
          "Content-Type": "application/json",
          "api-key": "tiny_sk_your_key_here"
      },
      json={
          "botId": "your-agent-id",
          "messages": [
              {"role": "user", "content": "What are your business hours?"}
          ]
      },
      stream=True
  )

  for line in response.iter_lines():
      if not line:
          continue
      decoded = line.decode("utf-8")
      if not decoded.startswith("data: "):
          continue
      data = decoded.removeprefix("data: ")
      if data == "[DONE]":
          break
      parsed = json.loads(data)
      content = parsed["choices"][0]["delta"].get("content", "")
      if content:
          print(content, end="", flush=True)
  ```
</CodeGroup>

## Non-streaming

Set `stream` to `false` to receive the complete response in a single JSON payload.

<CodeGroup>
  ```javascript JavaScript theme={null}
  const response = await fetch("https://api.tinytalk.ai/v1/chat/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "api-key": "tiny_sk_your_key_here"
    },
    body: JSON.stringify({
      botId: "your-agent-id",
      stream: false,
      messages: [
        { role: "user", content: "What are your business hours?" }
      ]
    })
  });

  const data = await response.json();
  console.log(data.choices[0].message.content);
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      "https://api.tinytalk.ai/v1/chat/completions",
      headers={
          "Content-Type": "application/json",
          "api-key": "tiny_sk_your_key_here"
      },
      json={
          "botId": "your-agent-id",
          "stream": False,
          "messages": [
              {"role": "user", "content": "What are your business hours?"}
          ]
      }
  )

  data = response.json()
  print(data["choices"][0]["message"]["content"])
  ```
</CodeGroup>
