Skip to content

Commit 930155a

Browse files
authored
feat(anthropic): add anthropic beta client support (#7010)
* Implement beta message handling for Anthropic SDK and add corresponding test cases - Added a new hook for the beta version of the Anthropic SDK to support message creation. - Introduced test cases for the beta messages.create and messages.stream methods, ensuring proper span creation and handling of streaming responses. - Created two new YAML test cassettes to validate interactions with the beta API. * code review
1 parent 0dc8604 commit 930155a

File tree

4 files changed

+370
-2
lines changed

4 files changed

+370
-2
lines changed

packages/datadog-instrumentations/src/anthropic.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,16 @@ for (const extension of extensions) {
112112

113113
return exports
114114
})
115+
116+
addHook({
117+
name: '@anthropic-ai/sdk',
118+
file: `resources/beta/messages/messages.${extension}`,
119+
versions: ['>=0.33.0']
120+
}, exports => {
121+
const Messages = exports.Messages
122+
123+
shimmer.wrap(Messages.prototype, 'create', wrapCreate)
124+
125+
return exports
126+
})
115127
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
interactions:
2+
- request:
3+
body: "{\n \"model\": \"claude-3-7-sonnet-20250219\",\n \"messages\": [\n {\n
4+
\ \"role\": \"user\",\n \"content\": \"Hello, world!\"\n }\n ],\n
5+
\ \"max_tokens\": 100,\n \"temperature\": 0.5\n}"
6+
headers:
7+
? !!python/object/apply:multidict._multidict.istr
8+
- Accept
9+
: - application/json
10+
? !!python/object/apply:multidict._multidict.istr
11+
- Accept-Encoding
12+
: - gzip,deflate
13+
? !!python/object/apply:multidict._multidict.istr
14+
- Connection
15+
: - keep-alive
16+
Content-Length:
17+
- '174'
18+
? !!python/object/apply:multidict._multidict.istr
19+
- Content-Type
20+
: - application/json
21+
? !!python/object/apply:multidict._multidict.istr
22+
- User-Agent
23+
: - Anthropic/JS 0.33.0
24+
? !!python/object/apply:multidict._multidict.istr
25+
- anthropic-version
26+
: - '2023-06-01'
27+
? !!python/object/apply:multidict._multidict.istr
28+
- x-stainless-arch
29+
: - arm64
30+
? !!python/object/apply:multidict._multidict.istr
31+
- x-stainless-lang
32+
: - js
33+
? !!python/object/apply:multidict._multidict.istr
34+
- x-stainless-os
35+
: - MacOS
36+
? !!python/object/apply:multidict._multidict.istr
37+
- x-stainless-package-version
38+
: - 0.33.0
39+
? !!python/object/apply:multidict._multidict.istr
40+
- x-stainless-retry-count
41+
: - '0'
42+
? !!python/object/apply:multidict._multidict.istr
43+
- x-stainless-runtime
44+
: - node
45+
? !!python/object/apply:multidict._multidict.istr
46+
- x-stainless-runtime-version
47+
: - v22.17.0
48+
method: POST
49+
uri: https://api.anthropic.com/v1/messages?beta=true
50+
response:
51+
body:
52+
string: '{"model":"claude-3-7-sonnet-20250219","id":"msg_01DaYerrE2paN7DTx3fExPL3","type":"message","role":"assistant","content":[{"type":"text","text":"Hello!
53+
It''s nice to meet you. I''m Claude, an AI assistant created by Anthropic.
54+
How can I help you today? Whether you have a question, need information, or
55+
just want to chat, I''m here and ready to assist."}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":11,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"output_tokens":55,"service_tier":"standard"}}'
56+
headers:
57+
CF-RAY:
58+
- 9a58ca77c9633cff-CDG
59+
Connection:
60+
- keep-alive
61+
Content-Encoding:
62+
- gzip
63+
Content-Type:
64+
- application/json
65+
Date:
66+
- Fri, 28 Nov 2025 09:20:43 GMT
67+
Server:
68+
- cloudflare
69+
Transfer-Encoding:
70+
- chunked
71+
X-Robots-Tag:
72+
- none
73+
anthropic-organization-id:
74+
- 9c200896-5432-457d-9657-de2aa1125ae3
75+
anthropic-ratelimit-input-tokens-limit:
76+
- '11000000'
77+
anthropic-ratelimit-input-tokens-remaining:
78+
- '11000000'
79+
anthropic-ratelimit-input-tokens-reset:
80+
- '2025-11-28T09:20:42Z'
81+
anthropic-ratelimit-output-tokens-limit:
82+
- '1400000'
83+
anthropic-ratelimit-output-tokens-remaining:
84+
- '1400000'
85+
anthropic-ratelimit-output-tokens-reset:
86+
- '2025-11-28T09:20:43Z'
87+
anthropic-ratelimit-tokens-limit:
88+
- '12400000'
89+
anthropic-ratelimit-tokens-remaining:
90+
- '12400000'
91+
anthropic-ratelimit-tokens-reset:
92+
- '2025-11-28T09:20:42Z'
93+
cf-cache-status:
94+
- DYNAMIC
95+
request-id:
96+
- req_011CVa3JvsjWUteHDMzp7rZc
97+
retry-after:
98+
- '19'
99+
strict-transport-security:
100+
- max-age=31536000; includeSubDomains; preload
101+
x-envoy-upstream-service-time:
102+
- '1284'
103+
status:
104+
code: 200
105+
message: OK
106+
version: 1
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
interactions:
2+
- request:
3+
body: "{\n \"model\": \"claude-3-7-sonnet-20250219\",\n \"messages\": [\n {\n
4+
\ \"role\": \"user\",\n \"content\": \"Hello, world!\"\n }\n ],\n
5+
\ \"max_tokens\": 100,\n \"temperature\": 0.5,\n \"stream\": true\n}"
6+
headers:
7+
? !!python/object/apply:multidict._multidict.istr
8+
- Accept
9+
: - application/json
10+
? !!python/object/apply:multidict._multidict.istr
11+
- Accept-Encoding
12+
: - gzip,deflate
13+
? !!python/object/apply:multidict._multidict.istr
14+
- Connection
15+
: - keep-alive
16+
Content-Length:
17+
- '192'
18+
? !!python/object/apply:multidict._multidict.istr
19+
- Content-Type
20+
: - application/json
21+
? !!python/object/apply:multidict._multidict.istr
22+
- User-Agent
23+
: - Anthropic/JS 0.33.0
24+
? !!python/object/apply:multidict._multidict.istr
25+
- anthropic-version
26+
: - '2023-06-01'
27+
? !!python/object/apply:multidict._multidict.istr
28+
- x-stainless-arch
29+
: - arm64
30+
? !!python/object/apply:multidict._multidict.istr
31+
- x-stainless-lang
32+
: - js
33+
? !!python/object/apply:multidict._multidict.istr
34+
- x-stainless-os
35+
: - MacOS
36+
? !!python/object/apply:multidict._multidict.istr
37+
- x-stainless-package-version
38+
: - 0.33.0
39+
? !!python/object/apply:multidict._multidict.istr
40+
- x-stainless-retry-count
41+
: - '0'
42+
? !!python/object/apply:multidict._multidict.istr
43+
- x-stainless-runtime
44+
: - node
45+
? !!python/object/apply:multidict._multidict.istr
46+
- x-stainless-runtime-version
47+
: - v22.17.0
48+
method: POST
49+
uri: https://api.anthropic.com/v1/messages?beta=true
50+
response:
51+
body:
52+
string: 'event: message_start
53+
54+
data: {"type":"message_start","message":{"model":"claude-3-7-sonnet-20250219","id":"msg_015wqyvM5QFg4MA1yEkouCXW","type":"message","role":"assistant","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":11,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"output_tokens":2,"service_tier":"standard"}} }
55+
56+
57+
event: content_block_start
58+
59+
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""} }
60+
61+
62+
event: content_block_delta
63+
64+
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello!"}
65+
}
66+
67+
68+
event: content_block_delta
69+
70+
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"
71+
It"} }
72+
73+
74+
event: ping
75+
76+
data: {"type": "ping"}
77+
78+
79+
event: content_block_delta
80+
81+
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"''s
82+
nice to meet you"} }
83+
84+
85+
event: content_block_delta
86+
87+
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":".
88+
I''m Claude"} }
89+
90+
91+
event: content_block_delta
92+
93+
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":",
94+
an AI assistant made"} }
95+
96+
97+
event: content_block_delta
98+
99+
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"
100+
by Anthropic."} }
101+
102+
103+
event: content_block_delta
104+
105+
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"
106+
How"} }
107+
108+
109+
event: content_block_delta
110+
111+
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"
112+
can"} }
113+
114+
115+
event: content_block_delta
116+
117+
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"
118+
I help you today?"} }
119+
120+
121+
event: content_block_stop
122+
123+
data: {"type":"content_block_stop","index":0 }
124+
125+
126+
event: message_delta
127+
128+
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"input_tokens":11,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":32} }
129+
130+
131+
event: message_stop
132+
133+
data: {"type":"message_stop" }
134+
135+
136+
'
137+
headers:
138+
CF-RAY:
139+
- 9a58ca80cd5299bd-CDG
140+
Cache-Control:
141+
- no-cache
142+
Connection:
143+
- keep-alive
144+
Content-Type:
145+
- text/event-stream; charset=utf-8
146+
Date:
147+
- Fri, 28 Nov 2025 09:20:44 GMT
148+
Server:
149+
- cloudflare
150+
Transfer-Encoding:
151+
- chunked
152+
X-Robots-Tag:
153+
- none
154+
anthropic-organization-id:
155+
- 9c200896-5432-457d-9657-de2aa1125ae3
156+
anthropic-ratelimit-input-tokens-limit:
157+
- '11000000'
158+
anthropic-ratelimit-input-tokens-remaining:
159+
- '11000000'
160+
anthropic-ratelimit-input-tokens-reset:
161+
- '2025-11-28T09:20:43Z'
162+
anthropic-ratelimit-output-tokens-limit:
163+
- '1400000'
164+
anthropic-ratelimit-output-tokens-remaining:
165+
- '1400000'
166+
anthropic-ratelimit-output-tokens-reset:
167+
- '2025-11-28T09:20:43Z'
168+
anthropic-ratelimit-tokens-limit:
169+
- '12400000'
170+
anthropic-ratelimit-tokens-remaining:
171+
- '12400000'
172+
anthropic-ratelimit-tokens-reset:
173+
- '2025-11-28T09:20:43Z'
174+
cf-cache-status:
175+
- DYNAMIC
176+
request-id:
177+
- req_011CVa3K31aZX6i8Tcq74Ka5
178+
retry-after:
179+
- '17'
180+
strict-transport-security:
181+
- max-age=31536000; includeSubDomains; preload
182+
x-envoy-upstream-service-time:
183+
- '609'
184+
status:
185+
code: 200
186+
message: OK
187+
version: 1

0 commit comments

Comments
 (0)