# Stream an answer (/docs/api/query-stream)
<EndpointLayout>
<EndpointHeader method="POST" path="/v1/engine/query/stream" />

Takes the same body as [Query an engine](/docs/api/query) and returns the answer as it's written, ending with the complete, checked answer. `lynk.query` with `stream` calls it for you.

<EndpointExamples>
<CodeTabs>
  <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" \
      -H "Accept: text/event-stream" \
      -d '{"engine_id": "your-engine-id", "query": "Summarize the refund policy."}'
    ```
  </Tab>
  <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>
</CodeTabs>

<Tabs items={['200']}>
  <Tab value="200">
    ```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}
    ```
  </Tab>
</Tabs>
</EndpointExamples>

## Body [#body]

<ApiFields>
  <ApiField name="engine_id" type="string<uuid>" required>
    The engine to ask.
  </ApiField>
  <ApiField name="query" type="string" required>
    The question, in plain words.
  </ApiField>
  <ApiField name="top_k" type="integer" defaultValue="8">
    How many chunks to search for, from 1 to 50.
  </ApiField>
  <ApiField name="model_id" type="string | null">
    A generation model your plan includes.
  </ApiField>
</ApiFields>

A stream always uses the engine's **Answer Mode** from its settings. Any `query_mode` you send is ignored here.

## Events [#events]

<ApiFields>
  <ApiField name="token" type="event" required>
    `delta`: the next piece of the answer.
  </ApiField>
  <ApiField name="final" type="event" required>
    `answer`, `citations`, and `confidence`: the complete, checked answer. Sent once.
  </ApiField>
  <ApiField name="done" type="event" required>
    `ok`: the stream is finished.
  </ApiField>
  <ApiField name="error" type="event">
    `detail`: the answer couldn't be finished. No `final` follows.
  </ApiField>
</ApiFields>

Some answers arrive whole in `final` with no `token` events, such as a [Precision Mode](/docs/knowledge/retrieval/precision-mode) answer. Always read `final` for the complete answer.

## Status codes [#status-codes]

| Status | Meaning |
| --- | --- |
| `200` | The stream started. Errors after this point arrive as an `error` event. |
| `400` | The query is empty. |
| `401` | The key is missing, malformed, or revoked. |
| `402` | Your plan's monthly allowance is used up. |
| `403` | The key's capability or scope doesn't allow reading this engine. |
| `404` | The engine doesn't exist in the key's workspace. |
| `429` | A rate limit was hit. |
</EndpointLayout>
