# Streaming answers (/docs/knowledge/streaming-answers)
A full answer can take a few seconds to write. Streaming sends it piece by piece as it's written, so your UI can show the first words at once instead of waiting for the end.

## Stream an answer [#stream-an-answer]

Pass `stream` to `query` and read the pieces as they arrive:

<CodeTabs>
  <Tab value="Python">
    ```python
    from docklee import Lynk

    lynk = Lynk(engine_id="your-engine-id")
    stream = await lynk.query("Summarize the refund policy.", stream=True)
    async for token in stream:
        print(token, end="", flush=True)
    ```
  </Tab>
  <Tab value="TypeScript">
    ```ts
    import { Lynk } from 'docklee';

    const lynk = new Lynk({ engineId: 'your-engine-id' });
    const stream = (await lynk.query('Summarize the refund policy.', {
      stream: true,
    })) as AsyncGenerator<string>;
    for await (const token of stream) {
      process.stdout.write(token);
    }
    ```
  </Tab>
  <Tab value="cURL">
    ```bash
    curl -N https://api.docklee.com/v1/engine/query/stream \
      -H "Authorization: Bearer $DOCKLEE_API_KEY" \
      -H "Content-Type: application/json" \
      -d '{"engine_id": "your-engine-id", "query": "Summarize the refund policy.", "top_k": 8}'
    ```
  </Tab>
</CodeTabs>

In Python, streaming is async only. There's no `_sync` version. With cURL, `-N` turns off buffering so you see each event as it arrives.

## Events [#events]

Over HTTP, `POST /v1/engine/query/stream` responds with server-sent events:

```text
event: token
data: {"delta": "Customers can request "}

event: token
data: {"delta": "a refund within 30 days."}

event: final
data: {"answer": "Customers can request a refund within 30 days.", "citations": [{"chunk_id": "c2a8f5e1-7b39-4d6c-a014-8e3f2b9d61c7", "page_number": 4, "section_title": "Refunds and returns"}], "confidence": 0.93}

event: done
data: {"ok": true}
```

| Event | Data | Meaning |
| --- | --- | --- |
| `token` | `delta` | The next piece of the answer. |
| `final` | `answer`, `citations`, `confidence` | The complete, checked answer. Sent once, after the last `token`. |
| `done` | `ok` | The stream is finished. |
| `error` | `detail` | The answer couldn't be finished. No `final` follows. |

The SDK reads the events for you and yields each `delta` in order. It stops at `done`.

<Callout type="warn">
Some answers arrive whole in `final`, with no `token` events before it, such as a [Precision Mode](/docs/knowledge/retrieval/precision-mode) answer or a reply that the information isn't available. Over HTTP, always read `final` for the complete answer.
</Callout>

## Mode while streaming [#mode-while-streaming]

A streamed answer always uses the engine's **Answer Mode** from its settings in the dashboard. The `mode` you pass is used by `query` without `stream`. See [Answer modes](/docs/knowledge/answers/answer-modes).

## Citations and confidence [#citations-and-confidence]

Citations and confidence are only known once the answer is complete. Read them from the `final` event over HTTP, or call `query` without `stream` when you need them in the SDK.

## Next steps [#next-steps]

Your UI can show answers as they're written. Choose how strictly they stay within your sources next.

<Cards>
  <Card title="Answer modes" href="/docs/knowledge/answers/answer-modes" description="Grounded, blended, and retrieval only." />
  <Card title="Citations and confidence" href="/docs/knowledge/answers/citations-and-confidence" description="What the final event tells you." />
  <Card title="Query an engine" href="/docs/knowledge/query-an-engine" description="Get the whole answer in one response." />
</Cards>
