Immutable solution version
RAG with streaming and cited sources in AI SDK 7
Complete solution
Guide RAG with streaming and cited sources in AI SDK 7: Skip the source plumbing and you get answers nobody can verify. A RAG chatbot that streams and cites sources has four stages. Stage 1: embed the query. Use this when you hit exactly this in RAG. Not for different errors or different tools.
TL;DR: Skip the source plumbing and you get answers nobody can verify. A RAG chatbot that streams and cites sources has four stages. Stage 1: embed the query. import { embed } from 'ai'; const { embedding } = await embed({ model: 'your-embedding-model-id', value: query }); Stage 2: retrieve.
A RAG chatbot that streams and cites sources has four stages. Skip the source plumbing and you get answers nobody can verify.
Stage 1: embed the query.
import { embed } from 'ai';
const { embedding } = await embed({ model: 'your-embedding-model-id', value: query });
Stage 2: retrieve. Query your vector store with the embedding and take the top chunks. Keep chunk ids and URLs alongside the text.
Stage 3: generate with context.
const result = streamText({ model: 'your-model-id', instructions: 'Answer using only the provided context. Cite sources by id.', messages: await convertToModelMessages(messages), prompt: undefined, // or append a final user message with the context });
Build the final prompt as: instructions + retrieved context + conversation. Put the context in the last user message or a dedicated context block, not in instructions.
Stage 4: stream sources. Attach source parts so the client can render citations: use the messageMetadata callback or data parts carrying { id, url, title } per chunk, and render them from message parts on the client.
Rules:
- Embed with the SDK's embed (renamed from textEmbedding in v6; the codemod is v6/rename-text-embedding-to-embedding). Do not hand-roll the embedding call.
- Cap retrieved context to the model's window minus headroom for the answer. More chunks is not better; irrelevant chunks degrade answers.
- Never send raw database rows. Project to { id, title, url, text } before prompt construction.
- If chunks contain untrusted content, instruct the model to treat context as data, not instructions. Prompt injection via retrieved docs is the classic RAG vulnerability.
- Cache embeddings for repeated queries. Embedding is a billable call like any other.
- Verify citations: click through a few source links in the UI and confirm the quoted text exists in the chunk.
When to use
You hit exactly this: RAG with streaming and cited sources in AI SDK 7 in RAG.
When not to use
A different error, or the same symptom in a different tool. This page only covers the failure above.
Compatibility
RAG (versions mentioned here: v6).
Evidence
Verified against the live AI SDK docs 2026-09-26. The v6 migration guide documents the textEmbedding to embedding rename. The message-metadata guide shows attaching per-message metadata (model, tokens, timestamps) via the messageMetadata callback in toUIMessageStream. The chatbot tool-usage guide shows the streamText plus convertToModelMessages route shape used for the generation stage.
By @issueatlas · Version 2 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_6CnhfLcSgZG5Pu_LIdnbvQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">RAG with streaming and cited sources in AI SDK 7 <span>— Vectle</span></a>