Skip to content

feat(api): warn when a parent's token roll-up exceeds its own leaves - #5718

Open
mmabrouk wants to merge 1 commit into
fix/cost-estimation-correctnessfrom
fix/token-rollup-invariant
Open

feat(api): warn when a parent's token roll-up exceeds its own leaves#5718
mmabrouk wants to merge 1 commit into
fix/cost-estimation-correctnessfrom
fix/token-rollup-invariant

Conversation

@mmabrouk

@mmabrouk mmabrouk commented Aug 3, 2026

Copy link
Copy Markdown
Member

Stacked on #5714. Set the base to that branch, so this diff shows only its own change.

Why

A producer that repeats a child's token count on the parent gets counted twice, and nothing said so. The agent runner did exactly that, and the doubled totals reached analytics and evaluations, because ag.metrics.tokens.cumulative.total is a registered analytics metric.

The runner is fixed separately. This makes the next occurrence loud instead of silent.

What it does

After the roll-up, walk the forest once more and compare each parent's cumulative token total against the sum of its subtree's leaves. Log the offending span ids when they disagree. One extra depth-first pass per batch.

The check stays quiet when the leaves measured nothing. A span carrying only a run-level total, with no instrumented children, is a sanctioned shape and not a violation.

This detects rather than corrects, deliberately

Adding a parent's own incremental value to its children's cumulative is the right general definition, because a parent span can legitimately make its own model call. Silently suppressing it would give the wrong answer for honest producers.

So an old runner image posting to a new API still double counts. Now it names the span while doing so.

Verification

1,640 passing in OSS unit tests, 1,899 in EE.

Nine new tests drive the real ingest sequence: the corrected runner shape rolls up to the true total; the old shape yields 3,496 for a real 1,748 and is flagged; a four-turn tree counts each turn once; a leaf-only tree is unchanged; the reported cost still reaches the agent span; a run-level-only span is not a false positive; and the warning is actually logged.

@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Aug 3, 2026
@vercel

vercel Bot commented Aug 3, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agenta-documentation Ready Ready Preview Aug 3, 2026 10:20pm

Request Review

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 1b55336d-285a-4fc8-9408-0c258f35d9ef

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Railway Preview Environment

Preview URL https://gateway-pr-5718.up.railway.app/w
Project agenta-oss-clone-spike
Image tag pr-5718-83dcb4c
Status Deployed
Railway logs Open logs
Workflow logs View workflow run
Updated at 2026-08-03T22:21:56.529Z

A producer that repeats a child's token count on the parent gets counted twice, and
nothing said so. The agent runner did exactly that, and the resulting doubled totals
reached analytics and evaluations, because `ag.metrics.tokens.cumulative.total` is a
registered analytics metric. The runner is fixed separately; this makes the next
occurrence loud instead of silent.

After the roll-up, walk the forest once more and compare each parent's cumulative
token total against the sum of its subtree's leaves. Log the offending span ids when
they disagree. One extra depth-first pass per batch.

The check deliberately stays quiet when the leaves measured nothing. A span that
carries only a run-level total, with no instrumented children, is a sanctioned shape
and not a violation.

This detects rather than corrects. Adding a parent's own incremental value to its
children's cumulative is the right general definition, because a parent span can
legitimately make its own model call, so silently suppressing it would be wrong for
honest producers. An old runner image posting to a new API will still double count,
and now it will name the span while doing so.

Tests: 1,640 passing in OSS and 1,899 in EE. Nine new tests drive the real ingest
sequence: the corrected runner shape rolls up to the true total, the old shape yields
3,496 for a real 1,748 and is flagged, a four-turn tree counts each turn once, a
leaf-only tree is unchanged, the reported cost still reaches the agent span, and a
run-level-only span is not a false positive.

Claude-Session: https://claude.ai/code/session_01RkWWQUNNzRbaB5jnCAdjYA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Backend size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant