# AST-aware code chunking with tree-sitter

> Atlas indexes code by AST declarations using tree-sitter, emitting one chunk per function or class rather than fixed line windows.

Atlas chunks code by parsing it. Using tree-sitter, it emits 1 chunk per right-sized declaration, a function, class, or method, and falls back to line windows only for the gaps between them. The result is that a search hit is a complete unit of code with a name, instead of 40 lines that happen to start and end in the middle of two different functions.

## Key takeaways

- One chunk per declaration, produced by a real parser rather than a line count.
- Chunks carry the symbol name, so results are identifiable.
- Line windows cover the gaps, so nothing in a file goes unindexed.
- Better chunk boundaries mean fewer, more relevant results per query.

## What fixed line windows get wrong

Splitting a file every 40 lines is 1 rule applied to code that does not follow it. Boundaries land mid-function, so a single function becomes 2 partial chunks and neither one contains the whole behavior a query is looking for.

The damage shows up as retrieval that is technically correct and practically useless. A search matches the half of a function containing the keyword, and the half containing the logic that answers the question sits in an adjacent chunk that did not score high enough to be returned. You get a citation to a fragment and have to go read the file anyway.

## Parsing instead of counting

Atlas runs tree-sitter over a file and emits 1 chunk per declaration that is a sensible size, naming each chunk after the symbol it contains. A 200-line class and a 6-line helper each become their own unit rather than being cut to a common length.

Because the boundaries follow the language's own structure, a retrieved chunk is something a developer would recognize as a thing: this function, that class. That makes results readable on their own, and it makes the symbol name available as an additional signal for both ranking and display.

## The fallback that keeps coverage complete

Not everything in a file sits inside a declaration. Imports, top-level constants, and configuration blocks are line-windowed instead, so AST chunking adds structure where structure exists without leaving the other 100 percent of the file unindexed.

This is why the fallback matters as much as the parser. A purely AST-based index quietly drops whatever the grammar does not model, and the omissions are invisible until a search for a constant returns nothing. Covering the gaps means the index never regresses against a simpler approach.

## FAQ

### How does Atlas split code for indexing?

Atlas uses tree-sitter to emit one chunk per declaration, such as a function, class, or method, and line-windows the gaps between declarations.

### Why is AST chunking better than fixed line windows?

Line windows cut through the middle of functions, so a match often lands in a fragment. AST chunks are complete, named units of code.

### Does anything in a file go unindexed?

No. Code outside declarations, such as imports and top-level constants, is covered by a line-window fallback.

### Do chunks keep the symbol name?

Yes. Each declaration chunk is named after the symbol it contains, which makes results identifiable and gives ranking an extra signal.

---

Canonical HTML: https://runatlas.sh/resources/features/ast-code-chunking
Source of truth: aeo_pages row `/resources/features/ast-code-chunking` (segment: Platform) (this file is generated from it, never hand-edited).
Licence: Atlas is proprietary with a free core. It is not open source and there is no public source repository.
