🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(otel): do not cost AI SDK agent spans that duplicate model-call usage (#14808)
Hassieb Pakzad310f1d060d18
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.tsindex 4b9ac69be1..9fa0b661ae 100644--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts@@ -356,12 +356,19 @@ export class OtelIngestionProcessor {source: "event" as const,};+ // AI SDK agent spans carry aggregate usage duplicating their+ // child model-call spans — skip model/usage/cost for them.+ const isAiSdkAgentSpan =+ this.isAiSdkAgentOperation(spanAttributes);+const usageDetails = UsageDetails.safeParse(- this.extractUsageDetails(- spanAttributes,- scopeSpan?.scope?.name ?? "",- spanContext,- ),+ isAiSdkAgentSpan+ ? {}+ : this.extractUsageDetails(+ spanAttributes,+ scopeSpan?.scope?.name ?? "",+ spanContext,+ ),);if (!usageDetails.success) {logger.warn(@@ -444,7 +451,9 @@ export class OtelIngestionProcessor {spanAttributes,scopeSpan?.scope?.name ?? "",),- modelName: this.extractModelName(spanAttributes),+ modelName: isAiSdkAgentSpan+ ? undefined+ : this.extractModelName(spanAttributes),completionStartTime: this.extractCompletionStartTime(spanAttributes,startTimeISO,@@ -454,10 +463,9 @@ export class OtelIngestionProcessor {providedUsageDetails: usageDetails.success? usageDetails.data: undefined,- providedCostDetails: this.extractCostDetails(- spanAttributes,- spanContext,- ),+ providedCostDetails: isAiSdkAgentSpan+ ? {}+ : this.extractCostDetails(spanAttributes, spanContext),// Propertiestags: this.extractTags(spanAttributes),@@ -1003,6 +1011,10 @@ export class OtelIngestionProcessor {metadata,);+ // AI SDK agent spans carry aggregate usage duplicating their child+ // model-call spans — skip model/usage/cost for them.+ const isAiSdkAgentSpan = this.isAiSdkAgentOperation(attributes);+const observation = {id: this.parseId(span.spanId?.data ?? span.spanId),traceId,@@ -1036,7 +1048,7 @@ export class OtelIngestionProcessor {attributes,instrumentationScopeName,) as any,- model: this.extractModelName(attributes),+ model: isAiSdkAgentSpan ? undefined : this.extractModelName(attributes),promptName:attributes?.[LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_NAME] ??attributes["langfuse.prompt.name"] ??@@ -1047,12 +1059,16 @@ export class OtelIngestionProcessor {attributes["langfuse.prompt.version"] ??this.parseLangfusePromptFromAISDK(attributes)?.version ??null,- usageDetails: this.extractUsageDetails(- attributes,- instrumentationScopeName,- observationContext,- ),- costDetails: this.extractCostDetails(attributes, observationContext),+ usageDetails: isAiSdkAgentSpan+ ? {}+ : this.extractUsageDetails(+ attributes,+ instrumentationScopeName,+ observationContext,+ ),+ costDetails: isAiSdkAgentSpan+ ? {}+ : this.extractCostDetails(attributes, observationContext),input: normalizedToolMetadata.input,output,};@@ -2256,6 +2272,27 @@ export class OtelIngestionProcessor {return params;}+ /**+ * The Vercel AI SDK OTel integration (@ai-sdk/otel) emits an+ * `invoke_agent` span (and `agent_step` child spans) that carry aggregate+ * `gen_ai.usage.*` token counts duplicating the per-call usage on the+ * grandchild model-call span (`gen_ai.operation.name: "chat"`). Populating+ * model/usage/cost on the agent spans as well as the model-call span would+ * classify both as generations and double every trace's cost, so model,+ * usage, and cost extraction is skipped for these spans. Agent-type spans+ * from other instrumentations (e.g. OpenInference `AGENT` spans) are not+ * affected because the gate is on the operation name, not the observation+ * type.+ */+ private isAiSdkAgentOperation(attributes: Record<string, unknown>): boolean {+ const operationName = attributes["gen_ai.operation.name"];++ return (+ typeof operationName === "string" &&+ ["invoke_agent", "agent_step"].includes(operationName)+ );+ }+private extractModelName(attributes: Record<string, unknown>,): string | undefined {