Chunking a codebase by a fixed token count, the same way you'd chunk an article, routinely splits a function in half — and a half-function chunk is nearly useless for a 'how does X work' question.

Chunk by structure, not by token count

  • Parse the code (using the language's AST or a simple structural parser) and chunk by function or class boundary, not by arbitrary token windows.
  • Include a function's signature and docstring even when the body is chunked separately, so retrieval has something to match against beyond raw logic.
  • Index file paths and import relationships as metadata — 'where is this function called from' is a common real question retrieval alone can't answer without that structure.

What this enables

A code-aware RAG assistant that retrieves whole, coherent functions answers 'how does X work' meaningfully better than one retrieving arbitrary fifty-line windows that cut across logic.

See RAG chunking strategies, a guide.

Pranjul Rathour, GenAI Engineer from Kanpur, India. Open to GenAI roles, hackathon judging, mentorship sessions and guest talks at any campus: pranjulrathour41@gmail.com.