Repository navigation
fix(server-utils): Sum Vercel AI reasoning tokens onto invoke_agent spans #26904
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c37d6cd
fix(server-utils): Sum Vercel AI reasoning tokens onto invoke_agent s…
sentry-junior[bot] b002849
fix(server-utils): Sum Vercel AI parent reasoning per step
sentry-junior[bot] 37721f5
ref(server-utils): Derive Vercel AI token counts once per model call
sentry-junior[bot] 40cb76a
ref(server-utils): Read Vercel AI parent usage from steps only
sentry-junior[bot] 4fe2513
test(node): Allow unordered envelopes in Vercel AI reasoning token tests
sentry-junior[bot] 7fa18ee
Merge branch 'develop' into fix/vercel-ai-parent-reasoning-tokens
RulaKhaled a49e868
test(server-utils): Drop duplicate v4 Google total test
sentry-junior[bot] 370368c
Merge branch 'develop' into fix/vercel-ai-parent-reasoning-tokens
RulaKhaled 363f568
test(node): Use conventions op constants in Vercel AI reasoning token…
sentry-junior[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
dev-packages/node-integration-tests/suites/tracing/vercelai/v5/scenario-reasoning-tokens.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { generateText, simulateReadableStream, stepCountIs, streamText, tool } from 'ai'; | ||
| import { MockLanguageModelV2 } from 'ai/test'; | ||
| import { z } from 'zod'; | ||
|
|
||
| // `@ai-sdk/google` 2.x reports candidate-only `outputTokens` and a separate `reasoningTokens`, with the | ||
| // raw counts in `providerMetadata`. Two steps reason 80 and 100 tokens. | ||
| const STEPS = [ | ||
| { candidates: 20, thoughts: 80, finishReason: 'tool-calls' }, | ||
| { candidates: 20, thoughts: 100, finishReason: 'stop' }, | ||
| ]; | ||
|
|
||
| function usage({ candidates, thoughts }) { | ||
| return { | ||
| inputTokens: 10, | ||
| outputTokens: candidates, | ||
| totalTokens: 10 + candidates + thoughts, | ||
| reasoningTokens: thoughts, | ||
| }; | ||
| } | ||
|
|
||
| function providerMetadata({ candidates, thoughts }) { | ||
| return { | ||
| google: { | ||
| usageMetadata: { | ||
| promptTokenCount: 10, | ||
| candidatesTokenCount: candidates, | ||
| thoughtsTokenCount: thoughts, | ||
| totalTokenCount: 10 + candidates + thoughts, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const tools = { | ||
| getWeather: tool({ | ||
| description: 'Get the current weather for a location', | ||
| inputSchema: z.object({ location: z.string() }), | ||
| execute: async ({ location }) => `Weather in ${location}: Sunny, 72°F`, | ||
| }), | ||
| }; | ||
|
|
||
| const toolCall = { | ||
| type: 'tool-call', | ||
| toolCallId: 'call-1', | ||
| toolName: 'getWeather', | ||
| input: JSON.stringify({ location: 'San Francisco' }), | ||
| }; | ||
|
|
||
| async function run() { | ||
| await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
| let generateCalls = 0; | ||
| await generateText({ | ||
| model: new MockLanguageModelV2({ | ||
| doGenerate: async () => { | ||
| const step = STEPS[generateCalls++]; | ||
| return { | ||
| finishReason: step.finishReason, | ||
| usage: usage(step), | ||
| content: generateCalls === 1 ? [toolCall] : [{ type: 'text', text: 'Sunny, 72°F.' }], | ||
| warnings: [], | ||
| providerMetadata: providerMetadata(step), | ||
| }; | ||
| }, | ||
| }), | ||
| tools, | ||
| stopWhen: stepCountIs(5), | ||
| prompt: 'What is the weather in San Francisco?', | ||
| }); | ||
|
|
||
| let streamCalls = 0; | ||
| const result = streamText({ | ||
| model: new MockLanguageModelV2({ | ||
| doStream: async () => { | ||
| const step = STEPS[streamCalls++]; | ||
| const content = | ||
| streamCalls === 1 | ||
| ? [toolCall] | ||
| : [ | ||
| { type: 'text-start', id: '0' }, | ||
| { type: 'text-delta', id: '0', delta: 'Sunny, 72°F.' }, | ||
| { type: 'text-end', id: '0' }, | ||
| ]; | ||
| return { | ||
| stream: simulateReadableStream({ | ||
| chunks: [ | ||
| { type: 'stream-start', warnings: [] }, | ||
| ...content, | ||
| { | ||
| type: 'finish', | ||
| finishReason: step.finishReason, | ||
| usage: usage(step), | ||
| providerMetadata: providerMetadata(step), | ||
| }, | ||
| ], | ||
| }), | ||
| }; | ||
| }, | ||
| }), | ||
| tools, | ||
| stopWhen: stepCountIs(5), | ||
| prompt: 'What is the weather in San Francisco?', | ||
| }); | ||
| for await (const _part of result.fullStream) { | ||
| void _part; | ||
| } | ||
|
|
||
| // `@ai-sdk/openai` 2.x reports reasoning only in the usage, inside an output that includes it. | ||
| await generateText({ | ||
| model: new MockLanguageModelV2({ | ||
| doGenerate: async () => ({ | ||
| finishReason: 'stop', | ||
| usage: { inputTokens: 10, outputTokens: 50, totalTokens: 60, reasoningTokens: 30 }, | ||
| content: [{ type: 'text', text: 'Sunny.' }], | ||
| warnings: [], | ||
| providerMetadata: { openai: {} }, | ||
| }), | ||
| }), | ||
| prompt: 'What is the weather in San Francisco?', | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...ckages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-reasoning-tokens.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { generateText, stepCountIs, streamText, tool } from 'ai'; | ||
| import { MockLanguageModelV3, simulateReadableStream } from 'ai/test'; | ||
| import { z } from 'zod'; | ||
|
|
||
| // Two Gemini steps that reason 80 and 100 tokens. `providerMetadata` describes one step, so only the | ||
| // SDK's usage can give the operation its 180. | ||
| const STEPS = [ | ||
| { candidates: 20, thoughts: 80, finishReason: { unified: 'tool-calls', raw: 'tool_calls' } }, | ||
| { candidates: 20, thoughts: 100, finishReason: { unified: 'stop', raw: 'STOP' } }, | ||
| ]; | ||
|
|
||
| function usage({ candidates, thoughts }) { | ||
| return { | ||
| inputTokens: { total: 10, noCache: 10, cacheRead: 0, cacheWrite: undefined }, | ||
| outputTokens: { total: candidates + thoughts, text: candidates, reasoning: thoughts }, | ||
| }; | ||
| } | ||
|
|
||
| function providerMetadata({ candidates, thoughts }) { | ||
| return { | ||
| google: { | ||
| usageMetadata: { | ||
| promptTokenCount: 10, | ||
| candidatesTokenCount: candidates, | ||
| thoughtsTokenCount: thoughts, | ||
| totalTokenCount: 10 + candidates + thoughts, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const tools = { | ||
| getWeather: tool({ | ||
| description: 'Get the current weather for a location', | ||
| inputSchema: z.object({ location: z.string() }), | ||
| execute: async ({ location }) => `Weather in ${location}: Sunny, 72°F`, | ||
| }), | ||
| }; | ||
|
|
||
| const toolCall = { | ||
| type: 'tool-call', | ||
| toolCallId: 'call-1', | ||
| toolName: 'getWeather', | ||
| input: JSON.stringify({ location: 'San Francisco' }), | ||
| }; | ||
|
|
||
| async function run() { | ||
| await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
| let generateCalls = 0; | ||
| await generateText({ | ||
| experimental_telemetry: { isEnabled: true }, | ||
| model: new MockLanguageModelV3({ | ||
| doGenerate: async () => { | ||
| const step = STEPS[generateCalls++]; | ||
| return { | ||
| finishReason: step.finishReason, | ||
| usage: usage(step), | ||
| content: generateCalls === 1 ? [toolCall] : [{ type: 'text', text: 'Sunny, 72°F.' }], | ||
| warnings: [], | ||
| providerMetadata: providerMetadata(step), | ||
| }; | ||
| }, | ||
| }), | ||
| tools, | ||
| stopWhen: stepCountIs(5), | ||
| prompt: 'What is the weather in San Francisco?', | ||
| }); | ||
|
|
||
| let streamCalls = 0; | ||
| const result = streamText({ | ||
| experimental_telemetry: { isEnabled: true }, | ||
| model: new MockLanguageModelV3({ | ||
| doStream: async () => { | ||
| const step = STEPS[streamCalls++]; | ||
| const content = | ||
| streamCalls === 1 | ||
| ? [toolCall] | ||
| : [ | ||
| { type: 'text-start', id: '0' }, | ||
| { type: 'text-delta', id: '0', delta: 'Sunny, 72°F.' }, | ||
| { type: 'text-end', id: '0' }, | ||
| ]; | ||
| return { | ||
| stream: simulateReadableStream({ | ||
| chunks: [ | ||
| { type: 'stream-start', warnings: [] }, | ||
| ...content, | ||
| { | ||
| type: 'finish', | ||
| finishReason: step.finishReason, | ||
| usage: usage(step), | ||
| providerMetadata: providerMetadata(step), | ||
| }, | ||
| ], | ||
| }), | ||
| }; | ||
| }, | ||
| }), | ||
| tools, | ||
| stopWhen: stepCountIs(5), | ||
| prompt: 'What is the weather in San Francisco?', | ||
| }); | ||
| for await (const _part of result.fullStream) { | ||
| void _part; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.