Skip to content

Commit 1faf69d

Browse files
authored
fix(instrumentations): add cache token span attributes for OpenAI, Bedrock, Together AI, and Vertex AI (#1026)
1 parent 3867c36 commit 1faf69d

4 files changed

Lines changed: 70 additions & 6 deletions

File tree

packages/instrumentation-anthropic/src/instrumentation.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,13 +533,19 @@ export class AnthropicInstrumentation extends InstrumentationBase {
533533
span.setAttribute(ATTR_GEN_AI_USAGE_INPUT_TOKENS, totalInputTokens);
534534

535535
// Cache token attributes (v1.40)
536-
if (result.usage.cache_creation_input_tokens != null) {
536+
if (
537+
result.usage.cache_creation_input_tokens !== null &&
538+
result.usage.cache_creation_input_tokens !== undefined
539+
) {
537540
span.setAttribute(
538541
ATTR_GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
539542
result.usage.cache_creation_input_tokens,
540543
);
541544
}
542-
if (result.usage.cache_read_input_tokens != null) {
545+
if (
546+
result.usage.cache_read_input_tokens !== null &&
547+
result.usage.cache_read_input_tokens !== undefined
548+
) {
543549
span.setAttribute(
544550
ATTR_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
545551
result.usage.cache_read_input_tokens,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"log": {
3+
"_recordingName": "Test Anthropic with AWS Bedrock Instrumentation/should set cache tokens in span for Anthropic messages API with cached tokens",
4+
"creator": {
5+
"comment": "persister:fs",
6+
"name": "Polly.JS",
7+
"version": "6.0.6"
8+
},
9+
"entries": [
10+
{
11+
"_id": "bedrock_anthropic_cache_001",
12+
"_order": 0,
13+
"cache": {},
14+
"request": {
15+
"bodySize": 120,
16+
"cookies": [],
17+
"headers": [],
18+
"headersSize": 0,
19+
"httpVersion": "HTTP/1.1",
20+
"method": "POST",
21+
"postData": {
22+
"mimeType": "application/json",
23+
"text": "{\"anthropic_version\": \"bedrock-2023-05-31\", \"max_tokens\": 300, \"messages\": [{\"role\": \"user\", \"content\": \"What are the 4 cardinal directions?\"}]}"
24+
},
25+
"queryString": [],
26+
"url": "https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-3-5-sonnet-20241022-v2-0/invoke"
27+
},
28+
"response": {
29+
"bodySize": 384,
30+
"content": {
31+
"mimeType": "application/json",
32+
"size": 384,
33+
"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}}"
34+
},
35+
"cookies": [],
36+
"headers": [
37+
{
38+
"name": "content-type",
39+
"value": "application/json"
40+
}
41+
],
42+
"headersSize": 0,
43+
"httpVersion": "HTTP/1.1",
44+
"redirectURL": "",
45+
"status": 200,
46+
"statusText": "OK"
47+
},
48+
"startedDateTime": "2025-01-01T00:00:00.000Z",
49+
"time": 500,
50+
"timings": {
51+
"receive": 0,
52+
"send": 0,
53+
"wait": 500
54+
}
55+
}
56+
]
57+
}
58+
}

packages/instrumentation-bedrock/src/instrumentation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,10 +703,10 @@ export class BedrockInstrumentation extends InstrumentationBase {
703703
? (() => {
704704
const inputTokens = usage["input_tokens"] || 0;
705705
const outputTokens = usage["output_tokens"] || 0;
706-
const cacheRead = usage["cache_read_input_tokens"] || 0;
707-
const cacheCreation = usage["cache_creation_input_tokens"] || 0;
706+
const cacheRead = usage["cache_read_input_tokens"];
707+
const cacheCreation = usage["cache_creation_input_tokens"];
708708
const totalInputTokens =
709-
inputTokens + cacheRead + cacheCreation;
709+
inputTokens + (cacheRead ?? 0) + (cacheCreation ?? 0);
710710
return {
711711
[ATTR_GEN_AI_USAGE_INPUT_TOKENS]: totalInputTokens,
712712
[ATTR_GEN_AI_USAGE_OUTPUT_TOKENS]: usage["output_tokens"],

packages/instrumentation-google-generativeai/src/instrumentation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ export class GenAIInstrumentation extends InstrumentationBase {
495495
SpanAttributes.GEN_AI_USAGE_REASONING_TOKENS,
496496
reasoningTokens,
497497
);
498-
if (cachedTokens != null)
498+
if (cachedTokens !== null && cachedTokens !== undefined)
499499
span.setAttribute(
500500
ATTR_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
501501
cachedTokens,

0 commit comments

Comments
 (0)