Repository navigation
fix: parallel tool metrics - #4193
Merged
VascoSch92 merged 2 commits intoJul 23, 2026
Merged
Conversation
luciobaiocchi
force-pushed
the
fix/4189-parallel-tool-metrics
branch
2 times, most recently
from
July 22, 2026 15:40
ebb3b39 to
ade86c1
Compare
luciobaiocchi
marked this pull request as ready for review
July 22, 2026 15:51
VascoSch92
reviewed
Jul 23, 2026
VascoSch92
reviewed
Jul 23, 2026
…a parallel batch Parallel tool calls all come from one LLM response, so they share a single llm_response_id and a single per-request TokenUsage. The per-request metrics subtitle from OpenHands#4105 was therefore rendered identically under every sibling of the batch, reading as N separate requests worth of tokens (OpenHands#4189). Only the first ActionEvent of a batch (the batch primary) now shows the per-request numbers; siblings fall back to the labeled totals-only format. A batch's events are always rendered consecutively (one LLM response emits all its tool calls before the next response exists), so the primary is detected by tracking just the previous ActionEvent's response id -- O(1) per event, no scan of the event stream, which keeps rendering cheap on long benchmark trajectories. The detector (_claim_batch_primary) is named to signal that it mutates, and a test in test_response_dispatch.py pins the consecutive-emission invariant it relies on. Single tool calls and non-ActionEvents are always their own primary, so their output is unchanged.
luciobaiocchi
force-pushed
the
fix/4189-parallel-tool-metrics
branch
from
July 23, 2026 09:00
ade86c1 to
0040c84
Compare
VascoSch92
approved these changes
Jul 23, 2026
Member
|
@luciobaiocchi Thanks :-D |
1 of 5 tasks
5 tasks
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.
HUMAN:
This is a fix follow-up to #4146, raised by @VascoSch92 in the approving review.
AGENT:
Why
The per-request metrics subtitle matches an event to its usage by
llm_response_id.When one LLM response emits multiple parallel tool calls, every resulting
ActionEventshares that onellm_response_idand oneTokenUsage, so theidentical per-request line was rendered under each sibling:
The numbers are correct but read as three separate 2K requests. Totals are
unaffected. Fixes #4189.
Summary
ActionEventof a batch (samellm_response_id) shows theper-request numbers; siblings fall back to the labeled totals-only format from
fix(visualizer): show per-request token usage alongside cumulative to… #4146 (
↑ input (total 3K) • …)._claim_batch_primary(event)decides this in O(1) by tracking only theprevious ActionEvent's
llm_response_id. A batch's events always renderconsecutively (one LLM response emits all its tool calls before the next
response exists), so a single remembered id is enough to tell a batch primary
from its siblings — no scan of the event stream. It's named to signal that it
mutates, and must be called once per rendered event.
_format_metrics_subtitlegains a keyword-onlyforce_totalsflag; the tworender entry points (
DefaultConversationVisualizer._create_event_blockandDelegationVisualizer's action path) passforce_totals=not _claim_batch_primary(event).ActionEvents are always their own primary, sotheir output is unchanged.
Design decision: O(1) tracking vs. stateless scan
The first draft determined the batch primary statelessly, by scanning
state.eventsfor the firstActionEventsharing the response id. That wasidempotent and replay-safe, but it is O(N) per rendered ActionEvent — the
primary renders before it is appended to state, so the scan walks the whole
conversation to the end — i.e. O(M²) over an M-event trajectory, against a
disk-backed (cached) event store.
The human author (Lucio) preferred keeping the visualizer cheap on long
benchmark / eval trajectories and asked for the O(1) approach even though it
carries a little render-time state. We adopted the O(1) previous-
response_idtracking above. It is not fully idempotent (rendering the exact same event twice
in isolation would flip primary→sibling), but that is a test-only artifact: in
production each event streams through
on_eventexactly once and batches areconsecutive, and because it remembers only the immediately-preceding id, a full
replay still re-derives the correct result — so it stays replay-safe, strictly
better than a growing
setof shown ids.Two mitigations keep the small residual risk in check: the method name signals
its mutation, and
test_batch_action_events_are_emitted_consecutively(
tests/sdk/agent/test_response_dispatch.py) pins the consecutive-emissioninvariant the O(1) shortcut relies on — if a future change interleaves event
emission, that test fails before the metrics silently mislabel.
Considered alternative (possible follow-up): mark the batch index on the event
The cleanest design would make the decision a pure function of the event —
record the tool-call's batch index on
ActionEventat construction (the agentalready computes it:
for i, tool_call in enumerate(message.tool_calls)inresponse_dispatch.py, branching oni == 0), so the visualizer would just readevent.batch_index == 0. That removes the mutation and the consecutive-emissionassumption while staying O(1), and a real batch index could also simplify the
batch regrouping in
events_to_messages.It is intentionally not done here:
Eventisextra="forbid", so adding apersisted field to a core event type carries the same backward-compat risk that
closed #4147 (older readers rejecting new events), and it touches the sync/async
construction paths, the ACP agent, and many tests. That is a data-model change
that deserves its own PR and maintainer sign-off on the event-schema/compat
policy, rather than riding along in a visualizer fix.
Issue Number
Fixes #4189
How to Test
Four new tests:
_claim_batch_primarytracked across consecutive batches;end-to-end
_create_event_blockassertions on the default and delegatevisualizers (primary shows per-request, siblings show totals-only); and
test_batch_action_events_are_emitted_consecutively, which pins the emissioninvariant the O(1) detection relies on. All 43 visualizer tests and the full
tests/sdk/conversation+tests/tools/delegate+tests/sdk/agent/test_response_dispatchsuites pass;
ruffandpyrightclean.Video/Screenshots
Simulating the real callback order (each event renders before it is appended to
state), a 3-way parallel batch after a prior request (running total 3K/300):
Type
Notes
fix(visualizer): show per-request token usage alongside cumulative to… #4146 has merged to
main, this should be retargeted tomainwhen openedupstream against
OpenHands/software-agent-sdk.