Repository navigation
Predict query result sizes instead of re-serializing per candidate - #168
Merged
Merged
Conversation
Once the token budget fills, query_context measured the whole payload for every remaining ranked group, making selection O(groups x payload size). Each candidate is now serialized once and its exact size predicted from a running total; _measure only confirms candidates that fit. Rejected candidates still leave the same used_tokens value the old loop did, so the payload stays byte-identical at power-of-ten boundaries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Broad context queries now check most candidates against the token budget without re-serializing the whole payload, and the output is unchanged.
Problem
Once the budget filled before
limitwas reached,query_contextcalled_measure(payload)for every remaining ranked group. Each call ranjson.dumpson the whole payload, so selection took O(groups × payload size). On a 3,000-document project, the query "retry backoff" took about 2.6s, roughly half of it injson.dumps.Change
omitted_passages._settlerepeats_measure's fixed-point loop forused_tokensusing integers only._measure.used_tokensvalue the old loop left behind. That value can affect the next measurement near a power of ten, such as 999 versus 1001, so keeping it keeps the payload byte-identical.Checks
uv run pytest tests/test_context_query.py -q: 18 passed.test_broad_query_matches_full_measurement: the budget fills beforelimiton a 200-passage query. The test forces every candidate through the original_measurepath and asserts that the serialized payloads are identical.ruff check,ruff format --checkandmypypass on the changed files.bun run validate:fullwas not run.🤖 Generated with Claude Code