Typorithm · medium

Retrieval & Augmentation

retrieval-augmentllmragretrievalgrounding
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Retrieval & Augmentation

0%

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.

01

Embed the question and retrieve the top-K chunks.

02

Inject the retrieved context into the prompt.

03

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 }],
});

Read the implementation

Connect each part of the code to the concept before you type.

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 });

2. topK

topK caps how many nearest chunks come back — here the 5 closest.

{ topK: 5 }

3. Build context

Join the retrieved chunk texts into one context block to inject into the prompt.

const context = hits.map((h) => h.text).join("\n");

4. Model id

The current Claude model id used to generate the grounded answer.

model: "claude-opus-4-8",

5. Augmented prompt

The retrieved context plus the question is the augmented prompt the model answers from.

content: context + "\n\nQ: " + question

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Retrieval & Augmentation: Lesson & Practice · Typorithm | ApplyDjinn