feat(core): Accumulate tokens for gen_ai.invoke_agent spans from child LLM calls#17281
Conversation
size-limit report 📦
|
gen_ai.invoke_agent 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.
hmm, can we combine this into a single pass somehow? 🤔 that would be more efficient I suppose.
There was a problem hiding this comment.
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.
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
tmpAmountsand 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.
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.
| 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?
Problem
Currently,
gen_ai.invoke_agentspans (representing operations likegenerateText()) contain inaccurate token usage information. Users can only see token data on individualgen_ai.generate_textchild 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_agentspans by iterating over client LLM child spans and aggregating their token usage.