> ## Documentation Index
> Fetch the complete documentation index at: https://silmaril.dev/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](/docs/sdk/typescript).
* 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, FirewallBlockedException, HookLabel } from "@silmaril-security/sdk"
import { gateway, generateText, wrapLanguageModel } from "ai"

const shadowMode = true

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

const model = wrapLanguageModel({
  model: gateway("anthropic/claude-sonnet-4.6"),
  middleware: fw.asMiddleware({ scanOutput: false }),
})

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

const result = await fw.classify(response.text, { hook: HookLabel.LLM_OUTPUT })
const blocked =
  result.prediction === "MALICIOUS" || result.governance?.action === "block"

if (blocked && !shadowMode) {
  throw new FirewallBlockedException({
    score: result.score,
    threshold: result.threshold,
    promptText: response.text,
    hook: HookLabel.LLM_OUTPUT,
    result,
  })
}

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.

## Verify and enforce

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

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

## Coverage

With AI SDK 6, use the explicit output check above instead of relying on the middleware's `scanOutput` option. This example protects generated text. Classify tool-call arguments before executing a tool, and buffer streaming output until classification finishes when blocking before delivery is required.

Every protected call must use the wrapped `model` and the output check. 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](/docs/integrations/frameworks)
