The Open Framework for Building Agents
fix(instrumentation): report subagent action cost (#3756)
vercel[bot]ba963055a1b6
diff --git a/.changeset/report-agent-action-cost.md b/.changeset/report-agent-action-cost.mdnew file mode 100644index 000000000..738b6e477--- /dev/null+++ b/.changeset/report-agent-action-cost.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Include provider-reported `costUsd` alongside token counts on terminal subagent action instrumentation events and spans.diff --git a/docs/guides/instrumentation/instrumentation.mdx b/docs/guides/instrumentation/instrumentation.mdxindex cef7204aa..82ed776ee 100644--- a/docs/guides/instrumentation/instrumentation.mdx+++ b/docs/guides/instrumentation/instrumentation.mdx@@ -169,6 +169,8 @@ operation key when upserting records across replays.An ordinary tool emits both `action.*` and `tool.call.*` events. Use `action.*` for eve's durable dispatch lifecycle, including tools, skills, subagents, and remote agents. Use `tool.call.*` only when you need the AI SDK's in-process tool execution boundary.+For local and remote subagents, `action.completed` and `action.failed` include the child turn's `usage` directly. Token counts are always present when the child reports usage; `usage.costUsd` is present only when the model provider reports a price. Without the action-level usage, consumers would have to correlate child model events through trace lineage and sum their provider metadata, which is harder to do reliably across retries and parked turns.+Handlers from different files run concurrently and are failure-isolated. Donot depend on execution order. Use `flush` to drain buffered records and`shutdown` to release resources.diff --git a/packages/eve/src/instrumentation/lifecycle.ts b/packages/eve/src/instrumentation/lifecycle.tsindex 2c2f31ff0..54d553ece 100644--- a/packages/eve/src/instrumentation/lifecycle.ts+++ b/packages/eve/src/instrumentation/lifecycle.ts@@ -36,6 +36,8 @@ export interface InstrumentationModelRef {/** Token usage for one model call. A field is absent when the provider omits it. */export interface InstrumentationUsage {+ /** Provider-reported cost in US dollars. Absent when the provider omits pricing. */+ readonly costUsd?: number;readonly inputTokenDetails?: {readonly cacheReadTokens?: number;readonly cacheWriteTokens?: number;@@ -529,6 +531,7 @@ export interface InstrumentationActionFailedEvent {readonly idempotencyKey: string;readonly outcome: Exclude<InstrumentationActionOutcome, "completed">;readonly scope: InstrumentationAttemptScope;+ readonly usage?: InstrumentationUsage;}export type InstrumentationActionTerminalEvent =diff --git a/packages/eve/src/instrumentation/native-events.test.ts b/packages/eve/src/instrumentation/native-events.test.tsindex 9f391d7b7..aa6aa8f89 100644--- a/packages/eve/src/instrumentation/native-events.test.ts+++ b/packages/eve/src/instrumentation/native-events.test.ts@@ -304,11 +304,29 @@ describe("createInstrumentationHandleEvent", () => {createActionResultEvent({result: {callId: "delegate-1",+ isError: true,kind: "subagent-result",- origin: "dispatch",+ origin: "child",+ outcome: {+ kind: "terminal",+ result: { kind: "failed", error: "unavailable" },+ usageDelta: {+ cacheReadTokens: 1,+ cacheWriteTokens: 2,+ costUsd: 0.003,+ inputTokens: 4,+ outputTokens: 2,+ },+ },output: "unavailable",- isError: true,subagentName: "worker",+ usage: {+ cacheReadTokens: 1,+ cacheWriteTokens: 2,+ costUsd: 0.003,+ inputTokens: 4,+ outputTokens: 2,+ },},sequence: 0,stepIndex: 0,@@ -336,6 +354,7 @@ describe("createInstrumentationHandleEvent", () => {usage: {cacheReadTokens: 3,cacheWriteTokens: 4,+ costUsd: 0.012,inputTokens: 10,outputTokens: 5,},@@ -414,6 +433,12 @@ describe("createInstrumentationHandleEvent", () => {outcome: "failed",scope,type: "action.failed",+ usage: {+ costUsd: 0.003,+ inputTokenDetails: { cacheReadTokens: 1, cacheWriteTokens: 2 },+ inputTokens: 4,+ outputTokens: 2,+ },});expect(events[6]).toEqual({acceptedAtMs: 1_234,@@ -423,6 +448,7 @@ describe("createInstrumentationHandleEvent", () => {scope,type: "action.completed",usage: {+ costUsd: 0.012,inputTokenDetails: { cacheReadTokens: 3, cacheWriteTokens: 4 },inputTokens: 10,outputTokens: 5,diff --git a/packages/eve/src/instrumentation/native-events.ts b/packages/eve/src/instrumentation/native-events.tsindex 9bb683ebd..c1476ee64 100644--- a/packages/eve/src/instrumentation/native-events.ts+++ b/packages/eve/src/instrumentation/native-events.ts@@ -270,6 +270,7 @@ async function publishActionTerminal(outcome: event.data.status,scope,type: "action.failed",+ usage: actionUsage(event.data.result),} satisfies InstrumentationActionFailedEvent),);}@@ -282,7 +283,9 @@ function actionUsage(result: RuntimeActionResult): InstrumentationUsage | undefi) {return undefined;}- return {+ const usage: {+ -readonly [K in keyof InstrumentationUsage]: InstrumentationUsage[K];+ } = {inputTokenDetails: {cacheReadTokens: result.usage.cacheReadTokens,cacheWriteTokens: result.usage.cacheWriteTokens,@@ -290,6 +293,8 @@ function actionUsage(result: RuntimeActionResult): InstrumentationUsage | undefiinputTokens: result.usage.inputTokens,outputTokens: result.usage.outputTokens,};+ if (result.usage.costUsd !== undefined) usage.costUsd = result.usage.costUsd;+ return usage;}function actionName(action: RuntimeActionRequest): string {diff --git a/packages/eve/src/tracing/agent-action-instrumentation.ts b/packages/eve/src/tracing/agent-action-instrumentation.tsindex 2113e21b4..9d6c28b92 100644--- a/packages/eve/src/tracing/agent-action-instrumentation.ts+++ b/packages/eve/src/tracing/agent-action-instrumentation.ts@@ -238,6 +238,9 @@ export function createAgentActionInstrumentation(input: {): void {const span = startSpan(state);span.setAttribute("agent.action.outcome", event.outcome);+ if (event.usage !== undefined) {+ setAgentUsage(span, event.usage);+ }if (event.type === "action.failed") {if (event.errorCode !== undefined) {span.setAttribute("agent.action.error.code", event.errorCode);@@ -246,9 +249,6 @@ export function createAgentActionInstrumentation(input: {} else if (event.output.type === "error") {recordActionError(span, event.output.error);} else {- if (event.usage !== undefined) {- setAgentUsage(span, event.usage);- }if (input.recordOutputs && !isAgentInvocation(state.kind)) {const result = contentAttribute(event.output.output);if (result !== undefined) span.setAttribute("gen_ai.tool.call.result", result);diff --git a/packages/eve/src/tracing/agent-invocation-coordinator.test.ts b/packages/eve/src/tracing/agent-invocation-coordinator.test.tsindex e9f72aca8..f3f7ca893 100644--- a/packages/eve/src/tracing/agent-invocation-coordinator.test.ts+++ b/packages/eve/src/tracing/agent-invocation-coordinator.test.ts@@ -194,6 +194,7 @@ describe("agent invocation trace coordinator", () => {usageDelta: {cacheReadTokens: 1,cacheWriteTokens: 2,+ costUsd: 0.012,inputTokens: 3,outputTokens: 4,},@@ -210,7 +211,7 @@ describe("agent invocation trace coordinator", () => {terminal: {acceptedAtMs: 3,outcome: "completed",- usage: { inputTokens: 3, outputTokens: 4 },+ usage: { costUsd: 0.012, inputTokens: 3, outputTokens: 4 },},});expect(diff --git a/packages/eve/src/tracing/agent-invocation-terminal.ts b/packages/eve/src/tracing/agent-invocation-terminal.tsindex 25312792e..77f22c1c2 100644--- a/packages/eve/src/tracing/agent-invocation-terminal.ts+++ b/packages/eve/src/tracing/agent-invocation-terminal.ts@@ -76,6 +76,7 @@ export function settleAgentInvocationTrace(input: {? "cancelled": "failed",usage: {+ costUsd: usage.costUsd,inputTokenDetails: {cacheReadTokens: usage.cacheReadTokens,cacheWriteTokens: usage.cacheWriteTokens,diff --git a/packages/eve/src/tracing/agent-otel-provider.test.ts b/packages/eve/src/tracing/agent-otel-provider.test.tsindex 70752a202..af31911fb 100644--- a/packages/eve/src/tracing/agent-otel-provider.test.ts+++ b/packages/eve/src/tracing/agent-otel-provider.test.ts@@ -2802,6 +2802,7 @@ describe("createAgentOtelInstrumentation", () => {const runtime = createRuntime();await emitAttempt({actionUsage: {+ costUsd: 0.012,inputTokenDetails: { cacheReadTokens: 3, cacheWriteTokens: 4 },inputTokens: 10,outputTokens: 5,@@ -2821,6 +2822,7 @@ describe("createAgentOtelInstrumentation", () => {"agent.action.kind": "subagent-call","agent.action.name": "weather","agent.action.outcome": "completed",+ "agent.usage.cost_usd": 0.012,"agent.usage.cache_read_tokens": 3,"agent.usage.cache_write_tokens": 4,"agent.usage.input_tokens": 10,diff --git a/packages/eve/src/tracing/agent-otel-usage.ts b/packages/eve/src/tracing/agent-otel-usage.tsindex 55c732f7b..7d4248f02 100644--- a/packages/eve/src/tracing/agent-otel-usage.ts+++ b/packages/eve/src/tracing/agent-otel-usage.ts@@ -6,6 +6,9 @@ import { AGENT_USAGE_ATTRIBUTES } from "#tracing/agent-span-contract.js";/** Applies eve's structural token usage attributes to an agent span. */export function setAgentUsage(span: Span, usage: InstrumentationUsage): void {+ if (usage.costUsd !== undefined) {+ span.setAttribute(AGENT_USAGE_ATTRIBUTES.costUsd, usage.costUsd);+ }if (usage.inputTokens !== undefined) {span.setAttribute(AGENT_USAGE_ATTRIBUTES.inputTokens, usage.inputTokens);}diff --git a/packages/eve/src/tracing/agent-span-contract.ts b/packages/eve/src/tracing/agent-span-contract.tsindex 7cbd133b9..311abc8c2 100644--- a/packages/eve/src/tracing/agent-span-contract.ts+++ b/packages/eve/src/tracing/agent-span-contract.ts@@ -8,6 +8,7 @@ export const AGENT_SPAN_NAMES = {} as const;export const AGENT_USAGE_ATTRIBUTES = {+ costUsd: "agent.usage.cost_usd",inputTokens: "agent.usage.input_tokens",outputTokens: "agent.usage.output_tokens",cacheReadTokens: "agent.usage.cache_read_tokens",