Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:1ay1 Agentty Structure Aware Chunking

From Leeroopedia
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

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment