# Indexing (run once)

Split your documents into chunks, embed each chunk, and store the chunk text with its vector.

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

## Analogy

A build step. You do the heavy work ahead of time so every request is fast.

## Code example

`index-docs.ts`

```typescript
const chunks = splitIntoChunks(docText, 500);

for (const chunk of chunks) {
  const res = await client.embeddings.create({
    model: "text-embedding-3-small",
    input: chunk,
  });
  await db.query(
    "INSERT INTO docs (content, embedding) VALUES ($1, $2)",
    [chunk, JSON.stringify(res.data[0].embedding)],
  );
}
```

## In an agent

Re-index when a doc changes. That's the whole update process, no retraining.

## Practice

### Flashcards

- **What is a chunk?** A small piece of a document, stored for retrieval.

### 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.

## Questions people ask

### How do I index documents for RAG?

Split each document into chunks, create an embedding for each chunk, and store the chunk text with its vector and some metadata in a vector database. This runs ahead of time, like a build step, so answering questions later is fast. Run it again for any document that changes.

### How do I update a RAG system when my documents change?

Re-index the changed documents: delete their old chunks, split the new version, embed the chunks and store them. That is the whole update process. There is no model retraining, which is the big practical advantage of RAG over fine-tuning for company knowledge.

## Related concepts

- [RAG](https://studyvisually.com/learn/tech/ai-developers/embeddings-rag/rag)
- [Querying (every question)](https://studyvisually.com/learn/tech/ai-developers/embeddings-rag/rag-query)
- [Answer only from context](https://studyvisually.com/learn/tech/ai-developers/embeddings-rag/grounding-prompt)
- [Chunk size](https://studyvisually.com/learn/tech/ai-developers/embeddings-rag/chunking)

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