The retriever cannot find a sentence it never embedded
A retrieval index for the Belgavi's Book Store help desk is built from long policy pages and resolved CASE notes. Chunks are cut at 400 tokens because that size kept answers readable in an earlier prototype. Months later, Tanishka notices that questions about the last paragraph of several policies never retrieve the right page. The text is in the document store and in the chunk. It was never in the vector. The embedding model accepted the chunk, quietly cut it at its own input limit and returned a vector for the first part only.
This is not a model quality issue. It is a contract between the chunker and the embedding runtime that nobody wrote down. Every embedding model has a maximum input length, and runtimes differ in what they do when an input is longer: some cut the end silently, some raise an error, and some let you choose. A chunker that measures size in characters or words, or with a different tokenizer, cannot know where that cut falls.
Measured on a local runtime
The behaviour is easy to measure without any external service. Ollama documents a truncate option on its /api/embed endpoint: it truncates the end of each input to fit the context length, returns an error when it is false and the input is too long, and defaults to true. I ran Ollama 0.40.18 locally with two embedding models, all-minilm and nomic-embed-text. For each length, two inputs shared the same synthetic filler text and differed only in a final sentence: one about store credit for Jenya, one about a torn hardcover reported by Geetha. If the final sentence is embedded, the two vectors differ; if it is cut off, they are identical.
| Model | Filler words | Tokens counted | Cosine of the two inputs | With truncate false |
|---|---|---|---|---|
| all-minilm | 136 | 169 | 0.929 | accepted |
| all-minilm | 204 | 245 | 0.950 | accepted |
| all-minilm | 272 | 256 | 1.000 | HTTP 400 |
| nomic-embed-text | 1,632 | 1,841 | 0.998 | accepted |
| nomic-embed-text | 2,176 | 2,048 | 1.000 | HTTP 400 |
The token counts are the prompt_eval_count values Ollama returned for a single input. They stop at 256 for all-minilm and at 2,048 for nomic-embed-text. At those points the two different documents produce the same vector, a cosine of exactly 1.000, and the strict request is rejected with HTTP 400. With the default setting the request succeeds, and nothing in the response says that the tail was dropped except the token count.
Two details from the measurement are worth keeping. First, the all-minilm model metadata reports a context length of 512, but the cut happened at 256, which is the num_ctx parameter shipped with that model and also the max_seq_length of 256 published for the original sentence-transformers checkpoint. The number in one metadata field is not necessarily the number that applies. Second, the cosine between two inputs that share most of their text rises towards 1 as the shared part grows: 0.998 is not proof of truncation. Only an exact 1.000 together with a capped token count is.
These numbers come from one machine and one runtime version. They demonstrate the mechanism; they are not limits to copy into another stack.
Where the tokens go
The usable length of a chunk is smaller than the model limit. Special tokens consume a few positions, and anything prepended to the text consumes more: a document title, a section path, or a task instruction such as the search_document: prefix that some models expect. If the limit is 256 tokens, two are reserved for special tokens and a prefix with metadata takes 8, then only 246 tokens of chunk text are embedded.
A worked example shows how much disappears. A 1,400-token policy is split into 400-token chunks with 50 tokens of overlap, so chunks start at tokens 0, 350, 700 and 1,050. Each embedded span covers only the first 246 tokens of its chunk:
| Chunk | Chunk tokens | Embedded tokens | Never embedded |
|---|---|---|---|
| 1 | 0 to 400 | 0 to 246 | 246 to 350 is in no other span |
| 2 | 350 to 750 | 350 to 596 | 596 to 700 |
| 3 | 700 to 1,100 | 700 to 946 | 946 to 1,050 |
| 4 | 1,050 to 1,400 | 1,050 to 1,296 | 1,296 to 1,400 |
Four gaps of 104 tokens each leave 416 of the 1,400 tokens, about 30 percent, outside every vector. Overlap did not help, because overlap copies the start of the next chunk into the end of the previous one, and the end is exactly what gets cut. If facts sit at tokens 40, 300, 520, 760, 1,010 and 1,290, the facts at 300 and 1,010 can never be retrieved by vector search. Shrinking chunks to 240 tokens with 40 tokens of overlap produces seven chunks that all fit, and every token is covered.
Measure with the model tokenizer, then fail closed
The fix belongs in the ingestion pipeline, not in the retriever:
def check_chunk(text_tokens, prefix_tokens, special_tokens, model_limit):
usable = model_limit - special_tokens - prefix_tokens
if usable <= 0:
raise ValueError("prefix and special tokens exceed the model limit")
if text_tokens > usable:
return {"status": "too_long", "dropped": text_tokens - usable}
return {"status": "ok", "dropped": 0}Count tokens with the tokenizer of the embedding model, not of the chat model and not with a characters-per-token ratio. Include the prefix and any metadata header in the count. Then choose one of three behaviours deliberately: split the chunk further, reject it and report it, or accept truncation and record which span was embedded. Ask the runtime to fail rather than truncate where it supports that, as Ollama does with truncate: false, so that a tokenizer mismatch shows up as an error at ingestion instead of a silent recall loss months later.
Store the embedded span with each vector: start and end offsets in the source, the token count the runtime reported and the model identifier. A citation shown to a user should point at text that was actually embedded, and an audit can then find vectors whose span is shorter than their chunk.
Failure modes
Different tokenizers in the chunker and the model. A chunker sized with one tokenizer and a model using another disagree by tens of percent on some text, so chunks near the limit are cut unpredictably.
Prefixes and headers added after chunking. Prepending a title or task instruction at embedding time pushes chunk text past the limit even when the chunker sized it correctly.
A model swap with a shorter limit. Re-embedding an index with a new model can silently truncate chunks that fit the old one. Re-run the length check as part of every migration.
Overlap used as a safety margin. Overlap protects facts that straddle a boundary. It does not protect text past the model limit, because truncation removes the end of every chunk.
Batch requests hiding counts. Some runtimes report one token total for a batch. Measure per input, or the cap is invisible; in the measurement above, a two-input request reported 512 tokens for two inputs capped at 256 each.
Verification
- Ingestion test: build a chunk one token longer than the usable limit and assert the pipeline rejects or splits it.
- Canary pair: embed two texts that differ only after the limit; an exact cosine of 1.000 means truncation is happening somewhere.
- Coverage report: for each document, the fraction of tokens inside at least one embedded span.
- Retrieval test: a question whose answer sits in the last sentence of a long section must retrieve that section.
- Migration test: rerun the coverage report after any change of model, prefix or chunker.
The interactive lab computes embedded spans, uncovered tokens and unreachable facts for adjustable chunk size, overlap, limit, prefix and failure mode.
Interactive experiment
Embedding chunks: token limits, overlap and dropped text. Adjustable synthetic fixtures, decision traces, reset and JSON export.