Skip to content

perf(recall): count each source fact's tokens once per recall (#6329) - #6405

Merged
nicoloboschi merged 3 commits into
mainfrom
triage/6329
Nov 7, 2026
Merged

nicoloboschi merged 3 commits into
mainfrom
triage/6329

Conversation

@nicoloboschi

@nicoloboschi nicoloboschi commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #6329. Based on @harshitgupta31415's patch (929aabd) — thanks for the report and the fix.

What changes

  • Per-observation budget: when several observations cite the same source fact, its tokens are counted once and that count is charged to each observation's own budget. Before, the same text was counted again for every observation.
  • Unlimited budget: no token counting at all, since the count cannot change what is returned.
  • Same facts selected, same order, same truncated flag. The default 4,096-token total budget path is unchanged apart from the guard it no longer needs.

Numbers (this Mac, real tokenizer, median of 5)

Case Before After
100 observations × same 100 sources, 256/obs 6.40 ms 0.76 ms
3,000 distinct sources, 256/obs 2.06 ms 1.94 ms
3,000 distinct sources, unlimited 1.91 ms 0.20 ms
3,000 distinct sources, 4,096 total (default) 1.91 ms 1.69 ms

The reporter's patch only cached counts when sources repeat; this one always caches (a dict lookup is cheaper than a tokenizer call), which keeps a single code path.

Tests

Two new tests in tests/test_source_facts_selection.py: a shared source is counted once but still charged to every observation, and an unlimited budget counts nothing. Both fail on main and pass here; the 10 existing tests still pass.

Observations that cite the same source no longer recount its text for every
per-observation budget, and an unlimited budget no longer counts at all.
@nicoloboschi
nicoloboschi merged commit b103fb1 into main Nov 7, 2026
223 of 224 checks passed
@nicoloboschi
nicoloboschi deleted the triage/6329 branch November 7, 2026 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

count shared source tokens once per recall (63.17 ms → 4.40 ms, 14.35× faster)

1 participant