[Bug]: Compressor v3 extraction fails with exceed_context_size_error when session tokens exceed VLM ctx-size
Issue Origin
Observed or reproduced in a real environment
Bug Description
OpenViking compressor v3 extraction pipeline has no input truncation logic. When a session accumulates more tokens than the VLM model's --ctx-size, the extraction request is sent untruncated to the VLM, and llama.cpp rejects it with exceed_context_size_error. This is a reliability issue — any sufficiently long conversation will trigger it.
Steps to Reproduce
- Configure OV with llama.cpp VLM (e.g. Qwen3-VL-4B with
--ctx-size 12288) - Run a conversation session that accumulates >12288 tokens (system prompt + conversation messages)
- Session commits, triggering extraction/compression v3
- OV sends full session to VLM via
compressor_v3.py:_get_or_create_react()→config.vlm.get_vlm_instance() - No token counting or truncation occurs before sending
- llama.cpp rejects with
exceed_context_size_error
Expected Behavior
Extraction should either:
- Truncate session messages to fit within VLM context window, or
- Gracefully handle the overflow with a configurable
max_input_tokenslimit
Actual Behavior
openai.BadRequestError: Error code: 400 - request (26827 tokens) exceeds the available context size (12288 tokens)
The extraction pipeline silently sends the full request without any pre-flight token check. max_tokens in VLM config only controls output length, not input context.
Error Logs
08/10/2026, 8:53:43 PM
openai.BadRequestError: Error code: 400 - {\'error': {\'code': 400, 'message': 'request (26827 tokens) exceeds the available context size (12288 tokens), try increasing it', 'type': 'exceed_context_size_error', 'n_prompt_tokens': 26827, 'n_ctx': 12288}}
OpenViking Version
v0.4.13
Python Version
3.13
Operating System
Linux
Model Backend
Other (local llama.cpp)
Additional Context
Root cause analysis:
compressor_v3.py:_get_or_create_react()createsExtractLoopwith all messages (archive overview + system prompt + session messages) with no truncation- Unlike
embedding.max_input_tokens(default 4096) andrerank.max_input_tokens, the VLM/compression pipeline has no corresponding input limit config vlm.max_tokensonly affects output generation length- No workaround exists in
ov.conf— the only options are disabling extraction entirely or modifying source code (lost on upgrade)
I reproduced this gap on current main and confirmed that compressor v3 passes the full session message list into SessionExtractContextProvider / ExtractLoop, while VLMConfig only exposes an output max_tokens limit.
I can implement this together with #3927 as one focused change:
- Add an optional
vlm.max_input_tokenssetting (0/unset keeps current behavior). - Apply the budget at the session-extraction boundary, after vision-message normalization and before extraction prompts are assembled.
- Preserve the latest messages, while retaining required fixed context such as the extraction instruction and latest archive overview.
- Use the existing conservative token-estimation utilities rather than adding a tokenizer dependency.
- Add config round-trip/validation tests and compressor v3 regression tests covering no-limit behavior, recent-message retention, and bounded oversized input.
I would keep this scoped to session memory extraction/compression rather than silently truncating every VLM call, since media parsing and other VLM consumers have different input semantics.
Would this scope and truncation policy be acceptable? If so, I can send the implementation.
Thanks for reproducing and confirming. The scope and approach looks good — a few suggestions:
1. Token estimation accuracy
The current conservative estimator is what caused the overflow in the first place (estimator said ~12K, actual was ~25K — nearly 2x gap). Consider adding a safety margin (e.g., 15-20%) to the estimator so the effective limit is max_input_tokens * 0.85 rather than hitting the boundary exactly. Better to slightly under-utilize than to silently overflow.
2. Default behavior
Leaving current (unbounded) behavior as default means users who don't explicitly set max_input_tokens remain at risk. Two options:
- Default to a safe value derived from
ctx-size(e.g.,ctx-size * 0.75) - Or at minimum, emit a warning log when extraction input approaches the VLM context window
3. Truncation strategy
Keeping latest messages is correct for extraction, but also consider keeping the earliest N messages (often contain the user's original intent/goal, which is high-signal for memory extraction). A hybrid approach: keep system + earliest 2-3 messages + archive overview + fill the rest with most recent messages.
4. Per-session budget (ties into #3927)
Beyond per-call limits, consider a per-session token budget — e.g., max X tokens spent on extraction per session per hour. This prevents runaway extraction on extremely long sessions that trigger repeated compression cycles.
Happy to test once the PR is up.
Thanks for your contribution. The overflow is a valid reliability issue, but I don't think a global vlm.max_input_tokens with silent truncation is the right abstraction. Commit cadence and batch size should be controlled by the caller through the per-session auto_commit_policy. OpenViking should still protect the VLM request boundary because it owns the final extraction prompt, including schemas, archive overview and prefetched memories. Prefer turn-aware chunked extraction with split-on-overflow fallback over silently dropping old messages. We will carry out optimizations in subsequent iterations.