This repository was archived by the owner on May 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrumentation-anthropic.test.ts
More file actions
167 lines (133 loc) · 5.04 KB
/
Copy pathinstrumentation-anthropic.test.ts
File metadata and controls
167 lines (133 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { describe, it, expect, beforeEach, vi } from "vitest";
import { TraceStore, TraceCollector } from "../src/index.js";
import type { PatchContext } from "../src/instrumentation/types.js";
import { patchAnthropic, type AnthropicPatchTarget } from "../src/instrumentation/anthropic.js";
function createMockAnthropic() {
const mockResponse = {
id: "msg_123",
model: "claude-sonnet-4-6",
type: "message",
role: "assistant",
content: [{ type: "text", text: "Hello!" }],
usage: { input_tokens: 12, output_tokens: 8 },
stop_reason: "end_turn",
};
const mockCreate = vi.fn().mockResolvedValue(mockResponse);
const target: AnthropicPatchTarget = {
Messages: {
prototype: {
create: mockCreate,
},
},
};
return { target, mockCreate, mockResponse };
}
describe("Anthropic Auto-Instrumentation", () => {
let store: TraceStore;
let collector: TraceCollector;
let activeTraceId: string | null;
beforeEach(() => {
store = new TraceStore(":memory:");
collector = new TraceCollector({ store });
activeTraceId = null;
});
function makeContext(overrides?: Partial<PatchContext>): PatchContext {
return {
collector,
agentId: "test-agent",
captureContent: false,
getActiveTrace: () => activeTraceId,
...overrides,
};
}
it("records an LLM call span after a successful call", async () => {
const { target } = createMockAnthropic();
const ctx = makeContext();
const patch = patchAnthropic(target, ctx);
activeTraceId = collector.startTrace();
await target.Messages.prototype.create.call(
{},
{ model: "claude-sonnet-4-6", messages: [{ role: "user", content: "Hi" }], max_tokens: 1024 }
);
const spans = store.getSpans(activeTraceId);
expect(spans).toHaveLength(1);
expect(spans[0].name).toBe("llm.claude-sonnet-4-6");
expect(spans[0].type).toBe("agent.llm_call");
expect(spans[0].attributes.prompt_tokens).toBe(12);
expect(spans[0].attributes.completion_tokens).toBe(8);
expect(spans[0].status).toBe("ok");
expect(spans[0].duration_ms).toBeGreaterThanOrEqual(0);
// Verify cost record was created
const costs = store.getCostsByTrace(activeTraceId);
expect(costs).toHaveLength(1);
expect(costs[0].model).toBe("claude-sonnet-4-6");
patch.restore();
});
it("records error status and captures error message when the call throws", async () => {
const { target } = createMockAnthropic();
target.Messages.prototype.create = vi.fn().mockRejectedValue(new Error("overloaded"));
const ctx = makeContext();
const patch = patchAnthropic(target, ctx);
activeTraceId = collector.startTrace();
await expect(
target.Messages.prototype.create.call(
{},
{ model: "claude-sonnet-4-6", messages: [], max_tokens: 1024 }
)
).rejects.toThrow("overloaded");
const spans = store.getSpans(activeTraceId);
expect(spans).toHaveLength(1);
expect(spans[0].status).toBe("error");
expect(spans[0].attributes.error).toBe("overloaded");
expect(spans[0].duration_ms).toBeGreaterThanOrEqual(0);
patch.restore();
});
it("skips recording when no active trace", async () => {
const { target } = createMockAnthropic();
const ctx = makeContext();
const patch = patchAnthropic(target, ctx);
await target.Messages.prototype.create.call(
{},
{ model: "claude-sonnet-4-6", messages: [], max_tokens: 1024 }
);
const traces = store.listTraces({});
expect(traces).toHaveLength(0);
patch.restore();
});
it("restores original method on restore()", () => {
const { target, mockCreate } = createMockAnthropic();
const ctx = makeContext();
const patch = patchAnthropic(target, ctx);
expect(target.Messages.prototype.create).not.toBe(mockCreate);
patch.restore();
expect(target.Messages.prototype.create).toBe(mockCreate);
});
it("captures content when captureContent is true", async () => {
const { target } = createMockAnthropic();
const ctx = makeContext({ captureContent: true });
const patch = patchAnthropic(target, ctx);
activeTraceId = collector.startTrace();
await target.Messages.prototype.create.call(
{},
{ model: "claude-sonnet-4-6", messages: [{ role: "user", content: "Hello" }], max_tokens: 1024 }
);
const spans = store.getSpans(activeTraceId);
expect(spans[0].attributes.input).toBeDefined();
expect(spans[0].attributes.output).toBe("Hello!");
patch.restore();
});
it("uses input_tokens and output_tokens from Anthropic response", async () => {
const { target } = createMockAnthropic();
const ctx = makeContext();
const patch = patchAnthropic(target, ctx);
activeTraceId = collector.startTrace();
await target.Messages.prototype.create.call(
{},
{ model: "claude-sonnet-4-6", messages: [], max_tokens: 1024 }
);
const spans = store.getSpans(activeTraceId);
expect(spans[0].attributes.prompt_tokens).toBe(12);
expect(spans[0].attributes.completion_tokens).toBe(8);
patch.restore();
});
});