Repository navigation
studio: use generation usage for conversation search budgets - #9438
Imagineer99 merged 3 commits into
Conversation
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Using the real prompt token count here is the right direction, but the no-usage branch drops the budget to zero and empties the search rather than falling back to the dense estimate the way the GGUF loop does at llama_cpp.py:25264; could you keep that fallback and also drop the test rewrites that are not about this change? |
Lyxot
left a comment
There was a problem hiding this comment.
Using the real prompt_tokens is the right instinct, but I think the branch has two blocking problems as written.
1. The same-turn delta is measured in bytes and spent as tokens. spent = prompt_tokens + added_cost + 64, where added_cost is a UTF-8 byte delta of the JSON-serialised conversation max'd against completion_tokens, then subtracted from a token budget. That's ~4x over on ASCII and ~3x on CJK. Reproduced against the loop with an 8K window and a model that writes a normal English preamble before calling the tool:
prompt_budget = 7680
real prompt tokens = 3000
real completion tokens = 1120 (4480-char preamble)
honest room = 3496
computed budget = 0
charged for the delta = 4616 tokens (real: 1120)
So on a plain English turn the search is switched off with 3.5K tokens of room left — worse than the estimator it replaces. The existing tests don't catch this because their fixtures are small enough that the byte delta stays negligible.
2. Agreed on the no-usage branch, and it's the same failure the recall path already learned once: budget = 0 reaches tools.py:10255 as "There is no room left in this context to search earlier conversation", and llama_cpp.py:1034 already carries the note that flooring to zero "disabled recall on exactly the tight windows that need it."
Both problems disappear if the path measures instead of extrapolating. The safetensors backend owns the tokenizer and _apply_chat_template_for_generation, so it can count the current conversation exactly at the tool-call site the way the GGUF loop does at llama_cpp.py:25029 — exact count, estimator only as the raise-fallback. That drops the byte delta, the + 64, and the zero branch in one go.
Two smaller notes:
- The in-process holder is reset at the top of
generate_stream, but the orchestrator'sstats_holderis only written ongen_doneand never cleared ongen_error— same holder, two freshness contracts. - Deleting
test_the_dense_estimate_matches_the_flat_one_on_plain_asciiremoves the only parity coverage ofestimate_messages_tokens_dense, which is still used byllama_cpp.py,instruction_pin.pyandroutes/inference.py.
Also worth rebasing before another round — main has since replaced prompt_budget here with retrieval_budget(..., reply_returns = True) and added a result_budget_tokens path, and the branch is currently conflicting.
ec062d0 to
70c457e
Compare
|
Rebased onto 1. Bytes spent as tokens. Gone. Same fixture as yours, 8K window, 2. The zero floor. Gone with it. There is no 3. Counting exactly at the tool-call site. Not done, and the premise does not hold on the path that runs. The nearest thing to an exact count without a new worker round trip per tool call is the count the worker already makes: it tokenises each turn's prompt to run it ( 4. Two freshness contracts. Fixed by giving the loop a holder that only ever describes the latest turn. In the orchestrator, 5. 6. Regression coverage.
Not changed. The Verification. 760 passed across |
2d29928 to
1b2b299
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Checked the rebase and the new _spent_prompt_tokens path in studio/backend/core/inference/safetensors_agentic.py, and the byte delta and the zero floor are both gone. The only thing left is the assert reformat in studio/backend/tests/test_health_answers_within_probe_budget.py, which is not part of this change; could you drop it so the branch is just the budget work? |
|
The budget fix looks good and the earlier functional concerns are addressed. Daniel’s remaining request is still outstanding: studio/backend/tests/test_health_answers_within_probe_budget.py is still in the PR diff. Please rebase onto current main, confirm only the four budget-related files remain, and rerun the focused checks. |
The loop priced `search_conversation` by estimating the whole thread at four characters per token, so a turn that wrote a long preamble before calling the tool was charged whatever its characters implied rather than what it cost. The backend already tokenises each turn's prompt to run it and ships that count on gen_done, so spend that and estimate only what the loop appended afterwards. Both call sites hand the loop a holder that carries the LATEST turn only, cleared on the way in: `InferenceOrchestrator.generate_chat_completion_with_tools` hoists its existing per-turn `turn_stats` out of `_single_turn`, and `InferenceBackend` reads back `last_generation_stats`. The request-level `stats_holder` sums turns for the reply's usage report and is not what sizes a budget. No exact recount at the tool-call site: the loop runs in the parent process and `InferenceOrchestrator.models` mirrors the worker's model_info, which carries no tokenizer. Without a report the dense estimate covers the whole thread, as before, rather than reporting no room at all. Measured on an 8K window (prompt budget 7680) with a reported prompt of 3067 tokens and a 4600-character preamble: the search gets 3420 tokens. The same preamble charged as its 4600 bytes leaves 0.
f6189f2 to
447f339
Compare
|
Fixed in |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Rechecked The diff is limited to the four intended files, the remaining red test is inherited from |
Use request-local safetensors generation usage to size conversation search results against the model context window.
InferenceBackend.generate_streamintorun_safetensors_tool_loop.Checks