1. Split into chunks
Documents are split into ~512-token chunks so retrieval is granular.
const chunks = splitIntoChunks(doc, { size: 512, overlap: 64 });Typorithm · medium
Step 1 of 8 · intro card
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.
Split documents into ~512-token chunks.
Overlap keeps context from being cut at boundaries.
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 });
}Connect each part of the code to the concept before you type.
Documents are split into ~512-token chunks so retrieval is granular.
const chunks = splitIntoChunks(doc, { size: 512, overlap: 64 });Overlapping chunks share a bit of text so context isn't cut at a boundary.
overlap: 64Each chunk gets its own embedding vector to index.
const vec = await embedder.embed(chunk);The vector plus its source text is written into the vector database for later search.
await vectorStore.upsert({ id, vec, text: chunk });Read 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.