[Bug] Embedding input guard under-estimates tokens ~3x for URI/markdown-heavy text: directory overviews always hit 400 exceed_context_size_error
Environment
- OpenViking 0.4.23, Linux
embedding.dense: local llama-serverQwen3-Embedding-4B-Q6_K.gguf,n_ctx=2560, OpenAI-compatibleembedding.max_input_tokens = 2400
Symptom
Directory-level overview embeddings fail with HTTP 400 whenever a directory has many children:
CRITICAL - Failed to generate embedding: OpenAI API error: Error code: 400 -
{'error': {'code': 400, 'message': 'request (7208 tokens) exceeds the available
context size (2560 tokens), try increasing it', 'type': 'exceed_context_size_error',
'n_prompt_tokens': 7208, 'n_ctx': 2560}}
(uri=viking://user/default/memories/entities/kanban_task)
Item-level memory extraction succeeds; only the directory .overview.md embedding fails, so the affected write is reported as failed even though the memories landed.
Root cause — the input guard under-estimates tokens by ~3x for URI/markdown-heavy text
openviking/utils/embedding_input.py::estimate_embedding_input_tokens() counts:
cjk_chars + ceil(other_chars / 4)
For directory overviews (markdown bullet lists of URIs, snake_case identifiers, hyphens, mixed CJK titles) a BPE tokenizer produces far more tokens than chars/4. Measured on a real overview:
| text | OV estimate | real tokens (Qwen tokenizer) | ratio |
|---|---|---|---|
full .overview.md (28,152 chars) |
7,038 | 20,530 | 2.92x |
| truncated to "2,408 estimated" (9,629 chars) | 2,408 | 7,101 | 2.95x |
So a limit of 2400 "tokens" actually sends ~7,100 tokens into a 2,560-token context — the guard never protects the request.
Suggested fixes (any one):
- Use the real tokenizer for the guard when the provider exposes one (llama-server
/tokenize, tiktoken/HF tokenizers for hosted providers). - Make the heuristic conservative for ASCII-heavy structured text — e.g.
ceil(other_chars / 1.4)instead of/4, or count separators (/,_,-,:) as token boundaries. - Per-source limits: directory overviews / summaries need a much smaller cap than memory items; a single global
max_input_tokenscannot serve both. - Fail-safe: on
exceed_context_size_error, retry once with the input truncated to the provider's reportedn_ctx.
Workaround currently in use
Lowering embedding.max_input_tokens from 2400 to 700 (real ≈ 1,999 tokens for the worst observed sample) makes the requests fit. This is a blunt instrument: it also truncates long memory items for embedding (disk content unaffected).
Current-main check at 6227af5102b3874d162d65a0503bfeef849686c2: the production guard still calls truncate_embedding_input. With max_input_tokens=2400, it budgets 2,400 × 4 character-units (ordinary ASCII costs 1 unit; CJK costs 4), so the reported ~9.6k-character structured prefix is consistent with the current implementation. The issue's reported Qwen tokenizer result (7,101 tokens for 9,629 characters against a 2,560-token context) is strong evidence that this input can exceed the provider context. I could not independently replay the exact sample on current main because the source overview and tokenizer fixture are not included, so I am not claiming a fresh reproduction. The published config docs describe this as a maximum estimated token count; before a patch, could maintainers clarify whether provider-specific tokenization where available or a more conservative structured-text heuristic is preferred? A blanket tighter ratio would also truncate ordinary content more aggressively across providers.
We measured the same gap from the other side, and it reproduces outside your corpus.
Setup: llama.cpp server, embeddinggemma-2-BF16, one POST /tokenize per input, two strings of equal length:
- plain Russian prose: 204 chars -> 51 tokens -> 4.00 chars/token
- Russian + markdown +
viking://URIs: 209 chars -> 69 tokens -> 3.03 chars/token
So the constant is fine for prose and ~25% optimistic for the text memory cards are mostly made of (URIs, bullets, identifiers, file paths). Your ~3x on directory overviews looks like the same error compounded by aggregation: an overview concatenates N children, so a 25% per-item underestimate becomes an unbounded overrun, not a fixed one.
What we changed: treat max_input_tokens as a budget for the whole request rather than a property of the text, and measure the ratio on the real tokenizer instead of trusting the constant (one /tokenize call per corpus class is enough). On a 2560-token model, a guard at 2400 leaves 160 tokens of headroom, which is less than a single URI header line plus the truncation suffix.
Same family as #6129, where the estimate ignored Cyrillic altogether.