@@ -262,7 +262,16 @@ await client.messages.create({ max_tokens: 1024, messages: [{ role: 'user', cont
262
262
263
263
### Timeouts
264
264
265
- Requests time out after 10 minutes by default. You can configure this with a ` timeout ` option:
265
+ By default requests time out after 10 minutes. However if you have specified a large ` max_tokens ` value and are
266
+ * not* streaming, the default timeout will be calculated dynamically using the formula:
267
+ ``` typescript
268
+ const minimum = 10 * 60 ;
269
+ const calculated = (60 * 60 * maxTokens ) / 128_000 ;
270
+ return calculated < minimum ? minimum * 1000 : calculated * 1000 ;
271
+ ```
272
+ which will result in a timeout up to 60 minutes, scaled by the ` max_tokens ` parameter, unless overriden at the request or client level.
273
+
274
+ You can configure this with a ` timeout ` option:
266
275
267
276
<!-- prettier-ignore -->
268
277
``` ts
@@ -281,6 +290,24 @@ On timeout, an `APIConnectionTimeoutError` is thrown.
281
290
282
291
Note that requests which time out will be [ retried twice by default] ( #retries ) .
283
292
293
+ ### Long Requests
294
+
295
+ > [ !IMPORTANT]
296
+ > We highly encourage you use the streaming [ Messages API] ( #streaming-responses ) for longer running requests.
297
+
298
+ We do not recommend setting a large ` max_tokens ` values without using streaming.
299
+ Some networks may drop idle connections after a certain period of time, which
300
+ can cause the request to fail or [ timeout] ( #timeouts ) without receiving a response from Anthropic.
301
+
302
+ This SDK will also throw an error if a non-streaming request is expected to be above roughly 10 minutes long.
303
+ Passing ` stream: true ` or [ overriding] ( #timeouts ) the ` timeout ` option at the client or request level disables this error.
304
+
305
+ An expected request latency longer than the [ timeout] ( #timeouts ) for a non-streaming request
306
+ will result in the client terminating the connection and retrying without receiving a response.
307
+
308
+ When supported by the ` fetch ` implementation, we set a [ TCP socket keep-alive] ( https://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html )
309
+ option in order to reduce the impact of idle connection timeouts on some networks.
310
+
284
311
## Auto-pagination
285
312
286
313
List methods in the Anthropic API are paginated.
0 commit comments