Typorithm · medium

Chunking & Vector Store

chunking-indexingllmchunkingvector-storeoverlap
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Chunking & Vector Store

0%

RAG & Embeddings

Split documents into overlapping ~512-token chunks so retrieval is granular and context isn't cut at boundaries. Embed each chunk and upsert it into a vector database like pgvector or Pinecone for similarity search.

A whole document is too coarse to retrieve: you want the specific passage that answers a question, not a 50-page PDF. So you split each document into chunks of roughly 512 tokens — small enough to be precise, large enough to carry context.

Chunks are made to overlap (here 64 tokens) so a sentence that straddles a boundary still appears intact in at least one chunk. Without overlap, a key idea split across the cut could be lost from both chunks.

Each chunk is embedded into a vector and upserted into a vector store such as pgvector or Pinecone, along with its source text. The store indexes the vectors so you can later find the chunks most similar to a query.

01

Split documents into ~512-token chunks.

02

Overlap keeps context from being cut at boundaries.

03

Embed each chunk and upsert it to a vector store.

const chunks = splitIntoChunks(doc, { size: 512, overlap: 64 });
for (const chunk of chunks) {
  const vec = await embedder.embed(chunk);
  await vectorStore.upsert({ id, vec, text: chunk });
}

Read the implementation

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

1. Split into chunks

Documents are split into ~512-token chunks so retrieval is granular.

const chunks = splitIntoChunks(doc, { size: 512, overlap: 64 });

2. Overlap

Overlapping chunks share a bit of text so context isn't cut at a boundary.

overlap: 64

3. Embed each chunk

Each chunk gets its own embedding vector to index.

const vec = await embedder.embed(chunk);

4. Upsert to store

The vector plus its source text is written into the vector database for later search.

await vectorStore.upsert({ id, vec, text: chunk });

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.

Chunking & Vector Store: Lesson & Practice · Typorithm | ApplyDjinn