BUILDMARKETING.AI

Add streaming to your AI feature

Give this to your AI coding tool to make your website's AI answers appear word by word, and to find other causes of slow answers.

Prompt · for your AI coding tool (Claude Code, Cursor, Copilot)
You are working in my website's codebase. Our AI feature feels slow: visitors type a question, see a spinner, and wait for the whole answer before anything appears. Make the answer stream onto the page word by word as the AI writes it.

Before you change anything:
1. Find the full path of one question: the page or component the visitor types into, the server endpoint or function it calls, and the AI API call (OpenAI, Anthropic, Gemini or another provider).
2. Tell me which step waits for the complete answer today. Check the AI call, the server response, the hosting platform and the browser code.

Then make these changes:
3. Call the AI provider's streaming API (for example stream: true, or the SDK's stream helper) and forward each piece of text as it arrives.
4. Return a streamed response from the server: a ReadableStream with plain text, or Server-Sent Events (text/event-stream). Do not collect the whole answer before sending it.
5. Make sure nothing between the server and the browser buffers the response: check the hosting platform's function settings, any proxy or CDN, compression and middleware. Use a function type that supports streaming responses.
6. In the browser, read the response with fetch() and response.body.getReader(), decode each chunk with TextDecoder, and append it to the answer on screen as it arrives. Keep the page scrolled to the newest text.
7. Show a small "thinking" indicator only until the first words arrive, then remove it.
8. Handle errors part-way through: keep the text already shown and add a short message with a way to try again. Let the visitor stop or cancel an answer.
9. Keep anything that needs the full answer (saving the chat, logging, lead alerts) running after the stream ends, without delaying the first words.

Also check for speed problems streaming will not fix:
10. List what is sent to the AI on every question (system prompt size, history, documents). Turn on prompt caching for the parts that repeat, if the provider supports it.
11. Tell me whether a smaller or faster model, or a lower reasoning or thinking setting, would give good enough answers for this feature.
12. Point out questions that do not need AI at all (fixed answers, lookups, forms) and could be answered instantly in normal code.

Finally, measure before and after: the time until the first words appear, and the time until the answer is complete. Report both numbers to me.
From the articleWhy AI Answers Need to Be StreamedRead the article →