Incremental engine for long horizon agents π Star if you like it!
Index your codebase into LanceDB.
A live, syntax-aware vector index over your repo β into embedded LanceDB, with no database to run β in ~100 lines of plain async Python.
Point it at a directory, search it in natural language, and it re-embeds only what changes as you edit.
This is the Tree-sitter code-embedding pipeline, targeting LanceDB instead of Postgres. LanceDB is an embedded vector store β no server to start, no connection string; the index is just a ./lancedb_data/ directory you can copy or delete. You declare the transformation in native Python and your own types β target_state = transformation(source_state). The heavy lifting (incremental processing, change tracking, managed targets) runs in a Rust engine underneath, so a one-line edit re-embeds one chunk, not the repo.
query: "where do we embed chunks?"
[0.582] examples/code_embedding_lancedb/main.py (L66-L82)
@coco.fn
async def process_chunk(chunk, filename, id_gen, table):
... embedding=await coco.use_context(EMBEDDER).embed(chunk.text) ...
How it works
Walk a repo β detect language β split along the syntax tree with Tree-sitter β embed each chunk β upsert into LanceDB. With live=True, the source keeps watching and the index stays fresh as you code.
The whole indexing path is the snippet below β read it top-to-bottom in main.py:
@coco.fn(memo=True)
async def process_file(file: FileLike, table: lancedb.TableTarget[CodeEmbedding]) -> None:
text = await file.read_text()
language = detect_code_language(filename=str(file.file_path.path.name))
chunks = _splitter.split(text, chunk_size=1000, min_chunk_size=300,
chunk_overlap=300, language=language) # Tree-sitter, syntax-aware
id_gen = IdGenerator()
await coco.map(process_chunk, chunks, file.file_path.path, id_gen, table)
@coco.fn
async def process_chunk(chunk, filename, id_gen, table) -> None:
table.declare_row(row=CodeEmbedding(
id=await id_gen.next_id(chunk.text), filename=str(filename), code=chunk.text,
embedding=await coco.use_context(EMBEDDER).embed(chunk.text),
start_line=chunk.start.line, end_line=chunk.end.line,
))
@coco.fn
async def app_main(sourcedir: pathlib.Path) -> None:
table = await lancedb.mount_table_target(
LANCE_DB, table_name=TABLE_NAME,
table_schema=await lancedb.TableSchema.from_class(CodeEmbedding, primary_key=["id"]),
)
files = localfs.walk_dir(sourcedir, recursive=True,
path_matcher=PatternFilePathMatcher(included_patterns=["**/*.py", ...]),
live=True)
await coco.mount_each(process_file, files.items(), table)
π Full Tutorial β
The closest walkthrough is the Postgres version β same chunking, embedding, and incremental flow; only the target store differs. Step-by-step coverage of the data model, the lifespan, chunking, embedding, the App, and exactly what happens on each kind of change.
Why it's worth a star β
- No database to run. LanceDB is embedded β the index lives in
./lancedb_data/. Nothing to start, nothing to connect to; copy the directory to move it, delete it to start fresh. - Syntax-aware chunking, built in. Tree-sitter splits along real code structure β functions, classes, blocks β so retrieval returns whole units, not fragments cut mid-statement. Every major language; unknown types fall back to plain text.
- Incremental by default.
@coco.fn(memo=True)skips unchanged files and reuses embeddings for unchanged chunks;mount_table_targetupserts only the rows that moved and deletes orphans. Edit one function β one chunk is re-embedded. - Live updates.
live=True+cocoindex update -Lkeeps watching the filesystem and applies changes with low latency β always-fresh context for an agent. - Plain Python, your stack. Swap the embedding model (12k+ on Hugging Face), the chunking, or the vector store. No DSL.
Run it
LanceDB is embedded, so there's no server to start β the index is created on first run.
1. Install deps:
pip install -e .
2. Build / update the index (writes rows into LanceDB at ./lancedb_data/) β pick one:
cocoindex update main # catch-up: scan, sync changes, exit
cocoindex update -L main # live: catch up, then keep watching for edits
3. Query it β semantic search from the terminal:
python main.py "embedding"
Each result carries start_line/end_line, so hits point straight at the lines that matched. Query uses LanceDB's vector search, with the returned distance turned into a similarity score.
Want it production-ready, not DIY?
CocoIndex Code is this exact pipeline β AST-aware chunking, incremental re-index, local embeddings β hardened and packaged as a CLI and an MCP server you can plug straight into a coding or code-review agent.
npx skills add cocoindex-io/cocoindex-code # Claude Code skill, then /ccc
claude mcp add cocoindex-code -- ccc mcp # MCP: Codex, OpenCode, Cursor, any client
ccc index && ccc search "where we embed chunks" # CLI
If this made your agents smarter, give CocoIndex a star β β it helps a lot.
Docs Β· Walkthrough Β· Discord Β· See all examples β