Principle:1ay1 Agentty Structure Aware Chunking
| Knowledge Sources | |
|---|---|
| Domains | Information_Retrieval, Parsing |
| Last Updated | 2026-10-28 12:00 GMT |
Overview
Document chunking strategy that splits source files at semantic boundaries (functions, classes, headings) using syntax-tree parsing rather than fixed-size windows, so each chunk is a complete, self-contained unit that retrieves and reads meaningfully.
Description
Fixed-size chunking (e.g., 512-token windows) splits files mid-function, producing chunks that break the structure they're trying to represent. A chunk that starts in the middle of a function and ends mid-expression retrieves poorly — it lacks the function signature, the class context, or the heading that gives it meaning.
Structure-aware chunking uses tree-sitter to parse the syntax tree and split at function/class/heading boundaries. Each chunk is a complete unit: a whole function, a class definition, a markdown section. This improves both retrieval (the chunk's content is a coherent semantic unit) and model consumption (the model sees complete code, not fragments).
A skip-list excludes directories that are always huge and never hold signal: node_modules, .git, target, build.
Usage
Use this principle when:
- Indexing a codebase or documentation for RAG
- Fixed-size chunks break semantic units and retrieve poorly
- You want each retrieved chunk to be a complete, readable unit
Theoretical Basis
# Abstract algorithm (NOT real implementation)
def should_skip_dir(path):
return path in {"node_modules", ".git", "target", "build", "dist"}
def chunk_file(source, language):
tree = tree_sitter_parse(source, language)
chunks = []
for node in walk(tree):
if node.type in {function_def, class_def, heading}:
chunks.append(Chunk(
text=node.text,
file=path,
line_start=node.start_row,
line_end=node.end_row,
kind=node.type,
))
return chunks