@anthropic-ai/sdk 0.100.1 → 0.102.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/_vendor/partial-json-parser/parser.d.mts.map +1 -1
  3. package/_vendor/partial-json-parser/parser.d.ts.map +1 -1
  4. package/_vendor/partial-json-parser/parser.js +14 -2
  5. package/_vendor/partial-json-parser/parser.js.map +1 -1
  6. package/_vendor/partial-json-parser/parser.mjs +14 -2
  7. package/_vendor/partial-json-parser/parser.mjs.map +1 -1
  8. package/client.d.mts +33 -1
  9. package/client.d.mts.map +1 -1
  10. package/client.d.ts +33 -1
  11. package/client.d.ts.map +1 -1
  12. package/client.js +89 -19
  13. package/client.js.map +1 -1
  14. package/client.mjs +89 -19
  15. package/client.mjs.map +1 -1
  16. package/core/api.d.mts +11 -0
  17. package/core/api.d.mts.map +1 -0
  18. package/core/api.d.ts +11 -0
  19. package/core/api.d.ts.map +1 -0
  20. package/core/api.js +3 -0
  21. package/core/api.js.map +1 -0
  22. package/core/api.mjs +2 -0
  23. package/core/api.mjs.map +1 -0
  24. package/core/error.d.mts +11 -0
  25. package/core/error.d.mts.map +1 -1
  26. package/core/error.d.ts +11 -0
  27. package/core/error.d.ts.map +1 -1
  28. package/core/error.js +17 -1
  29. package/core/error.js.map +1 -1
  30. package/core/error.mjs +15 -0
  31. package/core/error.mjs.map +1 -1
  32. package/core/middleware.d.mts +117 -0
  33. package/core/middleware.d.mts.map +1 -0
  34. package/core/middleware.d.ts +117 -0
  35. package/core/middleware.d.ts.map +1 -0
  36. package/core/middleware.js +167 -0
  37. package/core/middleware.js.map +1 -0
  38. package/core/middleware.mjs +161 -0
  39. package/core/middleware.mjs.map +1 -0
  40. package/index.d.mts +3 -2
  41. package/index.d.mts.map +1 -1
  42. package/index.d.ts +3 -2
  43. package/index.d.ts.map +1 -1
  44. package/index.js +2 -1
  45. package/index.js.map +1 -1
  46. package/index.mjs +2 -2
  47. package/index.mjs.map +1 -1
  48. package/internal/request-options.d.mts +7 -0
  49. package/internal/request-options.d.mts.map +1 -1
  50. package/internal/request-options.d.ts +7 -0
  51. package/internal/request-options.d.ts.map +1 -1
  52. package/internal/request-options.js.map +1 -1
  53. package/internal/request-options.mjs.map +1 -1
  54. package/lib/BetaMessageStream.d.mts.map +1 -1
  55. package/lib/BetaMessageStream.d.ts.map +1 -1
  56. package/lib/BetaMessageStream.js +3 -0
  57. package/lib/BetaMessageStream.js.map +1 -1
  58. package/lib/BetaMessageStream.mjs +3 -0
  59. package/lib/BetaMessageStream.mjs.map +1 -1
  60. package/package.json +1 -1
  61. package/resources/beta/agents/agents.d.mts +11 -21
  62. package/resources/beta/agents/agents.d.mts.map +1 -1
  63. package/resources/beta/agents/agents.d.ts +11 -21
  64. package/resources/beta/agents/agents.d.ts.map +1 -1
  65. package/resources/beta/agents/agents.js.map +1 -1
  66. package/resources/beta/agents/agents.mjs.map +1 -1
  67. package/resources/beta/messages/messages.d.mts +13 -2
  68. package/resources/beta/messages/messages.d.mts.map +1 -1
  69. package/resources/beta/messages/messages.d.ts +13 -2
  70. package/resources/beta/messages/messages.d.ts.map +1 -1
  71. package/resources/beta/messages/messages.js +2 -0
  72. package/resources/beta/messages/messages.js.map +1 -1
  73. package/resources/beta/messages/messages.mjs +2 -0
  74. package/resources/beta/messages/messages.mjs.map +1 -1
  75. package/resources/beta/webhooks.d.mts +22 -22
  76. package/resources/beta/webhooks.d.ts +22 -22
  77. package/resources/messages/messages.d.mts +1 -0
  78. package/resources/messages/messages.d.mts.map +1 -1
  79. package/resources/messages/messages.d.ts +1 -0
  80. package/resources/messages/messages.d.ts.map +1 -1
  81. package/resources/messages/messages.js +2 -0
  82. package/resources/messages/messages.js.map +1 -1
  83. package/resources/messages/messages.mjs +2 -0
  84. package/resources/messages/messages.mjs.map +1 -1
  85. package/src/_vendor/partial-json-parser/parser.ts +18 -2
  86. package/src/client.ts +124 -23
  87. package/src/core/api.ts +11 -0
  88. package/src/core/error.ts +15 -0
  89. package/src/core/middleware.ts +293 -0
  90. package/src/index.ts +10 -1
  91. package/src/internal/request-options.ts +8 -0
  92. package/src/lib/BetaMessageStream.ts +3 -0
  93. package/src/resources/beta/agents/agents.ts +12 -21
  94. package/src/resources/beta/messages/messages.ts +19 -2
  95. package/src/resources/beta/webhooks.ts +22 -22
  96. package/src/resources/messages/messages.ts +4 -0
  97. package/src/version.ts +1 -1
  98. package/version.d.mts +1 -1
  99. package/version.d.ts +1 -1
  100. package/version.js +1 -1
  101. package/version.mjs +1 -1
@@ -0,0 +1,293 @@
1
+ import type { Fetch } from '../internal/builtin-types';
2
+ import { castToError, isAbortError } from '../internal/errors';
3
+ import { addRequestID } from '../internal/parse';
4
+ import type { FinalRequestOptions } from '../internal/request-options';
5
+ import type { APIRequest } from './api';
6
+ import { AnthropicError, APIConnectionError, RetryableError } from './error';
7
+ import { Stream } from './streaming';
8
+
9
+ /**
10
+ * Invokes the rest of the middleware chain, ending with the underlying `fetch`.
11
+ *
12
+ * This function can be invoked multiple times.
13
+ */
14
+ export type MiddlewareNext = (request: APIRequest) => Promise<Response>;
15
+
16
+ /**
17
+ * Helpers passed to each middleware alongside `next`, scoped to the request
18
+ * in flight (one context is shared by every middleware in the chain).
19
+ */
20
+ export interface MiddlewareContext {
21
+ /**
22
+ * The SDK request options the API call in flight was made with: `method`,
23
+ * `path`, the pre-encoded `body`, `stream`, etc.
24
+ *
25
+ * `undefined` when the chain isn't running for an SDK API request, i.e.
26
+ * for credential token-exchange requests.
27
+ */
28
+ readonly options?: FinalRequestOptions | undefined;
29
+
30
+ /**
31
+ * Parse a response body the way the SDK would for the request in flight:
32
+ *
33
+ * - JSON responses are decoded, with the non-enumerable `_request_id`
34
+ * property attached like SDK return values, and anything else resolves
35
+ * to the body text.
36
+ * - For streaming requests ({@link options}`.stream`), resolves immediately
37
+ * with a {@link Stream} reading an independent copy of the response body —
38
+ * iterating it doesn't consume the client's events, and aborting or
39
+ * `break`ing out of it doesn't cancel the underlying request. Each call
40
+ * returns a fresh `Stream` (streams are single-consumer, so they aren't
41
+ * cached). Error (non-2xx) responses parse as JSON/text rather than as a
42
+ * stream, mirroring the SDK's own handling.
43
+ * - For binary requests, resolves with the `Response` itself, unconsumed.
44
+ *
45
+ * Reads through an internal `response.clone()`, so the response stays
46
+ * readable: the client (and any other middleware) can still consume the
47
+ * body afterwards. Non-stream results are cached per `Response` and shared
48
+ * across the middleware chain, so repeated calls cost a single read.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * const mw: Middleware = async (request, next, ctx) => {
53
+ * const response = await next(request);
54
+ * const data = await ctx.parse<Message>(response);
55
+ * if (data.type === 'message') console.log(data.usage);
56
+ * return response;
57
+ * };
58
+ * ```
59
+ */
60
+ parse<T = unknown>(response: Response): Promise<T>;
61
+ }
62
+
63
+ /**
64
+ * A function that wraps each HTTP request made by the client.
65
+ *
66
+ * Middleware may observe or modify the request before calling `next`, observe
67
+ * or replace the response, short-circuit by returning a `Response` without
68
+ * calling `next`, or call `next` multiple times to implement custom retries.
69
+ *
70
+ * Middleware must not consume the body of the `Response` it returns - the
71
+ * client still needs to read it. To inspect the body, use
72
+ * `await ctx.parse(response)` (cached, leaves the body readable) or read a
73
+ * clone (`await response.clone().text()`); to transform it, return a
74
+ * replacement, e.g. `new Response(body, response)`.
75
+ *
76
+ * Middleware runs per HTTP attempt, inside the SDK's retry loop; the attempt
77
+ * number is available via the `X-Stainless-Retry-Count` request header. An
78
+ * error thrown from middleware propagates to the caller as-is.
79
+ *
80
+ * Middleware errors are **not** retried apart from connection-level errors:
81
+ * timeout/abort errors, errors thrown by `fetch()`, and `APIConnectionError`s
82
+ * or `RetryableError`s — thrown directly or present anywhere in an error's
83
+ * `cause` chain. Retryable middleware errors still propagate to the caller
84
+ * as-is once retries are exhausted.
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * const logger: Middleware = async (request, next) => {
89
+ * console.log('->', request.method, request.url);
90
+ * const response = await next(request);
91
+ * console.log('<-', response.status, request.url);
92
+ * return response;
93
+ * };
94
+ *
95
+ * const client = new Anthropic({ middleware: [logger] });
96
+ * ```
97
+ */
98
+ export type Middleware = (
99
+ request: APIRequest,
100
+ next: MiddlewareNext,
101
+ ctx: MiddlewareContext,
102
+ ) => Promise<Response>;
103
+
104
+ /**
105
+ * Errors thrown by the underlying `fetch`, as opposed to by a middleware.
106
+ *
107
+ * Tracked so the client can apply its connection-error retry policy to
108
+ * transport failures while letting errors thrown by middleware propagate to
109
+ * the caller untouched.
110
+ */
111
+ const fetchOriginErrors = new WeakSet<object>();
112
+
113
+ /** Whether `err` was thrown by the underlying `fetch` rather than by a middleware. */
114
+ export function isFetchOriginError(err: unknown): boolean {
115
+ return typeof err === 'object' && err !== null && fetchOriginErrors.has(err);
116
+ }
117
+
118
+ /**
119
+ * Whether an error thrown by middleware should stay on the SDK's
120
+ * connection-error retry policy: fetch-origin, abort, `APIConnectionError`, or
121
+ * `RetryableError` — checked through the error's `cause` chain.
122
+ */
123
+ export function isRetryableError(err: unknown): boolean {
124
+ const seen = new Set<unknown>(); // guard against `cause` cycles
125
+ while (typeof err === 'object' && err !== null && !seen.has(err)) {
126
+ seen.add(err);
127
+ if (
128
+ isFetchOriginError(err) ||
129
+ isAbortError(err) ||
130
+ err instanceof APIConnectionError ||
131
+ err instanceof RetryableError
132
+ ) {
133
+ return true;
134
+ }
135
+ err = (err as { cause?: unknown }).cause;
136
+ }
137
+ return false;
138
+ }
139
+
140
+ /**
141
+ * Wraps `fetchFn` so each call runs through `middleware`, keeping the same
142
+ * call signature as `fetch` itself.
143
+ *
144
+ * With no middleware, calls are passed straight through to `fetchFn`.
145
+ * Otherwise the arguments are normalized into an {@link APIRequest} (headers
146
+ * coerced to a `Headers` instance, URL stringified) before entering the
147
+ * chain. The chain is composed per call, so mutations of a `middleware`
148
+ * array are picked up by later requests.
149
+ *
150
+ * `options` — the SDK request options behind this call, when there are any —
151
+ * is surfaced to middleware as `ctx.options` and drives `ctx.parse`.
152
+ */
153
+ export function wrapFetchWithMiddleware(
154
+ fetchFn: Fetch,
155
+ middleware: readonly Middleware[],
156
+ options?: FinalRequestOptions | undefined,
157
+ ): Fetch {
158
+ return async (url, init = {}) => {
159
+ if (middleware.length === 0) {
160
+ // use undefined this binding; fetch errors if bound to something else in browser/cloudflare
161
+ return fetchFn.call(undefined, url, init);
162
+ }
163
+ const headers = init.headers instanceof Headers ? init.headers : new Headers(init.headers);
164
+ const response = await applyMiddleware(
165
+ fetchFn,
166
+ middleware,
167
+ options,
168
+ )({
169
+ ...init,
170
+ headers,
171
+ url:
172
+ typeof url === 'string' ? url
173
+ : url instanceof URL ? url.href
174
+ : url.url,
175
+ });
176
+ // Catch a footgun before the client tries to read the body itself and
177
+ // fails with a confusing low-level stream error.
178
+ if (response.bodyUsed || response.body?.locked) {
179
+ throw new AnthropicError(
180
+ 'middleware consumed the response body; use response.clone() to inspect it, ' +
181
+ 'or return new Response(body, response) to consume and replace it',
182
+ );
183
+ }
184
+ return response;
185
+ };
186
+ }
187
+
188
+ /**
189
+ * Creates the {@link MiddlewareContext} shared by every middleware in one chain.
190
+ */
191
+ function createMiddlewareContext(options: FinalRequestOptions | undefined): MiddlewareContext {
192
+ // Keyed on the Response so each `next()` call's response (e.g. with custom
193
+ // retries, or a middleware swapping in a replacement) parses independently,
194
+ // while several middleware parsing the same response share a single read.
195
+ const cache = new WeakMap<Response, Promise<unknown>>();
196
+ return {
197
+ options,
198
+ parse<T>(response: Response): Promise<T> {
199
+ // Streams are single-consumer, so caching one would hand later callers
200
+ // an already-consumed stream; every call gets a fresh clone-backed one.
201
+ if (options?.stream && response.ok) {
202
+ return parseMiddlewareResponse(response, options) as Promise<T>;
203
+ }
204
+ let parsed = cache.get(response);
205
+ if (!parsed) {
206
+ parsed = parseMiddlewareResponse(response, options);
207
+ cache.set(response, parsed);
208
+ }
209
+ return parsed as Promise<T>;
210
+ },
211
+ };
212
+ }
213
+
214
+ /**
215
+ * Mirrors the client's own response parsing (`defaultParseResponse` in
216
+ * `internal/parse.ts`), reading through a clone so the body stays available
217
+ * to the rest of the chain and the client itself.
218
+ */
219
+ async function parseMiddlewareResponse(
220
+ response: Response,
221
+ options: FinalRequestOptions | undefined,
222
+ ): Promise<unknown> {
223
+ if (response.bodyUsed || response.body?.locked) {
224
+ throw new AnthropicError(
225
+ 'cannot ctx.parse() a response whose body was already consumed; ' +
226
+ 'call ctx.parse() instead of reading the body, or read via response.clone()',
227
+ );
228
+ }
229
+
230
+ // Error responses parse as JSON/text below — the SDK only stream-parses
231
+ // successful responses, and middleware typically wants the error body.
232
+ if (options?.stream && response.ok) {
233
+ // A fresh controller rather than the request's own: aborting (or
234
+ // `break`ing out of) the middleware's stream must not cancel the
235
+ // in-flight request the client is still reading.
236
+ const streamClass = options.__streamClass ?? Stream;
237
+ return streamClass.fromSSEResponse(response.clone(), new AbortController());
238
+ }
239
+
240
+ // fetch refuses to read the body when the status code is 204.
241
+ if (response.status === 204) {
242
+ return null;
243
+ }
244
+
245
+ if (options?.__binaryResponse) {
246
+ return response;
247
+ }
248
+
249
+ const contentType = response.headers.get('content-type');
250
+ const mediaType = contentType?.split(';')[0]?.trim();
251
+ const isJSON = mediaType?.includes('application/json') || mediaType?.endsWith('+json');
252
+ if (isJSON) {
253
+ if (response.headers.get('content-length') === '0') {
254
+ // if there is no content we can't do anything
255
+ return undefined;
256
+ }
257
+ return addRequestID(await response.clone().json(), response);
258
+ }
259
+
260
+ return await response.clone().text();
261
+ }
262
+
263
+ /**
264
+ * Composes `middleware` around `fetchFn` and returns the entry point of the chain.
265
+ */
266
+ export function applyMiddleware(
267
+ fetchFn: Fetch,
268
+ middleware: readonly Middleware[],
269
+ options?: FinalRequestOptions | undefined,
270
+ ): MiddlewareNext {
271
+ // use undefined this binding; fetch errors if bound to something else in browser/cloudflare
272
+ let next: MiddlewareNext = async ({ url, ...init }) => {
273
+ try {
274
+ return await fetchFn.call(undefined, url, init);
275
+ } catch (err) {
276
+ // Brand the error as fetch-origin, normalizing with `castToError` first since a
277
+ // WeakSet can't hold primitives and the brand must be on the same object the
278
+ // client's own `castToError` will later pass through.
279
+ const error = castToError(err);
280
+ fetchOriginErrors.add(error);
281
+ throw error;
282
+ }
283
+ };
284
+
285
+ const ctx = createMiddlewareContext(options);
286
+ for (let i = middleware.length - 1; i >= 0; i--) {
287
+ const mw = middleware[i]!;
288
+ const nextInner = next;
289
+ next = async (request) => mw(request, nextInner, ctx);
290
+ }
291
+
292
+ return next;
293
+ }
package/src/index.ts CHANGED
@@ -4,7 +4,15 @@ export { Anthropic as default } from './client';
4
4
 
5
5
  export { type Uploadable, toFile } from './core/uploads';
6
6
  export { APIPromise } from './core/api-promise';
7
- export { BaseAnthropic, Anthropic, type ClientOptions, HUMAN_PROMPT, AI_PROMPT } from './client';
7
+ export { type Middleware, type MiddlewareContext, type MiddlewareNext } from './core/middleware';
8
+ export {
9
+ BaseAnthropic,
10
+ Anthropic,
11
+ type APIRequest,
12
+ type ClientOptions,
13
+ HUMAN_PROMPT,
14
+ AI_PROMPT,
15
+ } from './client';
8
16
  export { PagePromise } from './core/pagination';
9
17
  export {
10
18
  AnthropicError,
@@ -12,6 +20,7 @@ export {
12
20
  APIConnectionError,
13
21
  APIConnectionTimeoutError,
14
22
  APIUserAbortError,
23
+ RetryableError,
15
24
  NotFoundError,
16
25
  ConflictError,
17
26
  RateLimitError,
@@ -4,6 +4,7 @@ import { NullableHeaders } from './headers';
4
4
 
5
5
  import type { BodyInit } from './builtin-types';
6
6
  import { Stream } from '../core/streaming';
7
+ import type { Middleware } from '../core/middleware';
7
8
  import type { HTTPMethod, MergedRequestInit } from './types';
8
9
  import { type HeadersLike } from './headers';
9
10
 
@@ -66,6 +67,13 @@ export type RequestOptions = {
66
67
  */
67
68
  signal?: AbortSignal | undefined | null;
68
69
 
70
+ /**
71
+ * Additional {@link Middleware} to wrap this request's HTTP attempts.
72
+ *
73
+ * These run after any client-level middleware and apply to every attempt of this request, including retries.
74
+ */
75
+ middleware?: ReadonlyArray<Middleware> | undefined;
76
+
69
77
  /**
70
78
  * A unique key for this request to enable idempotency.
71
79
  */
@@ -578,6 +578,9 @@ export class BetaMessageStream<ParsedT = null> implements AsyncIterable<BetaMess
578
578
  snapshot.container = event.delta.container;
579
579
  snapshot.stop_reason = event.delta.stop_reason;
580
580
  snapshot.stop_sequence = event.delta.stop_sequence;
581
+ if (event.delta.stop_details != null) {
582
+ snapshot.stop_details = event.delta.stop_details;
583
+ }
581
584
  snapshot.usage.output_tokens = event.usage.output_tokens;
582
585
  snapshot.context_management = event.context_management;
583
586
 
@@ -519,20 +519,13 @@ export interface BetaManagedAgentsCustomTool {
519
519
  * JSON Schema for custom tool input parameters.
520
520
  */
521
521
  export interface BetaManagedAgentsCustomToolInputSchema {
522
- /**
523
- * JSON Schema properties defining the tool's input parameters.
524
- */
522
+ type: 'object';
523
+
525
524
  properties?: { [key: string]: unknown } | null;
526
525
 
527
- /**
528
- * List of required property names.
529
- */
530
- required?: Array<string>;
526
+ required?: Array<string> | null;
531
527
 
532
- /**
533
- * Must be 'object' for tool input schemas.
534
- */
535
- type?: 'object';
528
+ [k: string]: unknown;
536
529
  }
537
530
 
538
531
  /**
@@ -835,12 +828,12 @@ export interface AgentCreateParams {
835
828
  model: BetaManagedAgentsModel | BetaManagedAgentsModelConfigParams;
836
829
 
837
830
  /**
838
- * Body param: Human-readable name for the agent. 1-256 characters.
831
+ * Body param: Human-readable name for the agent.
839
832
  */
840
833
  name: string;
841
834
 
842
835
  /**
843
- * Body param: Description of what the agent does. Up to 2048 characters.
836
+ * Body param: Description of what the agent does.
844
837
  */
845
838
  description?: string | null;
846
839
 
@@ -864,12 +857,12 @@ export interface AgentCreateParams {
864
857
  multiagent?: SessionsAPI.BetaManagedAgentsMultiagentParams | null;
865
858
 
866
859
  /**
867
- * Body param: Skills available to the agent. Maximum 20.
860
+ * Body param: Skills available to the agent.
868
861
  */
869
862
  skills?: Array<BetaManagedAgentsSkillParams>;
870
863
 
871
864
  /**
872
- * Body param: System prompt for the agent. Up to 100,000 characters.
865
+ * Body param: System prompt for the agent.
873
866
  */
874
867
  system?: string | null;
875
868
 
@@ -911,8 +904,7 @@ export interface AgentUpdateParams {
911
904
  version: number;
912
905
 
913
906
  /**
914
- * Body param: Description. Up to 2048 characters. Omit to preserve; send empty
915
- * string or null to clear.
907
+ * Body param: Description. Omit to preserve; send empty string or null to clear.
916
908
  */
917
909
  description?: string | null;
918
910
 
@@ -945,20 +937,19 @@ export interface AgentUpdateParams {
945
937
  multiagent?: SessionsAPI.BetaManagedAgentsMultiagentParams | null;
946
938
 
947
939
  /**
948
- * Body param: Human-readable name. 1-256 characters. Omit to preserve. Cannot be
940
+ * Body param: Human-readable name. Must be non-empty. Omit to preserve. Cannot be
949
941
  * cleared.
950
942
  */
951
943
  name?: string;
952
944
 
953
945
  /**
954
946
  * Body param: Skills. Full replacement. Omit to preserve; send empty array or null
955
- * to clear. Maximum 20.
947
+ * to clear.
956
948
  */
957
949
  skills?: Array<BetaManagedAgentsSkillParams> | null;
958
950
 
959
951
  /**
960
- * Body param: System prompt. Up to 100,000 characters. Omit to preserve; send
961
- * empty string or null to clear.
952
+ * Body param: System prompt. Omit to preserve; send empty string or null to clear.
962
953
  */
963
954
  system?: string | null;
964
955
 
@@ -60,6 +60,8 @@ const DEPRECATED_MODELS: {
60
60
  'claude-2.0': 'July 21st, 2025',
61
61
  'claude-3-7-sonnet-latest': 'February 19th, 2026',
62
62
  'claude-3-7-sonnet-20250219': 'February 19th, 2026',
63
+ 'claude-opus-4-1': 'August 5th, 2026',
64
+ 'claude-opus-4-1-20250805': 'August 5th, 2026',
63
65
  };
64
66
 
65
67
  const MODELS_TO_WARN_WITH_THINKING_ENABLED: Model[] = ['claude-mythos-preview', 'claude-opus-4-6'];
@@ -393,6 +395,17 @@ export interface BetaAdvisorTool20260301 {
393
395
  */
394
396
  defer_loading?: boolean;
395
397
 
398
+ /**
399
+ * Bounds the advisor's total output (thinking + text) per call. When the advisor
400
+ * hits this cap, the returned advisor_result or advisor_redacted_result block
401
+ * carries stop_reason='max_tokens', and a truncation note is appended to the
402
+ * advice text the worker model sees (inside the encrypted blob in redacted mode).
403
+ * When set, the server also emits a remaining-tokens budget block in the advisor's
404
+ * prompt so the advisor self-shapes toward the cap. When omitted, the advisor
405
+ * model's default output cap applies and no budget block is emitted.
406
+ */
407
+ max_tokens?: number | null;
408
+
396
409
  /**
397
410
  * Maximum number of times the tool can be used in the API request.
398
411
  */
@@ -435,7 +448,8 @@ export interface BetaAdvisorToolResultError {
435
448
  | 'too_many_requests'
436
449
  | 'overloaded'
437
450
  | 'unavailable'
438
- | 'execution_time_exceeded';
451
+ | 'execution_time_exceeded'
452
+ | 'model_not_found';
439
453
 
440
454
  type: 'advisor_tool_result_error';
441
455
  }
@@ -447,7 +461,8 @@ export interface BetaAdvisorToolResultErrorParam {
447
461
  | 'too_many_requests'
448
462
  | 'overloaded'
449
463
  | 'unavailable'
450
- | 'execution_time_exceeded';
464
+ | 'execution_time_exceeded'
465
+ | 'model_not_found';
451
466
 
452
467
  type: 'advisor_tool_result_error';
453
468
  }
@@ -3228,6 +3243,8 @@ export interface BetaToolSearchToolResultErrorParam {
3228
3243
  error_code: 'invalid_tool_input' | 'unavailable' | 'too_many_requests' | 'execution_time_exceeded';
3229
3244
 
3230
3245
  type: 'tool_search_tool_result_error';
3246
+
3247
+ error_message?: string | null;
3231
3248
  }
3232
3249
 
3233
3250
  export interface BetaToolSearchToolSearchResultBlock {
@@ -63,7 +63,7 @@ export type BetaWebhookEventData =
63
63
 
64
64
  export interface BetaWebhookSessionArchivedEventData {
65
65
  /**
66
- * ID of the resource that triggered the event.
66
+ * ID of the session that triggered the event.
67
67
  */
68
68
  id: string;
69
69
 
@@ -76,7 +76,7 @@ export interface BetaWebhookSessionArchivedEventData {
76
76
 
77
77
  export interface BetaWebhookSessionCreatedEventData {
78
78
  /**
79
- * ID of the resource that triggered the event.
79
+ * ID of the session that triggered the event.
80
80
  */
81
81
  id: string;
82
82
 
@@ -89,7 +89,7 @@ export interface BetaWebhookSessionCreatedEventData {
89
89
 
90
90
  export interface BetaWebhookSessionDeletedEventData {
91
91
  /**
92
- * ID of the resource that triggered the event.
92
+ * ID of the session that triggered the event.
93
93
  */
94
94
  id: string;
95
95
 
@@ -102,7 +102,7 @@ export interface BetaWebhookSessionDeletedEventData {
102
102
 
103
103
  export interface BetaWebhookSessionIdledEventData {
104
104
  /**
105
- * ID of the resource that triggered the event.
105
+ * ID of the session that triggered the event.
106
106
  */
107
107
  id: string;
108
108
 
@@ -115,7 +115,7 @@ export interface BetaWebhookSessionIdledEventData {
115
115
 
116
116
  export interface BetaWebhookSessionOutcomeEvaluationEndedEventData {
117
117
  /**
118
- * ID of the resource that triggered the event.
118
+ * ID of the session that triggered the event.
119
119
  */
120
120
  id: string;
121
121
 
@@ -128,7 +128,7 @@ export interface BetaWebhookSessionOutcomeEvaluationEndedEventData {
128
128
 
129
129
  export interface BetaWebhookSessionPendingEventData {
130
130
  /**
131
- * ID of the resource that triggered the event.
131
+ * ID of the session that triggered the event.
132
132
  */
133
133
  id: string;
134
134
 
@@ -141,7 +141,7 @@ export interface BetaWebhookSessionPendingEventData {
141
141
 
142
142
  export interface BetaWebhookSessionRequiresActionEventData {
143
143
  /**
144
- * ID of the resource that triggered the event.
144
+ * ID of the session that triggered the event.
145
145
  */
146
146
  id: string;
147
147
 
@@ -154,7 +154,7 @@ export interface BetaWebhookSessionRequiresActionEventData {
154
154
 
155
155
  export interface BetaWebhookSessionRunningEventData {
156
156
  /**
157
- * ID of the resource that triggered the event.
157
+ * ID of the session that triggered the event.
158
158
  */
159
159
  id: string;
160
160
 
@@ -167,7 +167,7 @@ export interface BetaWebhookSessionRunningEventData {
167
167
 
168
168
  export interface BetaWebhookSessionStatusIdledEventData {
169
169
  /**
170
- * ID of the resource that triggered the event.
170
+ * ID of the session that triggered the event.
171
171
  */
172
172
  id: string;
173
173
 
@@ -180,7 +180,7 @@ export interface BetaWebhookSessionStatusIdledEventData {
180
180
 
181
181
  export interface BetaWebhookSessionStatusRescheduledEventData {
182
182
  /**
183
- * ID of the resource that triggered the event.
183
+ * ID of the session that triggered the event.
184
184
  */
185
185
  id: string;
186
186
 
@@ -193,7 +193,7 @@ export interface BetaWebhookSessionStatusRescheduledEventData {
193
193
 
194
194
  export interface BetaWebhookSessionStatusRunStartedEventData {
195
195
  /**
196
- * ID of the resource that triggered the event.
196
+ * ID of the session that triggered the event.
197
197
  */
198
198
  id: string;
199
199
 
@@ -206,7 +206,7 @@ export interface BetaWebhookSessionStatusRunStartedEventData {
206
206
 
207
207
  export interface BetaWebhookSessionStatusTerminatedEventData {
208
208
  /**
209
- * ID of the resource that triggered the event.
209
+ * ID of the session that triggered the event.
210
210
  */
211
211
  id: string;
212
212
 
@@ -219,7 +219,7 @@ export interface BetaWebhookSessionStatusTerminatedEventData {
219
219
 
220
220
  export interface BetaWebhookSessionThreadCreatedEventData {
221
221
  /**
222
- * ID of the resource that triggered the event.
222
+ * ID of the session that triggered the event.
223
223
  */
224
224
  id: string;
225
225
 
@@ -232,7 +232,7 @@ export interface BetaWebhookSessionThreadCreatedEventData {
232
232
 
233
233
  export interface BetaWebhookSessionThreadIdledEventData {
234
234
  /**
235
- * ID of the resource that triggered the event.
235
+ * ID of the session that triggered the event.
236
236
  */
237
237
  id: string;
238
238
 
@@ -245,7 +245,7 @@ export interface BetaWebhookSessionThreadIdledEventData {
245
245
 
246
246
  export interface BetaWebhookSessionThreadTerminatedEventData {
247
247
  /**
248
- * ID of the resource that triggered the event.
248
+ * ID of the session that triggered the event.
249
249
  */
250
250
  id: string;
251
251
 
@@ -258,7 +258,7 @@ export interface BetaWebhookSessionThreadTerminatedEventData {
258
258
 
259
259
  export interface BetaWebhookVaultArchivedEventData {
260
260
  /**
261
- * ID of the resource that triggered the event.
261
+ * ID of the vault that triggered the event.
262
262
  */
263
263
  id: string;
264
264
 
@@ -271,7 +271,7 @@ export interface BetaWebhookVaultArchivedEventData {
271
271
 
272
272
  export interface BetaWebhookVaultCreatedEventData {
273
273
  /**
274
- * ID of the resource that triggered the event.
274
+ * ID of the vault that triggered the event.
275
275
  */
276
276
  id: string;
277
277
 
@@ -284,7 +284,7 @@ export interface BetaWebhookVaultCreatedEventData {
284
284
 
285
285
  export interface BetaWebhookVaultCredentialArchivedEventData {
286
286
  /**
287
- * ID of the resource that triggered the event.
287
+ * ID of the vault credential that triggered the event.
288
288
  */
289
289
  id: string;
290
290
 
@@ -302,7 +302,7 @@ export interface BetaWebhookVaultCredentialArchivedEventData {
302
302
 
303
303
  export interface BetaWebhookVaultCredentialCreatedEventData {
304
304
  /**
305
- * ID of the resource that triggered the event.
305
+ * ID of the vault credential that triggered the event.
306
306
  */
307
307
  id: string;
308
308
 
@@ -320,7 +320,7 @@ export interface BetaWebhookVaultCredentialCreatedEventData {
320
320
 
321
321
  export interface BetaWebhookVaultCredentialDeletedEventData {
322
322
  /**
323
- * ID of the resource that triggered the event.
323
+ * ID of the vault credential that triggered the event.
324
324
  */
325
325
  id: string;
326
326
 
@@ -338,7 +338,7 @@ export interface BetaWebhookVaultCredentialDeletedEventData {
338
338
 
339
339
  export interface BetaWebhookVaultCredentialRefreshFailedEventData {
340
340
  /**
341
- * ID of the resource that triggered the event.
341
+ * ID of the vault credential that triggered the event.
342
342
  */
343
343
  id: string;
344
344
 
@@ -356,7 +356,7 @@ export interface BetaWebhookVaultCredentialRefreshFailedEventData {
356
356
 
357
357
  export interface BetaWebhookVaultDeletedEventData {
358
358
  /**
359
- * ID of the resource that triggered the event.
359
+ * ID of the vault that triggered the event.
360
360
  */
361
361
  id: string;
362
362