Skip to content

Commit 29c0f99

Browse files
committed
CR Comments
1 parent a6da699 commit 29c0f99

8 files changed

Lines changed: 268 additions & 50 deletions

File tree

packages/instrumentation-bedrock/tests/anthropic.test.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ describe("Test Anthropic with AWS Bedrock Instrumentation", () => {
140140
id: "msg_cache_test",
141141
type: "message",
142142
role: "assistant",
143-
content: [
144-
{ type: "text", text: "North, South, East, West." },
145-
],
143+
content: [{ type: "text", text: "North, South, East, West." }],
146144
model: modelId,
147145
stop_reason: "end_turn",
148146
stop_sequence: null,
@@ -177,14 +175,8 @@ describe("Test Anthropic with AWS Bedrock Instrumentation", () => {
177175
// Per OTel GenAI semconv (subset semantics), input_tokens includes
178176
// cache_read + cache_creation. Raw response: input=10, cache_read=5,
179177
// cache_creation=8 → summed input_tokens = 23.
180-
assert.strictEqual(
181-
attributes[ATTR_GEN_AI_USAGE_INPUT_TOKENS],
182-
23,
183-
);
184-
assert.strictEqual(
185-
attributes[ATTR_GEN_AI_USAGE_OUTPUT_TOKENS],
186-
7,
187-
);
178+
assert.strictEqual(attributes[ATTR_GEN_AI_USAGE_INPUT_TOKENS], 23);
179+
assert.strictEqual(attributes[ATTR_GEN_AI_USAGE_OUTPUT_TOKENS], 7);
188180
assert.strictEqual(
189181
attributes[SpanAttributes.GEN_AI_USAGE_TOTAL_TOKENS],
190182
30,

packages/instrumentation-bedrock/tests/cache-token-fold-in.test.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ describe("Bedrock Anthropic cache token fold-in semantics", () => {
6060
1250,
6161
"total_tokens should equal summed input (1200) + output (50)",
6262
);
63-
assert.strictEqual(
64-
attrs[ATTR_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS],
65-
900,
66-
);
63+
assert.strictEqual(attrs[ATTR_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS], 900);
6764
assert.strictEqual(
6865
attrs[ATTR_GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS],
6966
200,
@@ -77,14 +74,8 @@ describe("Bedrock Anthropic cache token fold-in semantics", () => {
7774
cache_read_input_tokens: 900,
7875
});
7976
assert.strictEqual(attrs[ATTR_GEN_AI_USAGE_INPUT_TOKENS], 1000);
80-
assert.strictEqual(
81-
attrs[SpanAttributes.GEN_AI_USAGE_TOTAL_TOKENS],
82-
1050,
83-
);
84-
assert.strictEqual(
85-
attrs[ATTR_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS],
86-
900,
87-
);
77+
assert.strictEqual(attrs[SpanAttributes.GEN_AI_USAGE_TOTAL_TOKENS], 1050);
78+
assert.strictEqual(attrs[ATTR_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS], 900);
8879
assert.strictEqual(
8980
attrs[ATTR_GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS],
9081
undefined,
@@ -98,10 +89,7 @@ describe("Bedrock Anthropic cache token fold-in semantics", () => {
9889
});
9990
assert.strictEqual(attrs[ATTR_GEN_AI_USAGE_INPUT_TOKENS], 100);
10091
assert.strictEqual(attrs[ATTR_GEN_AI_USAGE_OUTPUT_TOKENS], 50);
101-
assert.strictEqual(
102-
attrs[SpanAttributes.GEN_AI_USAGE_TOTAL_TOKENS],
103-
150,
104-
);
92+
assert.strictEqual(attrs[SpanAttributes.GEN_AI_USAGE_TOTAL_TOKENS], 150);
10593
assert.strictEqual(
10694
attrs[ATTR_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS],
10795
undefined,

packages/instrumentation-langchain/src/callback_handler.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,7 @@ export class TraceloopCallbackHandler extends BaseCallbackHandler {
582582

583583
// langchain-core's UsageMetadata lives on each AIMessage (generation.message).
584584
// Returns the first non-empty one found across all generations.
585-
private extractUsageMetadataFromGenerations(
586-
output: LLMResult,
587-
): {
585+
private extractUsageMetadataFromGenerations(output: LLMResult): {
588586
input_tokens?: number;
589587
output_tokens?: number;
590588
total_tokens?: number;

packages/instrumentation-langchain/test/cache-token-fold-in.test.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,9 @@ describe("LangChain cache token emission from usage_metadata", () => {
6060

6161
const runChat = async (output: LLMResult): Promise<void> => {
6262
const runId = `run-${Math.random()}`;
63-
await handler.handleChatModelStart(
64-
serializedLLM,
65-
[[]],
66-
runId,
67-
undefined,
68-
{ invocation_params: { model: "claude-3-5-sonnet" } },
69-
);
63+
await handler.handleChatModelStart(serializedLLM, [[]], runId, undefined, {
64+
invocation_params: { model: "claude-3-5-sonnet" },
65+
});
7066
await handler.handleLLMEnd(output, runId);
7167
};
7268

@@ -172,13 +168,9 @@ describe("LangChain cache token emission from usage_metadata", () => {
172168

173169
it("falls back to llmOutput.tokenUsage when usage_metadata is absent (backwards compat)", async () => {
174170
const runId = `run-${Math.random()}`;
175-
await handler.handleChatModelStart(
176-
serializedLLM,
177-
[[]],
178-
runId,
179-
undefined,
180-
{ invocation_params: { model: "gpt-4" } },
181-
);
171+
await handler.handleChatModelStart(serializedLLM, [[]], runId, undefined, {
172+
invocation_params: { model: "gpt-4" },
173+
});
182174
await handler.handleLLMEnd(
183175
{
184176
generations: [

packages/instrumentation-openai/src/instrumentation.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,8 +969,7 @@ export class OpenAIInstrumentation extends InstrumentationBase {
969969
totalTokens,
970970
);
971971
}
972-
const cachedTokens =
973-
result.usage.input_tokens_details?.cached_tokens;
972+
const cachedTokens = result.usage.input_tokens_details?.cached_tokens;
974973
if (cachedTokens) {
975974
span.setAttribute(
976975
ATTR_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
{
2+
"log": {
3+
"_recordingName": "Test OpenAI instrumentation/should set cache read input tokens in span for responses with cached tokens",
4+
"creator": {
5+
"comment": "persister:fs",
6+
"name": "Polly.JS",
7+
"version": "6.0.6"
8+
},
9+
"entries": [
10+
{
11+
"_id": "c94800faf5cc7c077d6bd96c5e4196b9",
12+
"_order": 0,
13+
"cache": {},
14+
"request": {
15+
"bodySize": 68,
16+
"cookies": [],
17+
"headers": [
18+
{
19+
"_fromType": "array",
20+
"name": "accept",
21+
"value": "application/json"
22+
},
23+
{
24+
"_fromType": "array",
25+
"name": "content-type",
26+
"value": "application/json"
27+
},
28+
{
29+
"_fromType": "array",
30+
"name": "user-agent",
31+
"value": "OpenAI/JS 6.32.0"
32+
},
33+
{
34+
"_fromType": "array",
35+
"name": "x-stainless-arch",
36+
"value": "arm64"
37+
},
38+
{
39+
"_fromType": "array",
40+
"name": "x-stainless-lang",
41+
"value": "js"
42+
},
43+
{
44+
"_fromType": "array",
45+
"name": "x-stainless-os",
46+
"value": "MacOS"
47+
},
48+
{
49+
"_fromType": "array",
50+
"name": "x-stainless-package-version",
51+
"value": "6.32.0"
52+
},
53+
{
54+
"_fromType": "array",
55+
"name": "x-stainless-retry-count",
56+
"value": "0"
57+
},
58+
{
59+
"_fromType": "array",
60+
"name": "x-stainless-runtime",
61+
"value": "node"
62+
},
63+
{
64+
"_fromType": "array",
65+
"name": "x-stainless-runtime-version",
66+
"value": "v22.22.1"
67+
},
68+
{
69+
"_fromType": "array",
70+
"name": "content-length",
71+
"value": "68"
72+
},
73+
{
74+
"_fromType": "array",
75+
"name": "accept-encoding",
76+
"value": "gzip,deflate"
77+
},
78+
{
79+
"name": "host",
80+
"value": "api.openai.com"
81+
}
82+
],
83+
"headersSize": 603,
84+
"httpVersion": "HTTP/1.1",
85+
"method": "POST",
86+
"postData": {
87+
"mimeType": "application/json",
88+
"params": [],
89+
"text": "{\"model\":\"gpt-4o-mini\",\"input\":\"Tell me a joke about OpenTelemetry\"}"
90+
},
91+
"queryString": [],
92+
"url": "https://api.openai.com/v1/responses"
93+
},
94+
"response": {
95+
"bodySize": 683,
96+
"content": {
97+
"encoding": "base64",
98+
"mimeType": "application/json",
99+
"size": 683,
100+
"text": "[\"H4sIAAAAAAAAE3VTTa/aMBD8K6kvvYCUEAqBS6X+gV4q9fCoosXZgB+O7dprWoT4713nAx5Pr7dkPLvrnRlfhWrEVngMrs43FVSrvCyxqhZYFnm+gqIqqqbIy3xZFZtFVW3k+suySIT1uizETNj9K0oaW1gTkDHpEQibGhgv1uvNZrUsinImAgHFwFxpO6eRKUzegzwdvI2G79GCDsiQ0lqZg9hehYMLeq5o8IzaOv6+zR7lzxOWM4HeW6abqPVMtB5/RzTyUjs0oOkitvlMKDOV1w0SKB0mvjKBfJSkeI0J6+BvbSO5SDXZEz4fkLW6lqAfLTrboObrHhzNl3beKaPmi3yxnOfreVGJgeAhjZhKhu5i+3IdrOjC4f9OLFbY5MmJTYm4BMBV1W4kypZb08VhqscQ4JBs+FBuaQ2hGeaNFdN++JeYAMZYglGEl18zoe3Bebsf/3rWVvw8XrJGNRkdMfvO8v5AjR2SZ3RyKttzDE5ZdNkfRcfEVD4D57SSffuvO7Mz31BCDJgpyl5joEzaqBvzmbIjmEZj338nyIPEnchsO7bR9oyfxI3v461OO0AIivflxRLowLMrqJ8cYm85W45zypnAp0wweFY2hnoKcZ2sGPzh3TtH3EQesT7h5UPcYxK1d5VjVHfYWX9hMVmBYM2Y5V76pN7QANvW+vtfiF0HfuzOGQ/QIl34Hqlvq/Ce6oD+rPj+pMaH0ULU1NttPU5rEnYuBS0mqJhsuwoe2UH/NZrfH/C8M/q9DSoJwhFqVOwSOuh3tDwwiRzJigGc0mBd/chHPgBumOijkWPSRaMC7HUfwNinky+gzNtXlV7vW+TxOFm4pHHzoJa3mXj3KBerd9Db+rsLd3re70ag78gyQZzEu86cZmiAgBvcbv8AWeFv5icFAAA=\"]"
101+
},
102+
"cookies": [
103+
{
104+
"domain": "api.openai.com",
105+
"expires": "2026-05-28T10:58:34.000Z",
106+
"httpOnly": true,
107+
"name": "__cf_bm",
108+
"path": "/",
109+
"sameSite": "None",
110+
"secure": true,
111+
"value": "02CMVzrPPcxsao2auj7Q.td6y5xqLQct1DB7tKaLl4k-1779964112.2245798-1.0.1.1-_860cJD87skJAAopXuTNqG3gDJwgmToosZSk2A2M0buYkEKbkrrcoTevp7KzXDybXITcuQ1e_I4qK28SXaCjtL9YCEQ32342Qfs9dcXfV4a92acSpN1WqeSWqGV1k9lG"
112+
}
113+
],
114+
"headers": [
115+
{
116+
"name": "date",
117+
"value": "Thu, 28 May 2026 10:28:34 GMT"
118+
},
119+
{
120+
"name": "content-type",
121+
"value": "application/json"
122+
},
123+
{
124+
"name": "transfer-encoding",
125+
"value": "chunked"
126+
},
127+
{
128+
"name": "connection",
129+
"value": "keep-alive"
130+
},
131+
{
132+
"name": "cf-ray",
133+
"value": "a02c92b568e47a08-TLV"
134+
},
135+
{
136+
"name": "cf-cache-status",
137+
"value": "DYNAMIC"
138+
},
139+
{
140+
"name": "server",
141+
"value": "cloudflare"
142+
},
143+
{
144+
"name": "strict-transport-security",
145+
"value": "max-age=31536000; includeSubDomains; preload"
146+
},
147+
{
148+
"name": "x-content-type-options",
149+
"value": "nosniff"
150+
},
151+
{
152+
"name": "access-control-expose-headers",
153+
"value": "X-Request-ID, CF-Ray, CF-Ray"
154+
},
155+
{
156+
"name": "openai-organization",
157+
"value": "traceloop"
158+
},
159+
{
160+
"name": "openai-processing-ms",
161+
"value": "1884"
162+
},
163+
{
164+
"name": "openai-project",
165+
"value": "proj_tzz1TbPPOXaf6j9tEkVUBIAa"
166+
},
167+
{
168+
"name": "openai-version",
169+
"value": "2020-10-01"
170+
},
171+
{
172+
"name": "x-ratelimit-limit-requests",
173+
"value": "30000"
174+
},
175+
{
176+
"name": "x-ratelimit-limit-tokens",
177+
"value": "150000000"
178+
},
179+
{
180+
"name": "x-ratelimit-remaining-requests",
181+
"value": "29999"
182+
},
183+
{
184+
"name": "x-ratelimit-remaining-tokens",
185+
"value": "149999965"
186+
},
187+
{
188+
"name": "x-ratelimit-reset-requests",
189+
"value": "2ms"
190+
},
191+
{
192+
"name": "x-ratelimit-reset-tokens",
193+
"value": "0s"
194+
},
195+
{
196+
"name": "x-request-id",
197+
"value": "req_d316447754b2412ea3db6327c8d438b3"
198+
},
199+
{
200+
"_fromType": "array",
201+
"name": "set-cookie",
202+
"value": "__cf_bm=02CMVzrPPcxsao2auj7Q.td6y5xqLQct1DB7tKaLl4k-1779964112.2245798-1.0.1.1-_860cJD87skJAAopXuTNqG3gDJwgmToosZSk2A2M0buYkEKbkrrcoTevp7KzXDybXITcuQ1e_I4qK28SXaCjtL9YCEQ32342Qfs9dcXfV4a92acSpN1WqeSWqGV1k9lG; HttpOnly; SameSite=None; Secure; Path=/; Domain=api.openai.com; Expires=Thu, 28 May 2026 10:58:34 GMT"
203+
},
204+
{
205+
"name": "content-encoding",
206+
"value": "gzip"
207+
},
208+
{
209+
"name": "alt-svc",
210+
"value": "h3=\":443\"; ma=86400"
211+
}
212+
],
213+
"headersSize": 1146,
214+
"httpVersion": "HTTP/1.1",
215+
"redirectURL": "",
216+
"status": 200,
217+
"statusText": "OK"
218+
},
219+
"startedDateTime": "2026-05-28T10:28:31.988Z",
220+
"time": 2834,
221+
"timings": {
222+
"blocked": -1,
223+
"connect": -1,
224+
"dns": -1,
225+
"receive": 0,
226+
"send": 0,
227+
"ssl": -1,
228+
"wait": 2834
229+
},
230+
"_recordingName": "Test OpenAI instrumentation/should set cache read input tokens in span for responses with cached tokens"
231+
}
232+
],
233+
"pages": [],
234+
"version": "1.2"
235+
}
236+
}

packages/instrumentation-together/test/instrumentation.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,10 @@ describe("Test Together instrumentation", async function () {
598598
choices: [
599599
{
600600
index: 0,
601-
message: { role: "assistant", content: "North, South, East, West." },
601+
message: {
602+
role: "assistant",
603+
content: "North, South, East, West.",
604+
},
602605
finish_reason: "stop",
603606
},
604607
],
@@ -613,7 +616,9 @@ describe("Test Together instrumentation", async function () {
613616

614617
await together.chat.completions.create({
615618
model: "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
616-
messages: [{ role: "user", content: "What are the 4 cardinal directions?" }],
619+
messages: [
620+
{ role: "user", content: "What are the 4 cardinal directions?" },
621+
],
617622
});
618623

619624
const spans = memoryExporter.getFinishedSpans();

0 commit comments

Comments
 (0)