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 withstreamText:
With tool calls
When tools are involved, the middleware scans tool outputs as they flow back into the prompt: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
Everyuser and tool message in params.prompt before the model call. system and assistant messages are skipped.