Repository navigation
fix(visualizer): show per-request token usage alongside cumulative to… - #4146
Conversation
- Replace getattr with isinstance check for llm_response_id lookup - Use walrus operator in _find_request_usage; drop redundant final return - Fix DelegationVisualizer to forward event into _format_metrics_subtitle so ActionEvent/MessageEvent subtitles no longer stay cumulative-only while other event types show per-request numbers - Clarify docstring: response_id matching is not guaranteed on the ACP path - Give reasoning tokens and cache hit rate the same per-request "(total X)" treatment as input/output tokens, avoiding unlabeled cumulative numbers next to per-request ones - Strengthen test to assert the full formatted subtitle string instead of loose substring checks - Add regression tests for the DelegationVisualizer event-forwarding bug and the reasoning/cache-hit formatting bug
|
Thanks for the thorough review, @VascoSch92 — addressed all of it in 6c7d08f:
|
There was a problem hiding this comment.
Rebase looks good and last round's stuff is all handled: reasoning and cache hit are per-request now, delegate visualizer passes the event through in both spots.
I did go check whether the response_id lookup is actually safe after a resume, since that was my worry. It is. restore_metrics shares the object by reference so usage_to_metrics doesn't go stale, and merge concatenates per usage-id so token_usages[-1] genuinely would have been wrong. Good call there.
Two things left and they're really the same thing: numbers on that line that aren't labelled. Inline.
Also description still says three new tests, there's five.
Round-2 review feedback on OpenHands#4146: cost is always cumulative (Cost entries carry no response_id, unlike TokenUsage) and now says so; events with no attributable per-request usage (user messages, unknown/per-session response ids as in ACP) label every number (total) instead of printing bare cumulative values that read as per-request.
- cost prints as '$ (total 0.4231)' as suggested, so nothing on the line is unlabeled; a per-request cost needs response_id on Cost entries, which is a separate PR - _find_request_usage: collapse the double loop into next(...) - fix delegate test docstring: only Condensation carries an llm_response_id; AgentErrorEvent does not show per-request numbers
The fallback previously appended a bare '(total)' label; the review asks for '(total 54K)' so unattributable events (user messages, AgentErrorEvent, ACP) use the same shape as the per-request format and the cost field.
VascoSch92
left a comment
There was a problem hiding this comment.
LGTM.
There is just one thing with parallel tool execution that I saw just now. response_dispatch.py:205-210 emits one ActionEvent per tool call, all sharing the same llm_response_id, so a parallel batch repeats the identical per-request numbers.
Therefore, you will have duplication in the output (but this is just missleading is not affecting the final results).
It is not a blocker. But if you could open a follow up PR would be great :-) (but up to you)
yes for sure, i will open an issue first |
HUMAN:
While debugging LLM costs I kept misreading the running totals shown under
each event as per-request token usage. This PR makes the visualizer show
both, so the numbers finally mean what they look like.
AGENT:
Why
Fixes #4105. The metrics subtitle under each event showed only cumulative
totals ("↑ input 54k"), which read as per-request usage. Readers (including
while debugging cost issues) misinterpret running totals as single-request
numbers, and there was no way to tell the two apart.
Summary
_format_metrics_subtitle()now takes the event being rendered and showsper-request usage alongside the running total:
↑ input 4.63K (total 9.02K).Reasoning tokens and cache hit rate get the same per-request treatment.
event.llm_response_idagainstTokenUsage.response_id(_find_request_usage()), not by takingtoken_usages[-1]— the combined list merges agent/condenser usages and isnot chronological across them.
AgentErrorEvent,unknown or per-session response ids as in ACP) fall back to totals-only —
now labeled explicitly:
↑ input 54K (total) • … • $ (total 0.4231).Labeling was chosen over skipping because ACP events never match, so
skipping would remove the subtitle entirely for ACP transcripts.
$ (total …):Costentries carry noresponse_id(unlikeTokenUsage), so per-request cost cannot beattributed today (see Notes).
DelegationVisualizerforwards the event into_format_metrics_subtitlein both of its render paths, so delegate transcripts use the same format.
Issue Number
Fixes #4105
How to Test
7 new tests: per-request vs cumulative placement (full-string assertion),
per-request reasoning/cache-hit, response_id lookup vs naive last-index
(agent + condenser interleaved), labeled totals-only fallback (no event /
unknown response id), user-message fallback, delegate event forwarding, and
delegate labeled fallback. All 40 visualizer tests pass;
ruffandpyrightclean.Manual check: run any example (e.g.
uv run python examples/01_standalone_sdk/01_hello_world.py) and comparethe subtitle under an agent action (per-request + total) with the one under
a user message (labeled totals).
Video/Screenshots
Subtitle for an event with attributable usage:
Fallback (user message / ACP / error events):
Type
Notes
explicit
(total)labels. No API changes; visualizer-only.response_idtoCost(asResponseLatencyalready has) to enable true per-request cost. Kept out of this PR per
review discussion.