WeKnora icon indicating copy to clipboard operation
WeKnora copied to clipboard

[Bug]: runtime_context renders the full bound-KB catalog with no size cap — kb=all agents on multi-KB tenants exceed the context window at round 1 and burn tokens in futile rounds

Open vicfei opened this issue 2 months ago • 1 comments

Affected Component

Backend — agent runtime-context assembly and the context-management loop:

  • internal/agent/prompts.go:130-200 — formatKnowledgeBaseList
  • internal/agent/observe.go:510-562 — buildRuntimeContextBlock
  • internal/agent/engine.go:642 — manageContextWindow orchestration

Problem

buildRuntimeContextBlock renders the bound knowledge bases for the turn into the current user message, and formatKnowledgeBaseList has no cap on total size:

  • the for _, kb := range kbInfos loop is linear in the number of bound KBs (prompts.go:137);
  • each KB renders id/name/type/doc_count/capabilities + a full, untruncated description (prompts.go:148-150) + recent-document entries — 2 for document KBs (prompts.go:174-188) and up to 10 FAQ entries including full answers for FAQ KBs (prompts.go:152-173).

For an agent whose KB selection is "all knowledge bases" on a tenant with many KBs, the catalog alone exceeds the model's context window before the conversation has even started.

Measured

Synthetic catalog cost on upstream/main (each KB: realistic ~200-char description, 2 recent docs with the capped 120-char summaries the code already applies):

bound KBs catalog size
10 20.7 KB
50 103 KB
100 206 KB (~107k chars)
300 619 KB
541 1.12 MB (~581k chars)

≈ 2 KB per KB, strictly linear, no ceiling. 100 KBs is already window-threatening for 128k-context models. Observed on a large multi-KB deployment (541 KBs): the first-turn user message alone estimated at 219,319 tokens against a ~200k window.

What happens next (the amplifier)

Every downstream safeguard then behaves as designed but none can help, because the oversized input is a birth defect rather than accumulated growth:

  1. the token estimator correctly alarms (over the compaction threshold);
  2. the compactor correctly declines — there are 0 history messages and the current turn's user message is exempt by design, so there is nothing it is allowed to compact;
  3. no over-window rejection exists before the model call — the request goes out anyway;
  4. the runtime context is re-rendered into the current user message every round, so each futile round re-sends the whole catalog. Observed in the same deployment: ~15 rounds × ~175k prompt tokens ≈ 2.6M tokens burned per question, and a 40-question batch ≈ 100M tokens with no usable answers produced.

Related: #2981 (configurable context window) is complementary but orthogonal — even with the true window configured, the unbounded catalog still exceeds it.

Why this matters

  • Multi-KB tenants are the normal enterprise shape, and "all knowledge bases" is a default agent configuration — this is reachable without any exotic setup.
  • Context compaction shipped as a headline v0.8.0 feature; this is the one input shape it structurally cannot handle, so the failure mode is silent cost amplification rather than a visible error.

Suggested fix directions (independent; either helps, both are cheap)

  1. Fail-fast guard: before the model call, if the estimated turn input exceeds the context window, reject with a clear error (or degrade the scope) instead of looping — this alone turns a 2.6M-token question into a 1-request error.
  2. Budget the catalog: cap the rendered form by size — e.g. N full-detail KBs followed by a name-only list for the rest, truncated descriptions, and the same treatment for FAQ entries. Routing information survives in compressed form.

Happy to submit a PR for either or both if the approach sounds good.

WeKnora Version

upstream/main @ b60351f1 (all line references verified on this revision)

vicfei avatar Sep 10 '26 06:09 vicfei

Both guards from this issue are up as #3222: the catalog budget (full detail degrades to id-preserving single lines, hard ceiling with an omission note — 541-KB catalog drops ~666KB → ~110KB with all routing ids intact) and a fail-fast check that aborts the turn before the model call when the estimated request cannot fit the window. The per-entry slimming that landed in #3193 is kept as-is; this adds the missing total bound.

vicfei avatar Sep 12 '26 14:09 vicfei