Skip to main content
The Vercel AI SDK supports language-model middleware. Benchspan ships an asMiddleware() helper that scans the prompt before the model is invoked.
TypeScript only. The Vercel AI SDK has no official Python equivalent.

Install

Usage

route.ts

With streaming

Works identically with streamText:
If an injection is detected, the middleware throws before the stream is created, so your error handler fires normally.

With tool calls

When tools are involved, the middleware scans tool outputs as they flow back into the prompt:
Each time read_email returns and its output is added to the prompt, BenchGuard scans it. An injection in the email body aborts the run before the next model call.

What gets scanned

Every user and tool message in params.prompt before the model call. system and assistant messages are skipped.