/** * `agent({ retry })` for one model call. * * Two layers, both scoped to a single model request: * * 1. LangChain's own. Every built-in provider's chat model (except * `ChatOllama`, whose chat requests bypass it) sends each request through * `@langchain/core`'s `AsyncCaller`, which retries 5xx and network errors * and waits out a 429 whose `Retry-After` is 60s or less. B4 sets its * budget with the model's `maxRetries` ({@link providerMaxRetries}). * 2. This module's. `AsyncCaller` hands back, without retrying, a 429 it * classifies as a capacity limit (no `Retry-After`, or one over 60s). That * error is raised when the request is made, before any token, so B4 can * send the same model call again without repeating output * ({@link retryCapacityErrors}). A capacity 429 whose `retryAfterMs` is * longer than B4's 10s cap is surfaced at once instead: retrying sooner * than the server asked would only spend an attempt on another 429. */ import type { RetryConfig } from "@b4run/sdk"; export declare const DEFAULT_MAX_ATTEMPTS = 3; export declare const DEFAULT_BASE_DELAY_MS = 1000; export declare const MAX_RETRY_DELAY_MS = 10000; export interface ModelRetryPolicy { /** Attempts per model call, counting the first. */ readonly maxAttempts: number; /** Milliseconds before the first capacity retry; doubles each time. */ readonly baseDelay: number; } /** * The route's retry policy, defaulted and checked. Throws on a value that * cannot mean anything (not an object, an unknown key such as `maxAttemps`, * zero or fractional attempts, a negative delay), so a typo fails the route * instead of silently changing how often it retries. */ export declare function resolveModelRetryPolicy(retry: RetryConfig | undefined): ModelRetryPolicy; /** The chat model's `maxRetries`: the attempts after the first. */ export declare function providerMaxRetries(policy: ModelRetryPolicy): number; /** * The `maxRetries` for a chat model B4.run builds from a `retry` config: * `maxAttempts - 1`, with `maxAttempts` defaulting to 3. Throws on an * invalid `retry`, like the route itself. Used for the models B4 builds * besides the route's own: the summarizer, which gets its route's `retry`, * and the `b4 memory consolidate`/`reflect` distillation model, which has no * route and gets `{ maxAttempts }` from `b4.config.ts`'s * `memory.distill.retry` (validated by the CLI; unset means 3 attempts). * * `maxRetries` only reaches models whose requests go through * `@langchain/core`'s `AsyncCaller`. `ChatOllama`'s chat requests bypass it, * so on an Ollama model this has no effect. */ export declare function modelMaxRetries(retry: RetryConfig | undefined): number; /** * A 429 LangChain classified as a capacity limit and stamped retryable, * bare or inside `MiddlewareError`s. Recognised by LangChain's own stamps, * never by message text: the `retryable` mark (`getRetryable`, a * `Symbol.for` key, so duplicate copies of `@langchain/core` agree) and the * `rateLimitType` it sets alongside. A quota 429 carries * `rateLimitType: "stop"` and `retryable: false`; a 429 LangChain waited out * itself carries `rateLimitType: "wait"`. */ export declare function isCapacityRateLimitError(error: unknown): boolean; /** * The wait before capacity retry `retryIndex` (0 for the first retry), or * `undefined` when B4 must not retry at all. * * - The error carries a `retryAfterMs` (LangChain parsed a `Retry-After` * header, or a "try again in …" message): wait exactly that when it is 10s * or less; otherwise `undefined`, so the error surfaces right away with its * `retryAfterMs` for the caller to act on. * - No `retryAfterMs`: `baseDelay * 2^retryIndex` plus up to 500ms of jitter, * capped at 10s. */ export declare function capacityRetryDelay(error: unknown, retryIndex: number, baseDelay: number, random: () => number): number | undefined; /** Time source for the backoff; replaced in tests. */ export interface RetryClock { readonly sleep: (ms: number, signal: AbortSignal | undefined) => Promise; readonly random: () => number; } export declare const systemRetryClock: RetryClock; /** * Run one model call, sending it again after a capacity 429 until it * succeeds, fails another way, has used `maxAttempts` attempts, or hits a * capacity 429 whose `retryAfterMs` is over the 10s cap. An abort, whether * the signal was already aborted when the 429 arrived or fires during the * wait, rejects with the signal's reason and sends nothing more. * * Invariant: it only ever retries an error LangChain stamps when the request * is made (a capacity 429 is the response to the request itself, raised * before any token streams), so sending the call again cannot duplicate * streamed output. A failure mid-stream is never a capacity 429 and is * rethrown untouched. Widening what this retries would break that. */ export declare function retryCapacityErrors(call: () => T | Promise, policy: ModelRetryPolicy, options?: { readonly signal?: AbortSignal; readonly clock?: RetryClock; }): Promise; //# sourceMappingURL=model-call-retry.d.ts.map