Skip to content
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 @@ -23,6 +23,7 @@ async function run() {
reasoningTokens: 7,
responseId: 'resp_abc123',
},
gateway: { cost: '0.0000074' },
},
}),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Event } from '@sentry/node';
import { afterAll, expect } from 'vitest';
import {
GEN_AI_CONVERSATION_ID,
GEN_AI_COST_TOTAL_TOKENS,
GEN_AI_EMBEDDINGS_INPUT,
GEN_AI_INPUT_MESSAGES,
GEN_AI_OPERATION_NAME,
Expand Down Expand Up @@ -786,9 +787,12 @@ export function registerVercelAiGenerationTests(version: '6' | '7', vercelAiVers
expect(generateContent.attributes[GEN_AI_USAGE_REASONING_OUTPUT_TOKENS]?.value).toBe(7);
// The per-response `responseId` is not a conversation id and must not be recorded as one.
expect(generateContent.attributes[GEN_AI_CONVERSATION_ID]).toBeUndefined();
expect(generateContent.attributes[GEN_AI_COST_TOTAL_TOKENS]?.value).toBe(0.0000074);

const invokeAgent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent')!;
expect(invokeAgent).toBeDefined();
// `providerMetadata` holds only the last step's cost, which the model-call span already carries.
expect(invokeAgent.attributes[GEN_AI_COST_TOTAL_TOKENS]).toBeUndefined();

// The system prompt is supplied via the v7-only `instructions` option. Only the channel
// instrumentation surfaces it (as `gen_ai.system_instructions`); v6 has no such option.
Expand Down
7 changes: 7 additions & 0 deletions packages/server-utils/src/ai/vercel-ai/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { SpanAttributeValue } from '@sentry/core';
import {
GEN_AI_COST_TOTAL_TOKENS,
GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
GEN_AI_USAGE_OUTPUT_TOKENS,
Expand Down Expand Up @@ -82,6 +83,12 @@ export function getProviderMetadataAttributes(providerMetadata: unknown): Record
setAttributeIfDefined(attributes, GEN_AI_USAGE_REASONING_OUTPUT_TOKENS, googleUsage.thoughtsTokenCount);
}

// Vercel AI Gateway reports the billed USD cost of the call, as a decimal string.
const gatewayCost = Number(metadata.gateway?.cost);
if (metadata.gateway?.cost != null && Number.isFinite(gatewayCost)) {
attributes[GEN_AI_COST_TOTAL_TOKENS] = gatewayCost;
}

return attributes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,5 @@ export interface ProviderMetadata {
vertex?: GoogleGenerativeAIProviderMetadata; // v6: Google Vertex uses 'vertex' key instead of 'google'
deepseek?: DeepSeekProviderMetadata;
perplexity?: PerplexityProviderMetadata;
gateway?: { cost?: string | number };
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable max-lines */
import {
GEN_AI_CONVERSATION_ID,
GEN_AI_COST_TOTAL_TOKENS,
GEN_AI_EMBEDDINGS_INPUT,
GEN_AI_FUNCTION_ID,
GEN_AI_INPUT_MESSAGES,
Expand Down Expand Up @@ -130,6 +131,16 @@ const ROOT_OPERATION_TYPES = new Set<ChannelEventType>([
'rerank',
]);

// Operations whose `providerMetadata` cost covers only one of their provider calls: the last step of a
// text or object operation, whose cost the `generate_content` spans carry, or one `embedMany` chunk.
const PARTIAL_COST_OPERATION_TYPES = new Set<ChannelEventType>([
'generateText',
'streamText',
'streamObject',
'generateObject',
'embedMany',
]);

/** Drop the per-operation `callId` maps once the owning top-level operation settles (success or error). */
export function clearOperationId(data: VercelAiChannelMessage): void {
if (!ROOT_OPERATION_TYPES.has(data.type)) {
Expand Down Expand Up @@ -201,6 +212,10 @@ function dropLastStepOnlyUsage(providerAttributes: Record<string, number | strin
// oxlint-disable-next-line typescript/no-dynamic-delete
delete providerAttributes[GEN_AI_USAGE_REASONING_OUTPUT_TOKENS];
}
if (PARTIAL_COST_OPERATION_TYPES.has(type)) {
// oxlint-disable-next-line typescript/no-dynamic-delete
delete providerAttributes[GEN_AI_COST_TOTAL_TOKENS];
}
}

/** Record tool name → description from an event's `tools`, so tool spans can backfill the description. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { GEN_AI_COST_TOTAL_TOKENS } from '@sentry/conventions/attributes';
import { getMainCarrier, setCurrentClient, spanToStaticSpanJSON } from '@sentry/core';
import type { Span } from '@sentry/core';
import { createSpanFromMessage, enrichSpanOnEnd } from '../../../src/integrations/vercel-ai/vercel-ai-dc-subscriber';
import { getDefaultTestClientOptions, TestClient } from '../../mocks/client';

describe('Vercel AI Gateway cost', () => {
beforeEach(() => {
getMainCarrier().__SENTRY__ = undefined;
});

afterEach(() => {
getMainCarrier().__SENTRY__ = undefined;
});

function runSpan(type: string, cost: unknown): Record<string, unknown> {
const client = new TestClient(
getDefaultTestClientOptions({ dsn: 'https://public@dsn.ingest.sentry.io/1337', tracesSampleRate: 1 }),
);
setCurrentClient(client);
client.init();
const endedSpans: Span[] = [];
client.on('spanEnd', span => endedSpans.push(span));

const message = {
type,
event: {},
result: { providerMetadata: { gateway: { cost } } },
} as Parameters<typeof createSpanFromMessage>[0];
const span = createSpanFromMessage(message, {} as Parameters<typeof createSpanFromMessage>[1]);
enrichSpanOnEnd(span!, message, {} as Parameters<typeof enrichSpanOnEnd>[2]);
span?.end();
return spanToStaticSpanJSON(endedSpans[0]!).data ?? {};
}

it.each(['languageModelCall', 'embed', 'rerank', 'experimental_decide'])(
'records the gateway cost on `%s` spans',
type => {
expect(runSpan(type, '0.0000074')[GEN_AI_COST_TOTAL_TOKENS]).toBe(0.0000074);
},
);

it.each(['generateText', 'streamText', 'embedMany'])('leaves the partial gateway cost off `%s` spans', type => {
expect(runSpan(type, '0.0000074')).not.toHaveProperty(GEN_AI_COST_TOTAL_TOKENS);
});

it('accepts a numeric cost and ignores one that is not a number', () => {
expect(runSpan('languageModelCall', 0.5)[GEN_AI_COST_TOTAL_TOKENS]).toBe(0.5);
expect(runSpan('languageModelCall', 'n/a')).not.toHaveProperty(GEN_AI_COST_TOTAL_TOKENS);
});
});
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
Loading