-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Accumulate tokens for gen_ai.invoke_agent
spans from child LLM calls
#17281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): Accumulate tokens for gen_ai.invoke_agent
spans from child LLM calls
#17281
Conversation
size-limit report 📦
|
gen_ai.invoke_agen
t spans from child LLM callsgen_ai.invoke_agent
spans from child LLM calls
packages/core/src/utils/vercel-ai.ts
Outdated
// Second pass: accumulate tokens for gen_ai.invoke_agent spans | ||
// TODO: Determine how to handle token aggregation for tool call spans. | ||
for (const span of event.spans) { | ||
accumulateTokensFromChildSpans(span, event.spans); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, can we combine this into a single pass somehow? 🤔 that would be more efficient I suppose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can but it's safer to leave them separate because we are processing child spans to accumulate the tokens, we need to make sure they are all transformed completely to the attribute we are expecting, otherwise we could end up with a parent span first that have child spans that still use vercel naming so we end up not processing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm overall this is quite expensive, looking at it :D because we loop over all spans a lot:
- Once in the first pass
- Once in the second pass
a. for each second pass, we iterate over all spans again when filtering
b. then finally once more over the filtered spans (which is a subset already, but still)
IMHO it's probably worth it to find a way to streamline this to get to max. 2 passes. I would propose something like this:
- We create an object to hold the summarized tokens. e.g. something like this:
const tmpAmounts: Map<string, TokenSummary> = new Map();
- We pass this into the first pass function as second argument, so that can mutate it
- The function that transforms attributes, at the very end, updates the passed in map by incrementing the token summary for the map item of the parentSpanId, e.g. something like this (simplified...):
const parentSpanId = spanToJSON(span).parent_span_id;
if (parentSpanId) {
const parentSummary = tmpAmounts.get(parentSpanId) || { total: 0 };
parentSummary.total += amount;
tmpAmounts.set(parentSpanId, parentSummary);
}
- Finally, after the first pass, we iterate over
tmpAmounts
and update the parent spans with the stored summaries.
I think this or something like this should reduce overhead considerably 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very nice, i'll try it out.
|
||
// Second pass: apply accumulated token data to parent spans | ||
for (const span of event.spans) { | ||
if (span.op !== 'gen_ai.invoke_agent') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (span.op !== 'gen_ai.invoke_agent') { | |
if (span.op !== 'gen_ai.invoke_agent' || !tokenAccumulator.has(span.span_id)) { |
should also be a reasonable shortcut?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sweet!
Problem
Currently,
gen_ai.invoke_agent
spans (representing operations likegenerateText()
) contain inaccurate token usage information. Users can only see token data on individualgen_ai.generate_text
child spans, but the tokens are not accumulated across nested spans, making it difficult to track total token consumption for complete AI operations.Solution
Implement token accumulation for
gen_ai.invoke_agent
spans by iterating over client LLM child spans and aggregating their token usage.