fix(llm): embedding requests within the model's token budget #582

Merged
sharang merged 1 commits from fix/embed-token-budget into main 2026-10-30 07:22:31 +00:00
Owner

Backlog t26.

Finding: on 16 and 22 Oct, pruefwerk-agent-prod (and the gateway) logged 7 context-length refusals each from the embedding model (counted in SigNoz without reading prod log bodies). bge-multilingual-gemma2 takes 8192 tokens. Code chunks are capped at 2048 estimated tokens (extract_chunks(.., 2048), 4 chars per token, which undercounts code), but embed() sent 16 of them in one request: ~51k tokens, refused, and the repo's embedding build failed.

Fix (compliance-agent/src/llm/embedding.rs):

  • plan_batches: consecutive batches of at most 16 inputs and 7000 estimated tokens (3 bytes per token, conservative for code). An 8 KiB chunk is ~2731 → two per request.
  • fit_text: a single text that would not fit alone is cut at a char boundary. The head carries the context header and signature.
  • Order is kept across batches (the vectors are concatenated per range).

This works whether the 8k limit is per input or per request; LiteLLM's model config isn't in a repo, so I couldn't check which.

Cost: code-heavy embedding builds now make about 8× more requests (2 chunks instead of 16 each). Requirement texts and queries are short, so they still go 16 per request.

Tests (new): batching by count (40 → 16/16/8) and by budget (5 × 8 KiB → 2/2/1), each batch within the budget; a 60 KB multibyte text cut at a char boundary to fit alone.

  • cargo fmt and clippy -D warnings clean.
  • cargo test -p compliance-agent: 956 unit tests plus all integration tests passed (locally with a podman MongoDB).

🤖 Generated with Claude Code

https://claude.ai/code/session_01KYcAAeTw22GE9QPzDM8yc4

Backlog t26. **Finding:** on 16 and 22 Oct, `pruefwerk-agent-prod` (and the gateway) logged 7 context-length refusals each from the embedding model (counted in SigNoz without reading prod log bodies). bge-multilingual-gemma2 takes 8192 tokens. Code chunks are capped at 2048 *estimated* tokens (`extract_chunks(.., 2048)`, 4 chars per token, which undercounts code), but `embed()` sent 16 of them in one request: ~51k tokens, refused, and the repo's embedding build failed. **Fix** (`compliance-agent/src/llm/embedding.rs`): - `plan_batches`: consecutive batches of at most 16 inputs **and** 7000 estimated tokens (3 bytes per token, conservative for code). An 8 KiB chunk is ~2731 → two per request. - `fit_text`: a single text that would not fit alone is cut at a char boundary. The head carries the context header and signature. - Order is kept across batches (the vectors are concatenated per range). This works whether the 8k limit is per input or per request; LiteLLM's model config isn't in a repo, so I couldn't check which. **Cost:** code-heavy embedding builds now make about 8× more requests (2 chunks instead of 16 each). Requirement texts and queries are short, so they still go 16 per request. **Tests** (new): batching by count (40 → 16/16/8) and by budget (5 × 8 KiB → 2/2/1), each batch within the budget; a 60 KB multibyte text cut at a char boundary to fit alone. - `cargo fmt` and `clippy -D warnings` clean. - `cargo test -p compliance-agent`: 956 unit tests plus all integration tests passed (locally with a podman MongoDB). 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01KYcAAeTw22GE9QPzDM8yc4
sharang added 1 commit 2026-10-29 17:53:16 +00:00
fix(llm): embedding requests within the model's token budget
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 17m13s
a21fd07203
bge-multilingual-gemma2 takes 8192 tokens; on prod (16 and 22 Oct, 7
each) a batch of 16 code chunks came to ~51k and was refused, so the
repo's embedding build failed (backlog t26). Chunks are capped at 2048
estimated tokens, but at 4 chars per token, which undercounts code, and
16 of them went into one request.

embed() now plans batches by input count (16) and an estimated token
budget (7000 per request, 3 bytes per token), and cuts a single text
that would not fit alone at a char boundary (the head keeps the context
header and signature). Order is kept across batches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KYcAAeTw22GE9QPzDM8yc4
sharang merged commit 1135b65b9e into main 2026-10-30 07:22:31 +00:00
Sign in to join this conversation.