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
4 changes: 3 additions & 1 deletion packages/server-utils/src/ai/google-genai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ export function addResponseAttributes(span: Span, response: GoogleGenAIResponse,
const hasOutput = typeof usage.candidatesTokenCount === 'number' || typeof usage.thoughtsTokenCount === 'number';
span.setAttributes(
getTokenUsageAttributes(
usage.promptTokenCount,
typeof usage.promptTokenCount === 'number'
? usage.promptTokenCount + (usage.toolUsePromptTokenCount ?? 0)
: undefined,
hasOutput ? (usage.candidatesTokenCount ?? 0) + (usage.thoughtsTokenCount ?? 0) : undefined,
undefined,
usage.cachedContentTokenCount,
Expand Down
5 changes: 4 additions & 1 deletion packages/server-utils/src/ai/google-genai/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ interface StreamingState {
responseId?: string;
/** The model name. */
responseModel?: string;
/** Number of prompt/input tokens used. */
/** Number of prompt/input tokens used. Tool-use prompt tokens are added when the stream ends. */
promptTokens?: number;
toolUsePromptTokens?: number;
/** Number of completion/output tokens used. */
completionTokens?: number;
candidateTokens?: number;
Expand Down Expand Up @@ -63,6 +64,7 @@ function handleResponseMetadata(chunk: GoogleGenAIResponse, state: StreamingStat
const usage = chunk.usageMetadata;
if (usage) {
if (typeof usage.promptTokenCount === 'number') state.promptTokens = usage.promptTokenCount;
if (typeof usage.toolUsePromptTokenCount === 'number') state.toolUsePromptTokens = usage.toolUsePromptTokenCount;
if (typeof usage.candidatesTokenCount === 'number') state.candidateTokens = usage.candidatesTokenCount;
if (typeof usage.thoughtsTokenCount === 'number') state.reasoningOutputTokens = usage.thoughtsTokenCount;
if (state.candidateTokens !== undefined || state.reasoningOutputTokens !== undefined) {
Expand Down Expand Up @@ -139,6 +141,7 @@ export async function* instrumentStream(
if (recordOutputs) {
setOutputMessagesAttribute(span, state.outputParts);
}
if (typeof state.promptTokens === 'number') state.promptTokens += state.toolUsePromptTokens ?? 0;
endStreamSpan(span, state, recordOutputs);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { describe, expect, it } from 'vitest';
import type { Span } from '@sentry/core';
import { GEN_AI_RESPONSE_TOOL_CALLS } from '@sentry/conventions/attributes';
import {
GEN_AI_RESPONSE_TOOL_CALLS,
GEN_AI_USAGE_INPUT_TOKENS,
GEN_AI_USAGE_OUTPUT_TOKENS,
GEN_AI_USAGE_TOTAL_TOKENS,
} from '@sentry/conventions/attributes';
import { addResponseAttributes } from '../../../../src/ai/google-genai/index';
import { instrumentStream } from '../../../../src/ai/google-genai/streaming';
import type { ContentPart, GoogleGenAIResponse } from '../../../../src/ai/google-genai/types';
Expand Down Expand Up @@ -126,3 +131,41 @@ describe('instrumentStream (google-genai tool calls)', () => {
expect(toolCalls).toEqual([functionCall]);
});
});

describe('google-genai tool-use prompt tokens', () => {
// Google Search, URL context and code execution results are fed back to the model and counted in
// `toolUsePromptTokenCount`, apart from `promptTokenCount`; `totalTokenCount` includes both.
const usageMetadata = {
promptTokenCount: 100,
toolUsePromptTokenCount: 40,
candidatesTokenCount: 20,
totalTokenCount: 160,
};

it('counts them as input tokens on a response', () => {
const { span, attributes } = createMockSpan();

addResponseAttributes(span, { usageMetadata } as GoogleGenAIResponse, false);

expect(attributes[GEN_AI_USAGE_INPUT_TOKENS]).toBe(140);
expect(attributes[GEN_AI_USAGE_OUTPUT_TOKENS]).toBe(20);
expect(attributes[GEN_AI_USAGE_TOTAL_TOKENS]).toBe(160);
});

it('counts them as input tokens on a stream, also when a later chunk only repeats one of the counts', async () => {
const { span, attributes } = createMockSpan();

const chunks = [
chunkWithParts([{ text: 'Hi' }], { usageMetadata: { promptTokenCount: 100, toolUsePromptTokenCount: 40 } }),
chunkWithParts([{ text: ' there' }], {
usageMetadata: { promptTokenCount: 100, candidatesTokenCount: 20, totalTokenCount: 160 },
}),
];

await drain(instrumentStream(streamOf(chunks), span, false));

expect(attributes[GEN_AI_USAGE_INPUT_TOKENS]).toBe(140);
expect(attributes[GEN_AI_USAGE_OUTPUT_TOKENS]).toBe(20);
expect(attributes[GEN_AI_USAGE_TOTAL_TOKENS]).toBe(160);
});
});
Loading