-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(core): Fix and add missing cache attributes in Vercel AI #17982
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,10 @@ import type { Client } from '../../client'; | |
| import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '../../semanticAttributes'; | ||
| import type { Event } from '../../types-hoist/event'; | ||
| import type { Span, SpanAttributes, SpanAttributeValue, SpanJSON, SpanOrigin } from '../../types-hoist/span'; | ||
| import { | ||
| GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE_ATTRIBUTE, | ||
| GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE, | ||
| } from '../ai/gen-ai-attributes'; | ||
| import { spanToJSON } from '../spanUtils'; | ||
| import { toolCallSpanMap } from './constants'; | ||
| import type { TokenSummary } from './types'; | ||
|
|
@@ -23,6 +27,7 @@ import { | |
| AI_TOOL_CALL_ID_ATTRIBUTE, | ||
| AI_TOOL_CALL_NAME_ATTRIBUTE, | ||
| AI_TOOL_CALL_RESULT_ATTRIBUTE, | ||
| AI_USAGE_CACHED_INPUT_TOKENS_ATTRIBUTE, | ||
| AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE, | ||
| AI_USAGE_PROMPT_TOKENS_ATTRIBUTE, | ||
| GEN_AI_RESPONSE_MODEL_ATTRIBUTE, | ||
|
|
@@ -107,6 +112,7 @@ function processEndedVercelAiSpan(span: SpanJSON): void { | |
|
|
||
| renameAttributeKey(attributes, AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE, GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE); | ||
| renameAttributeKey(attributes, AI_USAGE_PROMPT_TOKENS_ATTRIBUTE, GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE); | ||
| renameAttributeKey(attributes, AI_USAGE_CACHED_INPUT_TOKENS_ATTRIBUTE, GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE); | ||
|
|
||
| if ( | ||
| typeof attributes[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE] === 'number' && | ||
|
|
@@ -287,7 +293,7 @@ function addProviderMetadataToAttributes(attributes: SpanAttributes): void { | |
| if (providerMetadataObject.openai) { | ||
| setAttributeIfDefined( | ||
| attributes, | ||
| 'gen_ai.usage.input_tokens.cached', | ||
| GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE, | ||
| providerMetadataObject.openai.cachedPromptTokens, | ||
| ); | ||
| setAttributeIfDefined( | ||
|
|
@@ -309,35 +315,34 @@ function addProviderMetadataToAttributes(attributes: SpanAttributes): void { | |
| } | ||
|
|
||
| if (providerMetadataObject.anthropic) { | ||
| setAttributeIfDefined( | ||
| attributes, | ||
| 'gen_ai.usage.input_tokens.cached', | ||
| providerMetadataObject.anthropic.cacheReadInputTokens, | ||
| ); | ||
| setAttributeIfDefined( | ||
| attributes, | ||
| 'gen_ai.usage.input_tokens.cache_write', | ||
| providerMetadataObject.anthropic.cacheCreationInputTokens, | ||
| ); | ||
| const cachedInputTokens = | ||
| providerMetadataObject.anthropic.usage?.cache_read_input_tokens ?? | ||
| providerMetadataObject.anthropic.cacheCreationInputTokens; | ||
| setAttributeIfDefined(attributes, GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE, cachedInputTokens); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| const cacheWriteInputTokens = | ||
| providerMetadataObject.anthropic.usage?.cache_creation_input_tokens ?? | ||
| providerMetadataObject.anthropic.cacheCreationInputTokens; | ||
| setAttributeIfDefined(attributes, GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE_ATTRIBUTE, cacheWriteInputTokens); | ||
| } | ||
|
|
||
| if (providerMetadataObject.bedrock?.usage) { | ||
| setAttributeIfDefined( | ||
| attributes, | ||
| 'gen_ai.usage.input_tokens.cached', | ||
| GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE, | ||
| providerMetadataObject.bedrock.usage.cacheReadInputTokens, | ||
| ); | ||
| setAttributeIfDefined( | ||
| attributes, | ||
| 'gen_ai.usage.input_tokens.cache_write', | ||
| GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE_ATTRIBUTE, | ||
| providerMetadataObject.bedrock.usage.cacheWriteInputTokens, | ||
| ); | ||
| } | ||
|
|
||
| if (providerMetadataObject.deepseek) { | ||
| setAttributeIfDefined( | ||
| attributes, | ||
| 'gen_ai.usage.input_tokens.cached', | ||
| GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE, | ||
| providerMetadataObject.deepseek.promptCacheHitTokens, | ||
| ); | ||
| setAttributeIfDefined( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -288,6 +288,14 @@ export const AI_RESPONSE_PROVIDER_METADATA_ATTRIBUTE = 'ai.response.providerMeta | |
| */ | ||
| export const AI_SETTINGS_MAX_RETRIES_ATTRIBUTE = 'ai.settings.maxRetries'; | ||
|
|
||
| /** | ||
| * Basic LLM span information | ||
| * Multiple spans | ||
| * | ||
| * The number of cached input tokens that were used | ||
| * @see https://ai-sdk.dev/docs/ai-sdk-core/telemetry#basic-llm-span-information | ||
| */ | ||
| export const AI_USAGE_CACHED_INPUT_TOKENS_ATTRIBUTE = 'ai.usage.cachedInputTokens'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. l: I couldn't find the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| /** | ||
| * Basic LLM span information | ||
| * Multiple spans | ||
|
|
@@ -863,6 +871,21 @@ interface AnthropicProviderMetadata { | |
| * @see https://ai-sdk.dev/providers/ai-sdk-providers/anthropic#cache-control | ||
| */ | ||
| cacheReadInputTokens?: number; | ||
|
|
||
| /** | ||
| * Usage metrics for the Anthropic model. | ||
| */ | ||
| usage?: { | ||
| input_tokens: number; | ||
| cache_creation_input_tokens?: number; | ||
| cache_read_input_tokens?: number; | ||
| cache_creation?: { | ||
| ephemeral_5m_input_tokens?: number; | ||
| ephemeral_1h_input_tokens?: number; | ||
| }; | ||
| output_tokens?: number; | ||
| service_tier?: string; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.