Skip to content

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@ describe('Vercel AI integration', () => {
'vercel.ai.settings.maxSteps': 1,
'vercel.ai.streaming': false,
'gen_ai.response.model': 'mock-model-id',
'gen_ai.usage.input_tokens': 15,
'gen_ai.usage.output_tokens': 25,
'gen_ai.usage.total_tokens': 40,
'operation.name': 'ai.generateText',
'sentry.op': 'gen_ai.invoke_agent',
'sentry.origin': 'auto.vercelai.otel',
Expand Down Expand Up @@ -550,6 +553,9 @@ describe('Vercel AI integration', () => {
'vercel.ai.settings.maxSteps': 1,
'vercel.ai.streaming': false,
'gen_ai.response.model': 'mock-model-id',
'gen_ai.usage.input_tokens': 15,
'gen_ai.usage.output_tokens': 25,
'gen_ai.usage.total_tokens': 40,
'operation.name': 'ai.generateText',
'sentry.op': 'gen_ai.invoke_agent',
'sentry.origin': 'auto.vercelai.otel',
Expand Down
73 changes: 72 additions & 1 deletion packages/core/src/utils/vercel-ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,34 @@ function onVercelAiSpanStart(span: Span): void {
processGenerateSpan(span, name, attributes);
}

interface TokenSummary {
inputTokens: number;
outputTokens: number;
}

function vercelAiEventProcessor(event: Event): Event {
if (event.type === 'transaction' && event.spans) {
// Map to accumulate token data by parent span ID
const tokenAccumulator: Map<string, TokenSummary> = new Map();

// First pass: process all spans and accumulate token data
for (const span of event.spans) {
// this mutates spans in-place
processEndedVercelAiSpan(span);

// Accumulate token data for parent spans
accumulateTokensForParent(span, tokenAccumulator);
}

// Second pass: apply accumulated token data to parent spans
for (const span of event.spans) {
if (span.op !== 'gen_ai.invoke_agent') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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?

continue;
}

applyAccumulatedTokens(span, tokenAccumulator);
}
}

return event;
}
/**
Expand Down Expand Up @@ -241,6 +262,56 @@ export function addVercelAiProcessors(client: Client): void {
client.addEventProcessor(Object.assign(vercelAiEventProcessor, { id: 'VercelAiEventProcessor' }));
}

/**
* Accumulates token data from a span to its parent in the token accumulator map.
* This function extracts token usage from the current span and adds it to the
* accumulated totals for its parent span.
*/
function accumulateTokensForParent(span: SpanJSON, tokenAccumulator: Map<string, TokenSummary>): void {
const parentSpanId = span.parent_span_id;
if (!parentSpanId) {
return;
}

const inputTokens = span.data[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE];
const outputTokens = span.data[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE];

if (typeof inputTokens === 'number' || typeof outputTokens === 'number') {
const existing = tokenAccumulator.get(parentSpanId) || { inputTokens: 0, outputTokens: 0 };

if (typeof inputTokens === 'number') {
existing.inputTokens += inputTokens;
}
if (typeof outputTokens === 'number') {
existing.outputTokens += outputTokens;
}

tokenAccumulator.set(parentSpanId, existing);
}
}

/**
* Applies accumulated token data to the `gen_ai.invoke_agent` span.
* Only immediate children of the `gen_ai.invoke_agent` span are considered,
* since aggregation will automatically occur for each parent span.
*/
function applyAccumulatedTokens(span: SpanJSON, tokenAccumulator: Map<string, TokenSummary>): void {
const accumulated = tokenAccumulator.get(span.span_id);
if (!accumulated) {
return;
}

if (accumulated.inputTokens > 0) {
span.data[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] = accumulated.inputTokens;
}
if (accumulated.outputTokens > 0) {
span.data[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE] = accumulated.outputTokens;
}
if (accumulated.inputTokens > 0 || accumulated.outputTokens > 0) {
span.data['gen_ai.usage.total_tokens'] = accumulated.inputTokens + accumulated.outputTokens;
}
}

function addProviderMetadataToAttributes(attributes: SpanAttributes): void {
const providerMetadata = attributes[AI_RESPONSE_PROVIDER_METADATA_ATTRIBUTE] as string | undefined;
if (providerMetadata) {
Expand Down
Loading