From 6359db70f7ae471ebb33672c2bf48d454e474502 Mon Sep 17 00:00:00 2001 From: Dvir Rezenman Date: Mon, 15 Jun 2026 16:06:53 +0300 Subject: [PATCH 1/2] set input cache tokens --- .../src/instrumentation.ts | 10 +++- .../recording.har | 58 +++++++++++++++++++ .../src/instrumentation.ts | 4 +- .../src/instrumentation.ts | 2 +- 4 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 packages/instrumentation-bedrock/recordings/Test-Anthropic-with-AWS-Bedrock-Instrumentation_3427560822/should-set-cache-tokens-in-span-for-Anthropic-messages-API-with-cached-tokens_3921807056/recording.har diff --git a/packages/instrumentation-anthropic/src/instrumentation.ts b/packages/instrumentation-anthropic/src/instrumentation.ts index 7012cae0c..bf2d247f9 100644 --- a/packages/instrumentation-anthropic/src/instrumentation.ts +++ b/packages/instrumentation-anthropic/src/instrumentation.ts @@ -533,13 +533,19 @@ export class AnthropicInstrumentation extends InstrumentationBase { span.setAttribute(ATTR_GEN_AI_USAGE_INPUT_TOKENS, totalInputTokens); // Cache token attributes (v1.40) - if (result.usage.cache_creation_input_tokens != null) { + if ( + result.usage.cache_creation_input_tokens !== null && + result.usage.cache_creation_input_tokens !== undefined + ) { span.setAttribute( ATTR_GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, result.usage.cache_creation_input_tokens, ); } - if (result.usage.cache_read_input_tokens != null) { + if ( + result.usage.cache_read_input_tokens !== null && + result.usage.cache_read_input_tokens !== undefined + ) { span.setAttribute( ATTR_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, result.usage.cache_read_input_tokens, diff --git a/packages/instrumentation-bedrock/recordings/Test-Anthropic-with-AWS-Bedrock-Instrumentation_3427560822/should-set-cache-tokens-in-span-for-Anthropic-messages-API-with-cached-tokens_3921807056/recording.har b/packages/instrumentation-bedrock/recordings/Test-Anthropic-with-AWS-Bedrock-Instrumentation_3427560822/should-set-cache-tokens-in-span-for-Anthropic-messages-API-with-cached-tokens_3921807056/recording.har new file mode 100644 index 000000000..ced26678e --- /dev/null +++ b/packages/instrumentation-bedrock/recordings/Test-Anthropic-with-AWS-Bedrock-Instrumentation_3427560822/should-set-cache-tokens-in-span-for-Anthropic-messages-API-with-cached-tokens_3921807056/recording.har @@ -0,0 +1,58 @@ +{ + "log": { + "_recordingName": "Test Anthropic with AWS Bedrock Instrumentation/should set cache tokens in span for Anthropic messages API with cached tokens", + "creator": { + "comment": "persister:fs", + "name": "Polly.JS", + "version": "6.0.6" + }, + "entries": [ + { + "_id": "bedrock_anthropic_cache_001", + "_order": 0, + "cache": {}, + "request": { + "bodySize": 120, + "cookies": [], + "headers": [], + "headersSize": 0, + "httpVersion": "HTTP/1.1", + "method": "POST", + "postData": { + "mimeType": "application/json", + "text": "{\"anthropic_version\": \"bedrock-2023-05-31\", \"max_tokens\": 300, \"messages\": [{\"role\": \"user\", \"content\": \"What are the 4 cardinal directions?\"}]}" + }, + "queryString": [], + "url": "https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-3-5-sonnet-20241022-v2-0/invoke" + }, + "response": { + "bodySize": 384, + "content": { + "mimeType": "application/json", + "size": 384, + "text": "{\"id\": \"msg_bedrock_cache_test_001\", \"type\": \"message\", \"role\": \"assistant\", \"content\": [{\"type\": \"text\", \"text\": \"The 4 cardinal directions are: North, South, East, and West.\"}], \"model\": \"claude-3-5-sonnet-20241022\", \"stop_reason\": \"end_turn\", \"stop_sequence\": null, \"usage\": {\"input_tokens\": 10, \"cache_creation_input_tokens\": 8, \"cache_read_input_tokens\": 5, \"output_tokens\": 15}}" + }, + "cookies": [], + "headers": [ + { + "name": "content-type", + "value": "application/json" + } + ], + "headersSize": 0, + "httpVersion": "HTTP/1.1", + "redirectURL": "", + "status": 200, + "statusText": "OK" + }, + "startedDateTime": "2025-01-01T00:00:00.000Z", + "time": 500, + "timings": { + "receive": 0, + "send": 0, + "wait": 500 + } + } + ] + } +} diff --git a/packages/instrumentation-bedrock/src/instrumentation.ts b/packages/instrumentation-bedrock/src/instrumentation.ts index 54668476e..0701e3876 100644 --- a/packages/instrumentation-bedrock/src/instrumentation.ts +++ b/packages/instrumentation-bedrock/src/instrumentation.ts @@ -703,8 +703,8 @@ export class BedrockInstrumentation extends InstrumentationBase { ? (() => { const inputTokens = usage["input_tokens"] || 0; const outputTokens = usage["output_tokens"] || 0; - const cacheRead = usage["cache_read_input_tokens"] || 0; - const cacheCreation = usage["cache_creation_input_tokens"] || 0; + const cacheRead = usage["cache_read_input_tokens"]; + const cacheCreation = usage["cache_creation_input_tokens"]; const totalInputTokens = inputTokens + cacheRead + cacheCreation; return { diff --git a/packages/instrumentation-google-generativeai/src/instrumentation.ts b/packages/instrumentation-google-generativeai/src/instrumentation.ts index a982da5d4..4a86916d1 100644 --- a/packages/instrumentation-google-generativeai/src/instrumentation.ts +++ b/packages/instrumentation-google-generativeai/src/instrumentation.ts @@ -495,7 +495,7 @@ export class GenAIInstrumentation extends InstrumentationBase { SpanAttributes.GEN_AI_USAGE_REASONING_TOKENS, reasoningTokens, ); - if (cachedTokens != null) + if (cachedTokens !== null && cachedTokens !== undefined) span.setAttribute( ATTR_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cachedTokens, From ce12fa371c468e9e1c6b9a8e93bf1f662c6b9b70 Mon Sep 17 00:00:00 2001 From: Dvir Rezenman Date: Thu, 18 Jun 2026 17:43:46 +0300 Subject: [PATCH 2/2] linting --- packages/instrumentation-bedrock/src/instrumentation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/instrumentation-bedrock/src/instrumentation.ts b/packages/instrumentation-bedrock/src/instrumentation.ts index 0701e3876..299de20bf 100644 --- a/packages/instrumentation-bedrock/src/instrumentation.ts +++ b/packages/instrumentation-bedrock/src/instrumentation.ts @@ -706,7 +706,7 @@ export class BedrockInstrumentation extends InstrumentationBase { const cacheRead = usage["cache_read_input_tokens"]; const cacheCreation = usage["cache_creation_input_tokens"]; const totalInputTokens = - inputTokens + cacheRead + cacheCreation; + inputTokens + (cacheRead ?? 0) + (cacheCreation ?? 0); return { [ATTR_GEN_AI_USAGE_INPUT_TOKENS]: totalInputTokens, [ATTR_GEN_AI_USAGE_OUTPUT_TOKENS]: usage["output_tokens"],