Skip to content

Middleware ​

Middleware transforms runtime data without changing the core tool contract. Use hooks when behavior decides whether a run continues; use middleware when data needs to be transformed.

Create middleware ​

ts
import { createMiddleware } from '@anvia/core'

const hideInternalErrors = createMiddleware({
  onToolOutput({ result }) {
    if (!result.includes('INTERNAL_')) {
      return
    }

    return 'The tool returned an internal service error.'
  },
})

Attach it to an agent ​

ts
const agent = new AgentBuilder('support', model)
  .middleware(hideInternalErrors)
  .build()

Agent middleware applies to every run. Request middleware applies only to one prompt.

ts
const response = await agent
  .prompt(message)
  .withMiddleware(hideInternalErrors)
  .send()

Choose an interception point ​

MethodRuns when
onCompletionRequestBefore a provider-neutral request reaches the model.
onCompletionResponseAfter the model responds and before the runtime continues.
onToolInputAfter approval and before the tool handler runs.
onToolOutputBefore tool output is sent back to the model.

Agent-level middleware runs before request-level middleware.

Keep contracts in the handler ​

Middleware is useful for instrumentation, normalization, request shaping, large-result spillover, and output filtering. It must not bypass schema validation or replace handler authorization.

Built for Anvia.