Skip to main content

Overview

Middleware in Agenite allows you to intercept and modify agent behavior at various stages of execution. Middleware can be used for logging, error handling, state management, and more.

Interface

The middleware wraps the agent’s execution generator, allowing you to:
  • Intercept and modify values yielded by the generator
  • Process the final return value
  • Handle next values passed into the generator
  • Access the execution context

Middleware Types

Using middleware with an agent

Custom middleware examples

Logging middleware

Metrics middleware

State persistence middleware

Best practices

  1. Order matters: Middlewares execute in the order specified in the array, with each wrapping the ones that come after it
  2. Keep it focused: Each middleware should have a single responsibility
  3. Error handling: Implement proper error handling within the middleware
  4. Performance: Be mindful of performance impacts, especially in middleware that runs for every yield
  5. Typing: Use proper TypeScript types for your custom middleware

Next steps