Skip to content

Commit ffab311

Browse files
committed
feat(api): add claude-3.7 + support for thinking
1 parent 8665946 commit ffab311

File tree

22 files changed

+703
-129
lines changed

22 files changed

+703
-129
lines changed

README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
> [!IMPORTANT]
1+
> [!IMPORTANT]
22
> We're actively working on a new alpha version that migrates from `node-fetch` to builtin fetch.
3-
>
3+
>
44
> Please try it out and let us know if you run into any issues!
55
> https://github.com/anthropics/anthropic-sdk-typescript/issues/645
66
@@ -268,7 +268,16 @@ await client.messages.create({ max_tokens: 1024, messages: [{ role: 'user', cont
268268

269269
### Timeouts
270270

271-
Requests time out after 10 minutes by default. You can configure this with a `timeout` option:
271+
By default requests time out after 10 minutes. However if you have specified a large `max_tokens` value and are
272+
*not* streaming, the default timeout will be calculated dynamically using the formula:
273+
```typescript
274+
const minimum = 10 * 60;
275+
const calculated = (60 * 60 * maxTokens) / 128_000;
276+
return calculated < minimum ? minimum * 1000 : calculated * 1000;
277+
```
278+
which will result in a timeout up to 60 minutes, scaled by the `max_tokens` parameter, unless overriden at the request or client level.
279+
280+
You can configure this with a `timeout` option:
272281

273282
<!-- prettier-ignore -->
274283
```ts
@@ -287,6 +296,24 @@ On timeout, an `APIConnectionTimeoutError` is thrown.
287296

288297
Note that requests which time out will be [retried twice by default](#retries).
289298

299+
### Long Requests
300+
301+
> [!IMPORTANT]
302+
> We highly encourage you use the streaming [Messages API](#streaming-responses) for longer running requests.
303+
304+
We do not recommend setting a large `max_tokens` values without using streaming.
305+
Some networks may drop idle connections after a certain period of time, which
306+
can cause the request to fail or [timeout](#timeouts) without receiving a response from Anthropic.
307+
308+
This SDK will also throw an error if a non-streaming request is expected to be above roughly 10 minutes long.
309+
Passing `stream: true` or [overriding](#timeouts) the `timeout` option at the client or request level disables this error.
310+
311+
An expected request latency longer than the [timeout](#timeouts) for a non-streaming request
312+
will result in the client terminating the connection and retrying without receiving a response.
313+
314+
When supported by the `fetch` implementation, we set a [TCP socket keep-alive](https://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html)
315+
option in order to reduce the impact of idle connection timeouts on some networks.
316+
290317
## Auto-pagination
291318

292319
List methods in the Anthropic API are paginated.

api.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Types:
4141
- <code><a href="./src/resources/messages/messages.ts">ImageBlockParam</a></code>
4242
- <code><a href="./src/resources/messages/messages.ts">InputJSONDelta</a></code>
4343
- <code><a href="./src/resources/messages/messages.ts">Message</a></code>
44+
- <code><a href="./src/resources/messages/messages.ts">MessageCountTokensTool</a></code>
4445
- <code><a href="./src/resources/messages/messages.ts">MessageDeltaEvent</a></code>
4546
- <code><a href="./src/resources/messages/messages.ts">MessageDeltaUsage</a></code>
4647
- <code><a href="./src/resources/messages/messages.ts">MessageParam</a></code>
@@ -58,17 +59,29 @@ Types:
5859
- <code><a href="./src/resources/messages/messages.ts">RawMessageStartEvent</a></code>
5960
- <code><a href="./src/resources/messages/messages.ts">RawMessageStopEvent</a></code>
6061
- <code><a href="./src/resources/messages/messages.ts">RawMessageStreamEvent</a></code>
62+
- <code><a href="./src/resources/messages/messages.ts">RedactedThinkingBlock</a></code>
63+
- <code><a href="./src/resources/messages/messages.ts">RedactedThinkingBlockParam</a></code>
64+
- <code><a href="./src/resources/messages/messages.ts">SignatureDelta</a></code>
6165
- <code><a href="./src/resources/messages/messages.ts">TextBlock</a></code>
6266
- <code><a href="./src/resources/messages/messages.ts">TextBlockParam</a></code>
6367
- <code><a href="./src/resources/messages/messages.ts">TextCitation</a></code>
6468
- <code><a href="./src/resources/messages/messages.ts">TextCitationParam</a></code>
6569
- <code><a href="./src/resources/messages/messages.ts">TextDelta</a></code>
70+
- <code><a href="./src/resources/messages/messages.ts">ThinkingBlock</a></code>
71+
- <code><a href="./src/resources/messages/messages.ts">ThinkingBlockParam</a></code>
72+
- <code><a href="./src/resources/messages/messages.ts">ThinkingConfigDisabled</a></code>
73+
- <code><a href="./src/resources/messages/messages.ts">ThinkingConfigEnabled</a></code>
74+
- <code><a href="./src/resources/messages/messages.ts">ThinkingConfigParam</a></code>
75+
- <code><a href="./src/resources/messages/messages.ts">ThinkingDelta</a></code>
6676
- <code><a href="./src/resources/messages/messages.ts">Tool</a></code>
77+
- <code><a href="./src/resources/messages/messages.ts">ToolBash20250124</a></code>
6778
- <code><a href="./src/resources/messages/messages.ts">ToolChoice</a></code>
6879
- <code><a href="./src/resources/messages/messages.ts">ToolChoiceAny</a></code>
6980
- <code><a href="./src/resources/messages/messages.ts">ToolChoiceAuto</a></code>
7081
- <code><a href="./src/resources/messages/messages.ts">ToolChoiceTool</a></code>
7182
- <code><a href="./src/resources/messages/messages.ts">ToolResultBlockParam</a></code>
83+
- <code><a href="./src/resources/messages/messages.ts">ToolTextEditor20250124</a></code>
84+
- <code><a href="./src/resources/messages/messages.ts">ToolUnion</a></code>
7285
- <code><a href="./src/resources/messages/messages.ts">ToolUseBlock</a></code>
7386
- <code><a href="./src/resources/messages/messages.ts">ToolUseBlockParam</a></code>
7487
- <code><a href="./src/resources/messages/messages.ts">Usage</a></code>
@@ -175,20 +188,32 @@ Types:
175188
- <code><a href="./src/resources/beta/messages/messages.ts">BetaRawMessageStartEvent</a></code>
176189
- <code><a href="./src/resources/beta/messages/messages.ts">BetaRawMessageStopEvent</a></code>
177190
- <code><a href="./src/resources/beta/messages/messages.ts">BetaRawMessageStreamEvent</a></code>
191+
- <code><a href="./src/resources/beta/messages/messages.ts">BetaRedactedThinkingBlock</a></code>
192+
- <code><a href="./src/resources/beta/messages/messages.ts">BetaRedactedThinkingBlockParam</a></code>
193+
- <code><a href="./src/resources/beta/messages/messages.ts">BetaSignatureDelta</a></code>
178194
- <code><a href="./src/resources/beta/messages/messages.ts">BetaTextBlock</a></code>
179195
- <code><a href="./src/resources/beta/messages/messages.ts">BetaTextBlockParam</a></code>
180196
- <code><a href="./src/resources/beta/messages/messages.ts">BetaTextCitation</a></code>
181197
- <code><a href="./src/resources/beta/messages/messages.ts">BetaTextCitationParam</a></code>
182198
- <code><a href="./src/resources/beta/messages/messages.ts">BetaTextDelta</a></code>
199+
- <code><a href="./src/resources/beta/messages/messages.ts">BetaThinkingBlock</a></code>
200+
- <code><a href="./src/resources/beta/messages/messages.ts">BetaThinkingBlockParam</a></code>
201+
- <code><a href="./src/resources/beta/messages/messages.ts">BetaThinkingConfigDisabled</a></code>
202+
- <code><a href="./src/resources/beta/messages/messages.ts">BetaThinkingConfigEnabled</a></code>
203+
- <code><a href="./src/resources/beta/messages/messages.ts">BetaThinkingConfigParam</a></code>
204+
- <code><a href="./src/resources/beta/messages/messages.ts">BetaThinkingDelta</a></code>
183205
- <code><a href="./src/resources/beta/messages/messages.ts">BetaTool</a></code>
184206
- <code><a href="./src/resources/beta/messages/messages.ts">BetaToolBash20241022</a></code>
207+
- <code><a href="./src/resources/beta/messages/messages.ts">BetaToolBash20250124</a></code>
185208
- <code><a href="./src/resources/beta/messages/messages.ts">BetaToolChoice</a></code>
186209
- <code><a href="./src/resources/beta/messages/messages.ts">BetaToolChoiceAny</a></code>
187210
- <code><a href="./src/resources/beta/messages/messages.ts">BetaToolChoiceAuto</a></code>
188211
- <code><a href="./src/resources/beta/messages/messages.ts">BetaToolChoiceTool</a></code>
189212
- <code><a href="./src/resources/beta/messages/messages.ts">BetaToolComputerUse20241022</a></code>
213+
- <code><a href="./src/resources/beta/messages/messages.ts">BetaToolComputerUse20250124</a></code>
190214
- <code><a href="./src/resources/beta/messages/messages.ts">BetaToolResultBlockParam</a></code>
191215
- <code><a href="./src/resources/beta/messages/messages.ts">BetaToolTextEditor20241022</a></code>
216+
- <code><a href="./src/resources/beta/messages/messages.ts">BetaToolTextEditor20250124</a></code>
192217
- <code><a href="./src/resources/beta/messages/messages.ts">BetaToolUnion</a></code>
193218
- <code><a href="./src/resources/beta/messages/messages.ts">BetaToolUseBlock</a></code>
194219
- <code><a href="./src/resources/beta/messages/messages.ts">BetaToolUseBlockParam</a></code>

packages/vertex-sdk/yarn.lock

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
form-data-encoder "1.7.2"
2828
formdata-node "^4.3.2"
2929
node-fetch "^2.6.7"
30-
web-streams-polyfill "^3.2.1"
3130

3231
"@babel/code-frame@^7.0.0", "@babel/code-frame@^7.12.13", "@babel/code-frame@^7.22.13", "@babel/code-frame@^7.23.5":
3332
version "7.23.5"
@@ -3257,11 +3256,6 @@ [email protected]:
32573256
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-4.0.0-beta.3.tgz#2898486b74f5156095e473efe989dcf185047a38"
32583257
integrity sha512-QW95TCTaHmsYfHDybGMwO5IJIM93I/6vTRk+daHTWFPhwh+C8Cg7j7XyKrwrj8Ib6vYXe0ocYNrmzY4xAAN6ug==
32593258

3260-
web-streams-polyfill@^3.2.1:
3261-
version "3.3.2"
3262-
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.3.2.tgz#32e26522e05128203a7de59519be3c648004343b"
3263-
integrity sha512-3pRGuxRF5gpuZc0W+EpwQRmCD7gRqcDOMt688KmdlDAgAyaB1XlN0zq2njfDNm44XVdIouE7pZ6GzbdyH47uIQ==
3264-
32653259
webidl-conversions@^3.0.0:
32663260
version "3.0.1"
32673261
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"

src/core.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,18 @@ export abstract class APIClient {
408408
return reqHeaders;
409409
}
410410

411+
_calculateNonstreamingTimeout(maxTokens: number): number {
412+
const defaultTimeout = 10 * 60;
413+
const expectedTimeout = (60 * 60 * maxTokens) / 128_000;
414+
if (expectedTimeout > defaultTimeout) {
415+
throw new AnthropicError(
416+
'Streaming is strongly recommended for operations that may take longer than 10 minutes. ' +
417+
'See https://github.com/anthropics/anthropic-sdk-python#streaming-responses for more details',
418+
);
419+
}
420+
return defaultTimeout * 1000;
421+
}
422+
411423
/**
412424
* Used as a callback for mutating the given `FinalRequestOptions` object.
413425
*/
@@ -574,10 +586,23 @@ export abstract class APIClient {
574586
fetchOptions.method = fetchOptions.method.toUpperCase();
575587
}
576588

589+
// turn on TCP keep-alive for the sockets, if the runtime supports it
590+
const socketKeepAliveInterval = 60 * 1000;
591+
const keepAliveTimeout = setTimeout(() => {
592+
if (fetchOptions && (fetchOptions as any)?.agent?.sockets) {
593+
for (const socket of Object.values((fetchOptions as any)?.agent?.sockets).flat()) {
594+
if ((socket as any)?.setKeepAlive) {
595+
(socket as any).setKeepAlive(true, socketKeepAliveInterval);
596+
}
597+
}
598+
}
599+
}, socketKeepAliveInterval);
600+
577601
return (
578602
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
579603
this.fetch.call(undefined, url, fetchOptions).finally(() => {
580604
clearTimeout(timeout);
605+
clearTimeout(keepAliveTimeout);
581606
})
582607
);
583608
}

src/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
InputJSONDelta,
5454
Message,
5555
MessageCountTokensParams,
56+
MessageCountTokensTool,
5657
MessageCreateParams,
5758
MessageCreateParamsNonStreaming,
5859
MessageCreateParamsStreaming,
@@ -75,17 +76,29 @@ import {
7576
RawMessageStartEvent,
7677
RawMessageStopEvent,
7778
RawMessageStreamEvent,
79+
RedactedThinkingBlock,
80+
RedactedThinkingBlockParam,
81+
SignatureDelta,
7882
TextBlock,
7983
TextBlockParam,
8084
TextCitation,
8185
TextCitationParam,
8286
TextDelta,
87+
ThinkingBlock,
88+
ThinkingBlockParam,
89+
ThinkingConfigDisabled,
90+
ThinkingConfigEnabled,
91+
ThinkingConfigParam,
92+
ThinkingDelta,
8393
Tool,
94+
ToolBash20250124,
8495
ToolChoice,
8596
ToolChoiceAny,
8697
ToolChoiceAuto,
8798
ToolChoiceTool,
8899
ToolResultBlockParam,
100+
ToolTextEditor20250124,
101+
ToolUnion,
89102
ToolUseBlock,
90103
ToolUseBlockParam,
91104
Usage,
@@ -354,6 +367,7 @@ export declare namespace Anthropic {
354367
type ImageBlockParam as ImageBlockParam,
355368
type InputJSONDelta as InputJSONDelta,
356369
type Message as Message,
370+
type MessageCountTokensTool as MessageCountTokensTool,
357371
type MessageDeltaEvent as MessageDeltaEvent,
358372
type MessageDeltaUsage as MessageDeltaUsage,
359373
type MessageParam as MessageParam,
@@ -371,17 +385,29 @@ export declare namespace Anthropic {
371385
type RawMessageStartEvent as RawMessageStartEvent,
372386
type RawMessageStopEvent as RawMessageStopEvent,
373387
type RawMessageStreamEvent as RawMessageStreamEvent,
388+
type RedactedThinkingBlock as RedactedThinkingBlock,
389+
type RedactedThinkingBlockParam as RedactedThinkingBlockParam,
390+
type SignatureDelta as SignatureDelta,
374391
type TextBlock as TextBlock,
375392
type TextBlockParam as TextBlockParam,
376393
type TextCitation as TextCitation,
377394
type TextCitationParam as TextCitationParam,
378395
type TextDelta as TextDelta,
396+
type ThinkingBlock as ThinkingBlock,
397+
type ThinkingBlockParam as ThinkingBlockParam,
398+
type ThinkingConfigDisabled as ThinkingConfigDisabled,
399+
type ThinkingConfigEnabled as ThinkingConfigEnabled,
400+
type ThinkingConfigParam as ThinkingConfigParam,
401+
type ThinkingDelta as ThinkingDelta,
379402
type Tool as Tool,
403+
type ToolBash20250124 as ToolBash20250124,
380404
type ToolChoice as ToolChoice,
381405
type ToolChoiceAny as ToolChoiceAny,
382406
type ToolChoiceAuto as ToolChoiceAuto,
383407
type ToolChoiceTool as ToolChoiceTool,
384408
type ToolResultBlockParam as ToolResultBlockParam,
409+
type ToolTextEditor20250124 as ToolTextEditor20250124,
410+
type ToolUnion as ToolUnion,
385411
type ToolUseBlock as ToolUseBlock,
386412
type ToolUseBlockParam as ToolUseBlockParam,
387413
type Usage as Usage,

src/lib/BetaMessageStream.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export interface MessageStreamEvents {
2121
text: (textDelta: string, textSnapshot: string) => void;
2222
citation: (citation: BetaTextCitation, citationsSnapshot: BetaTextCitation[]) => void;
2323
inputJson: (partialJson: string, jsonSnapshot: unknown) => void;
24+
thinking: (thinkingDelta: string, thinkingSnapshot: string) => void;
25+
signature: (signature: string) => void;
2426
message: (message: BetaMessage) => void;
2527
contentBlock: (content: BetaContentBlock) => void;
2628
finalMessage: (message: BetaMessage) => void;
@@ -434,6 +436,18 @@ export class BetaMessageStream implements AsyncIterable<BetaMessageStreamEvent>
434436
}
435437
break;
436438
}
439+
case 'thinking_delta': {
440+
if (content.type === 'thinking') {
441+
this._emit('thinking', event.delta.thinking, content.thinking);
442+
}
443+
break;
444+
}
445+
case 'signature_delta': {
446+
if (content.type === 'thinking') {
447+
this._emit('signature', content.signature);
448+
}
449+
break;
450+
}
437451
default:
438452
checkNever(event.delta);
439453
}
@@ -557,6 +571,18 @@ export class BetaMessageStream implements AsyncIterable<BetaMessageStreamEvent>
557571
}
558572
break;
559573
}
574+
case 'thinking_delta': {
575+
if (snapshotContent?.type === 'thinking') {
576+
snapshotContent.thinking += event.delta.thinking;
577+
}
578+
break;
579+
}
580+
case 'signature_delta': {
581+
if (snapshotContent?.type === 'thinking') {
582+
snapshotContent.signature = event.delta.signature;
583+
}
584+
break;
585+
}
560586
default:
561587
checkNever(event.delta);
562588
}

src/lib/MessageStream.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export interface MessageStreamEvents {
2121
text: (textDelta: string, textSnapshot: string) => void;
2222
citation: (citation: TextCitation, citationsSnapshot: TextCitation[]) => void;
2323
inputJson: (partialJson: string, jsonSnapshot: unknown) => void;
24+
thinking: (thinkingDelta: string, thinkingSnapshot: string) => void;
25+
signature: (signature: string) => void;
2426
message: (message: Message) => void;
2527
contentBlock: (content: ContentBlock) => void;
2628
finalMessage: (message: Message) => void;
@@ -434,6 +436,18 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
434436
}
435437
break;
436438
}
439+
case 'thinking_delta': {
440+
if (content.type === 'thinking') {
441+
this._emit('thinking', event.delta.thinking, content.thinking);
442+
}
443+
break;
444+
}
445+
case 'signature_delta': {
446+
if (content.type === 'thinking') {
447+
this._emit('signature', content.signature);
448+
}
449+
break;
450+
}
437451
default:
438452
checkNever(event.delta);
439453
}
@@ -557,6 +571,18 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
557571
}
558572
break;
559573
}
574+
case 'thinking_delta': {
575+
if (snapshotContent?.type === 'thinking') {
576+
snapshotContent.thinking += event.delta.thinking;
577+
}
578+
break;
579+
}
580+
case 'signature_delta': {
581+
if (snapshotContent?.type === 'thinking') {
582+
snapshotContent.signature = event.delta.signature;
583+
}
584+
break;
585+
}
560586
default:
561587
checkNever(event.delta);
562588
}

0 commit comments

Comments
 (0)