Build your knowledge base

Embeddings and models

What gets embedded, how a chunk is built, and which embedding model to pick for your hardware.

On this page

Which nodes get a vector, how a long document is split into several, and which embedding model to run. Read this when a natural-language query misses something you expected it to find, or before you change the model on a store that already has vectors. This page answers "which model makes the vectors, and what gets one?". To choose where the model runs (built-in, Ollama or a hosted API), see Model providers.

What gets embedded#

19 embeddable kinds, drawn from five different parts of the graph. Everything else in the graph is indexed and queryable by name, but has no vector.

Where it comes from Kinds
Code symbols class, function, method, interface, struct, enum, field, macro, typedef, enum_constant, global_variable
Web and API surface endpoint, route
Data contracts table, view, schema_element, schema_type
Infrastructure resource
Decisions adr

Each one is embedded with its name, qualified name, file path, and captured signature and docstring. The extra text is the point: a name alone is thin signal, and a query like "resend a reading that failed to upload" tends to share its words with the signature and the docstring rather than with the identifier.

The set is wider than code on purpose. A route, a table and an architecture decision record are all semantically searchable, which is worth knowing before you write off a natural-language question about your schema or your infrastructure as out of scope.

The C and C++ trade-off, measured#

Data members, macros, typedefs, enum constants and file-scope variables are the majority of the symbols in a C or C++ tree. Before they had vectors, no semantic or hybrid query could return one of them at all.

They are also thinner signal than a function: a data member usually carries a name and a type and nothing else. Measured on a large legacy tree, adding them costs the kinds that were already embedded about five percentage points of recall@10, in exchange for tens of thousands of symbols going from unreachable to findable.

If your tree is C++-heavy and you would rather have the sharper ranking than the reach, kb query --kind narrows any single query. The trade-off is recorded per kind in src/contextlake/kb/kinds.py, so it can be revisited with the numbers in view.

What is deliberately not embedded#

File, module and package nodes. A path or a shared package name is low semantic signal, and skipping them keeps results clean. It also avoids re-embedding a cross-repo shared node once per referencing repository.

Sampling a large fleet#

kb embed --limit N caps how many nodes are embedded per repository in one pass: the first N embeddable nodes of each repo's shard, in shard order.

Read it as a sampling knob, not a resumable one.

Behaviour What it means for you
Takes the same first N every time A second --limit pass adds nothing new.
Replaces that repo's vectors rather than adding to them The store never accumulates across passes.
Never stamps the repo as fully embedded A later plain kb embed still does the whole repository.

Use it to try the pipeline on a large fleet before committing to a full run, not to embed a repository in instalments.

How ingested documents are chunked#

An ingested document (kb ingest) is embedded as several overlapping vectors rather than one vector over the whole page: 1200 characters each with 200 characters of overlap, split on paragraph boundaries so a sentence is never cut in half.

A document is still one node and kb query still returns nodes, so chunking is invisible from the outside. The vector store keys each chunk separately and keeps a document's best-scoring chunk when it ranks results.

The reason is dilution. A 14 KB page embedded as one vector is an average of everything the page discusses, and a question about one paragraph of it matches that average poorly.

What the measurement shows#

Measured on contextlake's own 29 documents (424 KB, mean 14.6 KB) with 53 queries selected by their position in the document.

One vector per document Chunked Delta
Hit rate 71.7% 94.3% +22.6pp
MRR 45.2% 80.4% +35.2pp
Tokens per query 276 291 +15

13 queries were fixed and 1 regressed. The queries were stratified by depth before indexing, and the unchunked arm is already worse at depth (62.5% against 79.3% at the surface). That is the dilution the change is aimed at, visible before any chunking was applied. Chunking also helps depth more (+29.2pp) than the surface (+17.2pp).

What the measurement does not establish#

One embedder (model2vec, which averages; a transformer would truncate instead, plausibly worse for a long document, and that was not measured), one single-topic corpus, and verbatim-sentence queries. The absolute numbers are optimistic and only the delta is sound.

The chunk size itself was never tuned. split_document in src/contextlake/kb/embeddings/chunk.py takes max_chars and overlap, so changing them is one argument. Changing them also makes the numbers above describe a configuration that is no longer the one running.

Upgrading an existing store#

Document vectors are only rewritten by kb ingest, so an existing store keeps its old whole-document vectors until you re-run it. kb ingest clears the ingest partition's vectors and then stores the new ones, so a re-run replaces them cleanly and no stale chunk survives a document that shrank. On a run whose probe got no answer, the first source clears after its first batch passes the check, or at the end of the source when every body is empty (see Choosing a model).

Choosing a model#

Model How to select it What it is
minishlab/potion-base-8M the default A static model: it looks each token up in a table instead of running a transformer forward pass. Small enough to ship as a CPU default, fast enough that query latency is not the thing you notice.
minishlab/potion-base-32M model = "minishlab/potion-base-32M" under [embeddings] The larger sibling of the same family.
bge-small (ONNX) the kb-fastembed extra plus engine = "fastembed" A real transformer instead of a static lookup. The extra installs onnxruntime, tokenizers, huggingface_hub and numpy, not the fastembed library. The vectors match fastembed 0.9.0, so a store built with it needs no re-embed. Another fastembed model needs pip install fastembed.

On a store that already holds vectors the identity guard refuses to mix models, so switch on a fresh vector store. kb ingest takes one probe vector. When the embedder answers, the check runs before any vector is cleared or any document is rewritten. A run refused at that check prints the error and This run changed nothing.

If the embedder gives no answer to the probe, nothing was proved. kb ingest still rewrites the documents. Until one source has passed the check, it clears a source's old vectors only after a batch of that source holds a vector that passes. A reply whose vectors are missing, empty or of different lengths does not pass. An embedder that never answers clears nothing. If every document body is empty, no batch runs and kb ingest clears the vectors at the end of the source.

A refusal at that check keeps the vectors and ends the run after that source. The source keeps its new documents, the sources after it do not run, and the exit code is 1.

Once one source has passed the check, each later source clears its vectors before it writes, as on a run whose probe answered. If the embedder stops after that, the next source has no vectors until you run kb ingest again.

A source that clears before it writes can still be refused: the first batch that comes back with another vector length fails the check after the clear. The source then keeps its new documents and has no vectors, the sources after it do not run, and the exit code is 1. Run kb ingest again to rebuild the vectors.

A later batch of another vector length stops the embed phase for that source. The vectors of the earlier batches stay, kb ingest prints EMBED INCOMPLETE with the lengths, and the exit code is 1.

When the embed phase of a source stops early, because the embedder stopped answering or a batch came back unusable, the summary counts the documents left without vectors as document(s) not embedded. target(s) unreadable counts the targets a source could not read, such as pages, URLs and files. Both make the run exit 1. To exit 0, put the flag before the command: contextlake --exit-zero-on-partial kb ingest.

kb enrich and kb wiki take one probe vector per run, before their first write. A refused run prints the error and This run changed nothing, exits 1 and keeps the stored vectors. kb wiki takes the probe after it has checked the repo ids you named.

When the probe gets no answer they go on. They write the new rows first and clear a partition's old vectors only after the first batch holds a vector that passes the check. An embedder that never answers clears nothing, so the partition keeps its old vectors, unless every document body is empty: then no batch runs and the old vectors are cleared at the end.

After a probe that answered, they clear before the first batch, so a batch that then fails leaves the partition with no vectors. A partition with no documents is cleared at once.

kb embed runs the check on the probe vector it takes at the start of each pass, --watch passes included. A refused run prints the error and exits 1 before it embeds any repo. In a one-shot run, a probe that fails or returns no vector also ends the run with exit 1 and writes no vectors. With --watch, that pass logs the same line, writes nothing, and the loop runs the next pass.

Which one is better on your code has a local answer, and kb eval is how to get it: build a golden set from queries your team types, embed with one model, score, re-embed with the other, score again. No published ranking against somebody else's corpus is worth as much as that run.

Re-embedding#

embed is incremental, like index. It re-embeds only repositories whose indexed HEAD moved since they were last embedded, so a scheduled refresh over a large fleet stays cheap.

See also#

Next steps