Skip to content

fix(budget): pass the handoff call's usage split to the run budget - #136

Open
Siddhant-K-code wants to merge 1 commit into
google:mainfrom
Siddhant-K-code:handoff-usage-split
Open

Siddhant-K-code wants to merge 1 commit into
google:mainfrom
Siddhant-K-code:handoff-usage-split

Conversation

@Siddhant-K-code

Copy link
Copy Markdown
Contributor

At the context ceiling, _make_node_context_ceiling_before_model_callback asks the stage's model for a handoff and charges that call with bc.record_tokens(used), where used is only total_token_count. The other direct calls (consolidator, planner, calibrator, meter_response_usage) pass the full split, and this one was missed in #78.

So for the handoff call:

  • Cached input is charged at full price. The handoff runs on the stage's whole prefix, which is usually cached, so the bill is close to the full request.
  • Its prompt, output and reasoning tokens don't show up on the spend report.

What changes

  • _write_handoff returns (text, usage_metadata) instead of (text, total), and ("", None) when there is no model or the call fails.
  • The caller passes cached_count, prompt_count, output_count and reasoning_count to record_tokens, the same way the calibrator does.

Tests

  • test_node_context_handoff_is_charged_with_its_usage_split in tests/test_adk_invariants.py runs a handoff with 8,000 of 10,000 tokens cached and 400 reasoning tokens, then checks the cached, fresh, prompt, output and reasoning totals. On main it fails, charging all 10,000 tokens as fresh.
  • test_adk_invariants.py, test_p2_llm_wire.py, test_fork.py and test_spend_observations.py pass, pre-commit passes, and the 12 mutants for the changed files are killed.

At the context ceiling a stage asks its model for a handoff, and that call
was charged with record_tokens(total) alone. Cached input was billed at full
price, so a handoff on a long cached prefix used up the budget faster than
the same tokens anywhere else, and its prompt, output and reasoning tokens
were missing from the spend report.

_write_handoff now returns the call's usage metadata instead of the total,
and the caller passes the cached, prompt, output and reasoning counts, like
the other direct model calls. A test with a cached, reasoning handoff fails
on main (all 10,000 tokens charged fresh) and passes here. The ADK
invariants, wire, fork and spend observation suites, the changed-file
mutants and pre-commit pass.

Assisted-by: GitHub Copilot

This branch has not been deployed

No deployments
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.

1 participant