# Querying (every question)

Embed the question, fetch the closest chunks, and send them to the model with an instruction to answer only from that context.

AI developers · Part 05: Embeddings + RAG · Card 7 of 14 · Last updated 2026-10-13

## Analogy

A request handler: look up the data, render it into the template, return the response.

## Code example

`ask.ts`

```typescript
const { rows } = await db.query(
  "SELECT content FROM docs ORDER BY embedding <=> $1 LIMIT 3",
  [JSON.stringify(questionVector)],
);
const context = rows.map((row) => row.content).join("\n---\n");

const answer = await client.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [
    { role: "system", content: "Answer ONLY from the context. If it's not there, say you don't know." },
    { role: "user", content: `Context:\n${context}\n\nQuestion: ${question}` },
  ],
});
```

## In an agent

The same query logic becomes the body of a searchDocs tool.

## Practice

### Flashcards

- **What is RAG?** Retrieve relevant chunks from your own data, add them to the prompt, and have the model answer from them.
- **Interview: first question when RAG answers wrong?** Were the right chunks retrieved? If not, fix retrieval. If yes, fix the prompt or model. Then add an eval case.

### Interview questions

**What is RAG and why use it?**

- Retrieve relevant chunks from your own data and add them to the prompt.
- The model answers from that context instead of from memory.
- It gives the model private, up-to-date knowledge and reduces hallucination, without fine-tuning.

**Your RAG gives wrong answers. How do you debug it?**

- Check the trace: were the right chunks retrieved?
- If not, fix retrieval: chunking, hybrid search, re-ranking, metadata filters.
- If they were, fix the prompt or the model.
- Then add the failing case to the eval set so it stays fixed.

## Questions people ask

### What happens when a user asks a RAG system a question?

The question is turned into an embedding, the vector database returns the closest chunks, and those chunks are added to the prompt with an instruction to answer only from them. The model then writes the answer. The same steps can become a searchDocs tool for an agent.

### How many steps does a RAG query take?

Three. Embed the question with the same model you used for indexing. Retrieve the top few chunks by similarity. Generate the answer by sending those chunks and the question to the model with grounding instructions. Using a different embedding model for questions and chunks breaks the similarity scores.

## Related concepts

- [Top K](https://studyvisually.com/learn/tech/ai-developers/embeddings-rag/top-k)
- [RAG](https://studyvisually.com/learn/tech/ai-developers/embeddings-rag/rag)
- [Re-ranking](https://studyvisually.com/learn/tech/ai-developers/embeddings-rag/re-ranking)
- [Evals](https://studyvisually.com/learn/tech/ai-developers/production/evals)

Web page: https://studyvisually.com/learn/tech/ai-developers/embeddings-rag/rag-query
Study this card in the app: https://studyvisually.com/app/learn?category=ai-developers&card=rag-query
