---
title: "Stream long answers · Agent Wranglers"
url: https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks/stream-answers
updated: 2026-10-20
licence: "All rights reserved"
---

# Stream long answers

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Recommended
- Chapter: [Latency, cost and fallbacks](https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks)
- Rule: Stream any answer a person waits for that can take more than 2 seconds, and measure the time to the first token.
- How: Use the provider's streaming mode and pass the text on to the browser as it arrives, for example with server-sent events. Show a working state at once. Stream plain text answers only. Structured output that code acts on is validated as a whole, so wait for it or stream only a progress note. Run [output checks](https://agentwranglers.se/playbook/ai-features/guardrails-and-human-review/output-checks) on the finished text, and replace it if they fail. Record time to first token and total time per feature.
- Check: The latency dashboard shows time to first token per feature. Its 95th percentile is under the first-token target in the feature's spec, such as 1.5 seconds.

Example:

```ts
app.get('/api/assistant', async (req, res) => {
  res.set({ 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-store' });
  const started = performance.now();
  let first = true;
  for await (const text of ai.stream({ promptId: 'help-chat', vars: { question: String(req.query.q) }, userId: req.user.id })) {
    if (first) { metrics.timing('ai.first_token_ms', performance.now() - started); first = false; }
    res.write(`data: ${JSON.stringify({ text })}\n\n`);
  }
  res.end();
});
```

```python
@app.get("/api/assistant")
async def assistant(q: str, user: User = Depends(current_user)):
    async def events():
        started, first = time.perf_counter(), True
        async for text in ai.stream(prompt_id="help-chat", variables={"question": q}, user_id=user.id):
            if first:
                metrics.timing("ai.first_token_ms", (time.perf_counter() - started) * 1000)
                first = False
            yield f"data: {json.dumps({'text': text})}\n\n"
    return StreamingResponse(events(), media_type="text/event-stream", headers={"Cache-Control": "no-store"})
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers ai features playbook: https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks/stream-answers`.

- Next: [Time out every model call](https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks/model-timeouts)
- [Latency, cost and fallbacks](https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks)
