Skip to content

Commit 546092d

Browse files
authored
fix(ai-sdk): report cached tokens usage (#839)
1 parent 1832387 commit 546092d

File tree

12 files changed

+904
-1132
lines changed

12 files changed

+904
-1132
lines changed

packages/ai-semantic-conventions/src/SemanticAttributes.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export const SpanAttributes = {
3535
LLM_USAGE_COMPLETION_TOKENS: "gen_ai.usage.completion_tokens",
3636
LLM_USAGE_INPUT_TOKENS: ATTR_GEN_AI_USAGE_INPUT_TOKENS,
3737
LLM_USAGE_OUTPUT_TOKENS: ATTR_GEN_AI_USAGE_OUTPUT_TOKENS,
38+
GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS:
39+
"gen_ai.usage.cache_creation_input_tokens",
40+
GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS: "gen_ai.usage.cache_read_input_tokens",
41+
GEN_AI_USAGE_REASONING_TOKENS: "gen_ai.usage.reasoning_tokens",
3842

3943
GEN_AI_AGENT_NAME: "gen_ai.agent.name",
4044

packages/instrumentation-anthropic/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"tslib": "^2.8.1"
4747
},
4848
"devDependencies": {
49-
"@anthropic-ai/sdk": "^0.56.0",
49+
"@anthropic-ai/sdk": "^0.71.0",
5050
"@opentelemetry/context-async-hooks": "^2.0.1",
5151
"@opentelemetry/sdk-trace-node": "^2.0.1",
5252
"@pollyjs/adapter-fetch": "^6.0.7",

packages/instrumentation-anthropic/recordings/Test-Anthropic-instrumentation_3769946143/should-set-attributes-in-span-for-completions-streaming_2198009633/recording.har

Lines changed: 0 additions & 162 deletions
This file was deleted.

packages/instrumentation-anthropic/recordings/Test-Anthropic-instrumentation_3769946143/should-set-attributes-in-span-for-completions_1224394582/recording.har

Lines changed: 0 additions & 162 deletions
This file was deleted.

packages/instrumentation-anthropic/src/instrumentation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,9 @@ export class AnthropicInstrumentation extends InstrumentationBase {
317317
usage: {
318318
input_tokens: 0,
319319
output_tokens: 0,
320-
cache_creation_input_tokens: 0,
321-
cache_read_input_tokens: 0,
320+
cache_creation: null,
321+
cache_creation_input_tokens: null,
322+
cache_read_input_tokens: null,
322323
server_tool_use: null,
323324
service_tier: null,
324325
},

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

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -91,100 +91,6 @@ describe("Test Anthropic instrumentation", async function () {
9191
context.disable();
9292
});
9393

94-
it("should set attributes in span for completions", async () => {
95-
const result = await anthropic.completions.create({
96-
model: "claude-2",
97-
max_tokens_to_sample: 300,
98-
prompt: `${AnthropicModule.HUMAN_PROMPT} Tell me a joke about OpenTelemetry${AnthropicModule.AI_PROMPT}`,
99-
});
100-
101-
const spans = memoryExporter.getFinishedSpans();
102-
const completionSpan = spans.find(
103-
(span) => span.name === "anthropic.completion",
104-
);
105-
106-
assert.ok(result);
107-
assert.ok(completionSpan);
108-
assert.strictEqual(
109-
completionSpan.attributes[`${SpanAttributes.LLM_REQUEST_MODEL}`],
110-
"claude-2",
111-
);
112-
assert.strictEqual(
113-
completionSpan.attributes[`${SpanAttributes.LLM_REQUEST_MAX_TOKENS}`],
114-
300,
115-
);
116-
assert.strictEqual(
117-
completionSpan.attributes[`${SpanAttributes.LLM_RESPONSE_MODEL}`],
118-
"claude-2.1",
119-
);
120-
assert.strictEqual(
121-
completionSpan.attributes[`${SpanAttributes.LLM_PROMPTS}.0.role`],
122-
"user",
123-
);
124-
assert.strictEqual(
125-
completionSpan.attributes[`${SpanAttributes.LLM_PROMPTS}.0.content`],
126-
`${AnthropicModule.HUMAN_PROMPT} Tell me a joke about OpenTelemetry${AnthropicModule.AI_PROMPT}`,
127-
);
128-
assert.strictEqual(
129-
completionSpan.attributes[`${SpanAttributes.LLM_COMPLETIONS}.0.role`],
130-
"assistant",
131-
);
132-
assert.strictEqual(
133-
completionSpan.attributes[`${SpanAttributes.LLM_COMPLETIONS}.0.content`],
134-
result.completion,
135-
);
136-
}).timeout(30000);
137-
138-
it("should set attributes in span for completions (streaming)", async () => {
139-
const result = await anthropic.completions.create({
140-
model: "claude-2",
141-
max_tokens_to_sample: 300,
142-
prompt: `${AnthropicModule.HUMAN_PROMPT} Tell me a joke about OpenTelemetry${AnthropicModule.AI_PROMPT}`,
143-
stream: true,
144-
});
145-
146-
let completion = "";
147-
for await (const chunk of result) {
148-
assert.ok(chunk);
149-
completion += chunk.completion;
150-
}
151-
152-
const spans = memoryExporter.getFinishedSpans();
153-
const completionSpan = spans.find(
154-
(span) => span.name === "anthropic.completion",
155-
);
156-
157-
assert.ok(completionSpan);
158-
assert.strictEqual(
159-
completionSpan.attributes[`${SpanAttributes.LLM_REQUEST_MODEL}`],
160-
"claude-2",
161-
);
162-
assert.strictEqual(
163-
completionSpan.attributes[`${SpanAttributes.LLM_REQUEST_MAX_TOKENS}`],
164-
300,
165-
);
166-
assert.strictEqual(
167-
completionSpan.attributes[`${SpanAttributes.LLM_RESPONSE_MODEL}`],
168-
"claude-2.1",
169-
);
170-
assert.strictEqual(
171-
completionSpan.attributes[`${SpanAttributes.LLM_PROMPTS}.0.role`],
172-
"user",
173-
);
174-
assert.strictEqual(
175-
completionSpan.attributes[`${SpanAttributes.LLM_PROMPTS}.0.content`],
176-
`${AnthropicModule.HUMAN_PROMPT} Tell me a joke about OpenTelemetry${AnthropicModule.AI_PROMPT}`,
177-
);
178-
assert.strictEqual(
179-
completionSpan.attributes[`${SpanAttributes.LLM_COMPLETIONS}.0.role`],
180-
"assistant",
181-
);
182-
assert.strictEqual(
183-
completionSpan.attributes[`${SpanAttributes.LLM_COMPLETIONS}.0.content`],
184-
completion,
185-
);
186-
}).timeout(30000);
187-
18894
it("should set attributes in span for messages", async () => {
18995
const message = await anthropic.messages.create({
19096
max_tokens: 1024,

packages/traceloop-sdk/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@
9393
"gitHead": "ef1e70d6037f7b5c061056ef2be16e3f55f02ed5",
9494
"devDependencies": {
9595
"@ai-sdk/amazon-bedrock": "^3.0.10",
96+
"@ai-sdk/anthropic": "^2.0.53",
9697
"@ai-sdk/google": "^2.0.8",
9798
"@ai-sdk/openai": "^2.0.19",
98-
"@anthropic-ai/sdk": "^0.56.0",
99+
"@anthropic-ai/sdk": "^0.71.0",
99100
"@aws-sdk/client-bedrock-runtime": "^3.848.0",
100101
"@google-cloud/aiplatform": "^4.4.0",
101102
"@google-cloud/vertexai": "^1.10.0",

0 commit comments

Comments
 (0)