1. Retrieve top-K
Embed the question and ask the store for the top-K most similar chunks.
const hits = await vectorStore.query(embed(question), { topK: 5 });Typorithm · medium
Step 1 of 8 · intro card
RAG & Embeddings
RAG embeds the question, retrieves the top-K most similar chunks, and injects them into the prompt so the model answers from your data. This grounds responses, reduces hallucination, and lets you cite the retrieved sources.
At query time you embed the user's question with the same model you used for indexing, then ask the vector store for the top-K nearest chunks. Using one embedding space for both sides is what makes the match meaningful.
The retrieved chunk texts are concatenated into a context block and placed before the question in the prompt. This is the 'augmentation' in retrieval-augmented generation: the model now answers from your data, not just its training.
Grounding the model in retrieved passages reduces hallucination and keeps answers current with your sources. Because you know exactly which chunks were retrieved, you can also show citations back to the original documents.
Embed the question and retrieve the top-K chunks.
Inject the retrieved context into the prompt.
Grounding cuts hallucination and enables citations.
const hits = await vectorStore.query(embed(question), { topK: 5 });
const context = hits.map((h) => h.text).join("\n");
const answer = await client.messages.create({
model: "claude-opus-4-8",
messages: [{ role: "user", content: context + "\n\nQ: " + question }],
});Connect each part of the code to the concept before you type.
Embed the question and ask the store for the top-K most similar chunks.
const hits = await vectorStore.query(embed(question), { topK: 5 });topK caps how many nearest chunks come back — here the 5 closest.
{ topK: 5 }Join the retrieved chunk texts into one context block to inject into the prompt.
const context = hits.map((h) => h.text).join("\n");The current Claude model id used to generate the grounded answer.
model: "claude-opus-4-8",The retrieved context plus the question is the augmented prompt the model answers from.
content: context + "\n\nQ: " + questionRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.