{"slug": "rag-for-code-why-chunking-by-function-beats-chunking-by-lines", "title": "RAG for Code: Why Chunking by Function Beats Chunking by Lines", "summary": "A developer built a retrieval-augmented generation (RAG) system for code and found that chunking by function boundaries dramatically outperformed line-based chunking. By using a parser to extract complete functions, methods, and classes, the system retrieved meaningful code units that allowed an LLM to answer questions accurately. The approach improved retrieval quality without changing the underlying model.", "body_md": "I built a retrieval system over a codebase so an LLM could answer questions about it, and my first version was nearly useless. The problem was not the model or the embeddings. It was how I cut the code into chunks. Splitting source by line count shreds the very structure that makes code meaningful. Here is why function-aware chunking works so much better, and how to do it.\n\nThe standard RAG tutorial says: split your documents into fixed-size chunks (say 500 tokens), embed each chunk, retrieve the closest ones to the query. For prose, fine. For code, this is destructive.\n\nA 500-token window does not respect function boundaries. You end up with chunks like \"the last third of `transfer()`\n\nand the first half of `approve()`\n\n.\" Neither function is complete. The embedding represents a fragment that means nothing on its own, and when you retrieve it, you hand the model half a function with no signature and no context.\n\nMy early system would confidently answer questions about functions it had only seen the middle of. The retrieval was the bottleneck, and the chunking was the cause.\n\nCode has natural units: functions, methods, classes, contracts. Those are the units a developer reasons about, so those are the units to chunk by. One function, one chunk. The chunk includes the full signature, the body, and ideally the doc comment above it.\n\n```\ninterface CodeChunk {\n  name: string;        // function or method name\n  signature: string;   // full signature for context\n  body: string;        // the complete function body\n  filePath: string;    // where it lives\n  startLine: number;\n}\n```\n\nNow each chunk is a complete, meaningful thing. Retrieve it and the model gets a whole function it can reason about, with its name and signature intact.\n\nFor Solidity or TypeScript, you can get a long way with a parser rather than regex. For TypeScript I use the compiler API or a tool like `ts-morph`\n\n; for Solidity, a proper parser that gives you the AST. The point is to walk the syntax tree and emit one chunk per function-level node, rather than slicing the raw text.\n\nA simplified shape of the extractor:\n\n``` js\nimport { Project } from \"ts-morph\";\n\nfunction chunkByFunction(filePath: string): CodeChunk[] {\n  const project = new Project();\n  const source = project.addSourceFileAtPath(filePath);\n  const chunks: CodeChunk[] = [];\n\n  for (const fn of source.getFunctions()) {\n    chunks.push({\n      name: fn.getName() ?? \"anonymous\",\n      signature: fn.getSignature().getDeclaration()?.getText() ?? \"\",\n      body: fn.getText(),          // the whole function, intact\n      filePath,\n      startLine: fn.getStartLineNumber(),\n    });\n  }\n  // also walk classes/methods the same way\n  return chunks;\n}\n```\n\nEach function comes out whole. No more half-functions.\n\nI run this entirely on a local model so a private codebase never leaves my machine. Ollama serves an embedding model; I embed each function chunk and store the vectors:\n\n``` js\nimport { Ollama } from \"ollama\";\nconst ollama = new Ollama();\n\nasync function embed(text: string): Promise<number[]> {\n  const r = await ollama.embeddings({ model: \"nomic-embed-text\", prompt: text });\n  return r.embedding;\n}\n```\n\nI embed `${chunk.name}\\n${chunk.signature}\\n${chunk.body}`\n\nso the function name and signature are part of the vector, not just the body. That makes name-based queries (\"what does `withdraw`\n\ndo\") retrieve well, because the name is in the embedded text.\n\nAfter switching to function chunks, the same questions that used to get fragmented, half-wrong answers got crisp ones. \"How does this contract handle reentrancy in withdrawals?\" now retrieves the *complete* `withdraw`\n\nfunction plus the modifier it uses, and the model can actually reason about the checks-effects-interactions order because it can see the whole thing.\n\nThe model did not get smarter. The retrieval got honest. I was handing it complete units of meaning instead of arbitrary text windows.\n\nOne thing I added later: for a retrieved function, I also pull in the one-line signatures of functions that call it. That gives the model a sense of how the function is used without bloating the chunk. It is cheap context that often answers the follow-up question before it is asked.\n\nRAG quality is mostly retrieval quality, and retrieval quality is mostly chunking quality. The instinct to chunk by size comes from text-document tutorials, but code is not prose. It has structure, and that structure is exactly what carries the meaning. Chunk along the structure, embed the name and signature with the body, and run it locally if the code is private. The embeddings and the model were never the problem. The scissors were.", "url": "https://wpnews.pro/news/rag-for-code-why-chunking-by-function-beats-chunking-by-lines", "canonical_source": "https://dev.to/pavelespitia/rag-for-code-why-chunking-by-function-beats-chunking-by-lines-njc", "published_at": "2026-06-30 15:03:05+00:00", "updated_at": "2026-06-30 15:19:20.288775+00:00", "lang": "en", "topics": ["large-language-models", "developer-tools", "natural-language-processing"], "entities": ["Ollama", "ts-morph", "TypeScript", "Solidity"], "alternates": {"html": "https://wpnews.pro/news/rag-for-code-why-chunking-by-function-beats-chunking-by-lines", "markdown": "https://wpnews.pro/news/rag-for-code-why-chunking-by-function-beats-chunking-by-lines.md", "text": "https://wpnews.pro/news/rag-for-code-why-chunking-by-function-beats-chunking-by-lines.txt", "jsonld": "https://wpnews.pro/news/rag-for-code-why-chunking-by-function-beats-chunking-by-lines.jsonld"}}