Skip to content

model

const model: object

retry: (config?) => Middleware = modelRetry

Exponential backoff retry for transient LLM failures.

Creates a model.retry() middleware that wraps LLM calls with exponential backoff.

On transient failures (rate limits, network errors, retryable model errors), retries up to maxRetries times with exponential backoff starting at initialDelayMs (doubling each attempt). Non-retryable errors propagate immediately without retry.

Uses the same retry classification as the core withRetry() utility: RateLimitError and NetworkError are retryable, AuthenticationError and ContentFilterError are not.

RetryConfig

Retry configuration. Defaults to 2 retries with 1000ms initial delay.

Middleware

Middleware that retries failed model calls with exponential backoff

// Default: 2 retries, 1s initial delay
agent.use(model.retry())
// Custom: 3 retries, 500ms initial delay
agent.use(model.retry({ maxRetries: 3, initialDelayMs: 500 }))

router: (config) => Middleware = modelRouter

Route model calls by complexity.

Creates a model.router() middleware that routes model calls by complexity.

Classifies each model call as simple, medium, or complex, then overrides the model to the configured route target. Saves 60-90% on LLM costs for mixed-complexity workloads.

ModelRouterConfig

Routes mapping and optional custom classifier

Middleware

Middleware that routes model calls by complexity

agent.use(model.router({
routes: {
simple: "anthropic/claude-haiku-4-5",
medium: "anthropic/claude-sonnet-4-6",
complex: "anthropic/claude-opus-4-6",
},
}))