Built-in Middleware
Agent Express ships with 16 middleware functions organized into 6 namespaces. All are imported from agent-express:
import { guard, observe, model, memory, tools, dev } from "agent-express"defaults()
Section titled “defaults()”By default, every Agent automatically applies a sensible set of middleware via the defaults() function. This includes:
model.retry()— exponential backoff for transient LLM failuresobserve.usage()— token trackingobserve.tools()— tool call recordingobserve.duration()— turn timingguard.maxIterations()— loop iteration limit (default: 25)
Customizing Defaults
Section titled “Customizing Defaults”// Defaults with custom optionsconst agent = new Agent({ name: "my-agent", model: "anthropic/claude-sonnet-4-6", instructions: "...", defaults: { maxIterations: 10, retry: { maxRetries: 3 } },})Opting Out
Section titled “Opting Out”// Bare minimum: no defaults appliedconst agent = new Agent({ name: "my-agent", model: "anthropic/claude-sonnet-4-6", instructions: "...", defaults: false,})Using defaults() Directly
Section titled “Using defaults() Directly”import { defaults } from "agent-express"
// Get the default middleware array for custom compositionconst defaultMiddleware = defaults({ maxIterations: 15 })agent.use(defaultMiddleware)Middleware Namespaces
Section titled “Middleware Namespaces”Agent Express organizes its built-in middleware into 6 namespaces:
- Guard — Safety middleware: budget, input/output validation, timeouts, iteration limits, tool approval.
- Observe — Observability middleware: token usage, tool calls, duration, structured logging.
- Model — Model middleware: retry with backoff, complexity-based routing.
- Memory — Memory middleware: context window compaction with 5 strategies.
- Tools — Tool middleware: TypeScript function tools with Zod schemas, MCP server connection.
- Dev — Development middleware: full lifecycle terminal trace.