> ## Documentation Index
> Fetch the complete documentation index at: https://silmaril.security/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Vercel AI Gateway

> Check input and generated text for model calls routed through Vercel AI Gateway.

Silmaril middleware runs in your application around the gateway model. It inspects calls made through that wrapped model.

## Prerequisites

* Install and configure the [TypeScript SDK](https://silmaril.security/docs/sdk/typescript.md).
* Set `AI_GATEWAY_API_KEY` for your Vercel AI Gateway account, or use Vercel's supported deployment authentication.
* Set `SILMARIL_API_KEY` and `SILMARIL_API_URL` to your provisioned Silmaril classify credentials.

## Install the AI SDK

```bash theme={"theme":"github-light-default"}
npm install ai@^6
```

## Wrap the gateway model

```typescript theme={"theme":"github-light-default"}
import { Firewall } from "@silmaril-security/sdk"
import { gateway, generateText, wrapLanguageModel } from "ai"

const fw = new Firewall({
  apiKey: process.env.SILMARIL_API_KEY!,
  apiUrl: process.env.SILMARIL_API_URL!,
  mode: "shadow",
})

const model = wrapLanguageModel({
  model: gateway("anthropic/claude-sonnet-4.6"),
  middleware: fw.asMiddleware({
    onClassify: (event) => {
      if (event.blocked) {
        console.warn("would block", event.hook, event.result.score)
      }
    },
  }),
})

const response = await generateText({
  model,
  prompt: "Summarize the benefits of request validation.",
})

console.log(response.text)
```

Use a model available to your gateway account. The example starts in shadow mode, so would-block decisions do not interrupt model calls. From TypeScript SDK 0.10.0 the middleware classifies generated text by default, so one wrapped model covers input and output.

## Verify and enforce

Confirm both input and output classifications appear in Silmaril. The middleware checks input before the model call and generated text before it is returned.

When ready to enforce, set `mode` to `"block"` and handle `FirewallBlockedException` before returning a response to the caller. See [TypeScript error handling](https://silmaril.security/docs/sdk/typescript.md#errors). Handle gateway authentication, model, and network errors separately. A failed request is not a benign verdict.

## Coverage

This example protects non-streamed generated text. On TypeScript SDK versions before 0.10.0, pass `scanOutput: false`, classify `response.text` explicitly, and stop on a blocking verdict before returning it, as shown in the [Vercel AI SDK guide](https://silmaril.security/docs/sdk/vercel-ai.md#ai-sdk-6). For AI SDK 5 and 6, `scanToolCalls: true` classifies generated tool-call arguments in non-streaming generation results only. It does not scan streamed tool-input parts; before executing a streamed tool call, separately await classification of its completed arguments with `HookLabel.TOOL_CALL` and stop on a blocking verdict, or use the [AI SDK 7 adapter](https://silmaril.security/docs/sdk/vercel-ai.md#ai-sdk-7) with `scanToolCalls: true` in Block mode to withhold them until their verdict. With AI SDK 6 streaming, generated text is classified after delivery, so use non-streaming generation or the AI SDK 7 adapter when blocking before delivery is required. Pass `parallel: true` to start the model call while its input is classified.

Every protected call must use the wrapped `model`. Configuring middleware in one application does not enable a gateway-wide guardrail for other applications or unwrapped calls.

* [Vercel AI Gateway documentation](https://vercel.com/docs/ai-gateway)
* [Vercel AI SDK and framework integrations](https://silmaril.security/docs/integrations/frameworks.md)

## Next steps

[Verify your integration](https://silmaril.security/docs/start/verify.md), then review [modes and enforcement](https://silmaril.security/docs/guides/enforcement.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.