Tawa docs
Integrations

Vercel AI SDK

Wrap a model once — every generateText/streamText call is scanned before it reaches the provider.

@tawaguard/sdk/ai-sdk wraps a Vercel AI SDK language model via wrapLanguageModel, so every call using it is scanned by Tawa before it reaches the provider. It's a separate subpath export — ai and @ai-sdk/provider stay optional peer dependencies, so nothing pulls them in unless you actually use this integration.

Install

npm install @tawaguard/sdk ai @ai-sdk/openai

Requires ai@^7 and @ai-sdk/provider@^4 as peer dependencies. If your app already uses an older Vercel AI SDK major version elsewhere, upgrade it first — npm install will not silently downgrade it for you.

Changed in 0.3.0: scan errors fail open by default

From 0.3.0 the middleware fails open on every scan it makes. If the Tawa request fails (gateway down, network error, timeout, a non-2xx reply), the error goes to onError and the call proceeds unscanned. This includes the prompt scan and the response scan, which threw in 0.2.x. To restore the 0.2.x behaviour, set failClosed: true. A genuine block (TawaBlockedError) is never affected by this option. See Gateway and network errors.

Wrap a model once

import { openai } from "@ai-sdk/openai";
import { generateText } from "ai";
import { TawaGuard } from "@tawaguard/sdk";
import { TawaBlockedError, tawa } from "@tawaguard/sdk/ai-sdk";

const tawaClient = await TawaGuard.create(process.env.TAWA_API_KEY!);
const model = tawa(openai("gpt-5"), tawaClient);

export async function POST(req: Request) {
  const { messages } = await req.json();

  try {
    const { text } = await generateText({ model, messages });
    return Response.json({ text });
  } catch (error) {
    if (error instanceof TawaBlockedError) {
      return new Response("Message blocked by content policy.", { status: 403 });
    }
    throw error;
  }
}

Use model anywhere you'd normally pass the raw provider model — every generateText/streamText call using it is scanned automatically. No manual analyze() call needed at each call site.

What gets scanned, and when

By default, the whole assembled prompt (system message + conversation history + the latest turn) is scanned via analyze (provenance user_prompt) in a transformParams hook, before the call reaches the model, not just the newest user message, because injected content can sit anywhere in prior history. Set promptProvenance: "assembled_context" to scan it with scanContext instead.

The prompt scan reads type: "text" parts only. Tool results are a different part type, so the prompt scan does not see them. From 0.3.0 the middleware scans them separately, and scans the tool calls the model emits (see Tool scanning). On 0.2.x, and with both tool options off, tool results and anything you fetch for RAG are not scanned by this integration: call scanChunk (RAG) or scanToolOutput (agent tools) yourself, inside the tool's execute function, before returning the content to the model.

A blocked or leaning-toward-blocked result throws TawaBlockedError instead of letting the call reach the model:

export class TawaBlockedError extends Error {
  readonly result: ScanResult; // the result that tripped it
  readonly stage: TawaStage;
  readonly toolName?: string; // set for the "tool-output" and "tool-call" stages
}

type TawaStage = "prompt" | "response" | "tool-output" | "tool-call";
stageWhat was scanned
"prompt"The assembled prompt, before the model runs
"response"The model's reply (only with scanResponses)
"tool-output"A tool result in the prompt, before the model reads it
"tool-call"A tool call the model emitted, before the tool runs

Available from 0.3.0

"tool-output", "tool-call", toolName and the exported TawaStage type are new in 0.3.0. The stage type of TawaBlockedError, shouldBlock and onReview widened from "prompt" | "response" to TawaStage. A callback typed against the old union must accept the two new values to compile.

The default blocking policy: always block on block/hard_block/ soft_block, never on allow/ood, and defer to the server's own suggestion for the genuinely undecided actions (ambiguous/review/ sandbox). Override it with shouldBlock (see below).

The gateway already collapses review to allow or block per your app's setting, so against a current gateway the result's action is allow or block and the default policy follows it directly. The undecided branch only applies to a gateway that predates the review flag. To act on uncertain results, use onReview.

Blocked messages persist in conversation history

Once a message in a conversation is blocked, every later message in that same conversation is blocked too — even ones with no malicious content of their own.

This is expected, not a detection bug, and follows from two other documented behaviors:

  1. The Vercel AI SDK's useChat resends the entire message history on every turn, not just the newest message.
  2. tawa() scans the whole assembled prompt on every call (see What gets scanned, and when), so an injection planted earlier in the conversation is still caught even when the current turn looks innocent.

A blocked tool result behaves the same way: it stays in history, is scanned again on every attempt, and blocks again.

A blocked message's text stays in messages history. Every later call resends it, tawa() rescans it, and it re-trips the block — so the conversation stays blocked until you intervene. Handle it one of two ways:

Option 1: lock the conversation

Once any message in a thread has been blocked, stop accepting new input and require a fresh conversation (new sessionId, empty history).

app/api/chat/route.ts
// Server: tag the response so the client can detect a blocked turn.
export async function POST(req: Request) {
  const { messages } = await req.json();

  try {
    const { text } = await generateText({ model, messages });
    return Response.json({ text });
  } catch (error) {
    if (error instanceof TawaBlockedError) {
      return Response.json({ blocked: true, reason: error.result.rationale }, { status: 403 });
    }
    throw error;
  }
}
// Client: gate the composer once any message in the thread was blocked.
const locked = messages.some((m) => m.metadata?.blocked === true);

<ChatInput disabled={locked} />
{locked && <Button onClick={startNewConversation}>Start new chat</Button>}

Option 2: strip the blocked exchange and continue

Drop the blocked user message and its "blocked" reply from what gets sent to tawa()/the model on the next turn, while still showing them in the UI transcript.

// Drop blocked exchanges before they're resent to tawa()/the model.
function stripBlocked(messages: UIMessage[]) {
  return messages.filter((m) => !m.metadata?.blocked);
}

const { text } = await generateText({ model, messages: stripBlocked(messages) });

This isn't a security regression: a message that was already blocked never reached the model, and rescanning the same rejected text on every later turn adds no protection — only false positives on unrelated later messages.

Lock the conversationStrip and continue
A false positiveUser must start a new chatConversation continues unaffected
An attacker retryingMust start a new session — gives you a clean per-session signal to rate-limit or alert onFree, unlimited attempts within one session
Security impactNoneNone

Neither is more "correct" — pick based on your own risk tolerance.

tawa() has no built-in concept of "this conversation is now poisoned" for either option — check for a blocked turn yourself (keyed off scanOptions.sessionId, which the SDK also uses for the live event stream).

Tool scanning

Available from 0.3.0

scanToolOutputs, scanToolCalls and failClosed are new in 0.3.0.

A tool result is where indirect prompt injection enters an agent: a web page, document or API response that a tool returns and the model then reads. The middleware scans tool traffic by default, on top of the prompt.

OptionDefaultWhat it scans
scanToolOutputstrueTool results in the prompt, with scanToolOutput and the tool's name, before the model reads them. A block throws TawaBlockedError with stage: "tool-output"
scanToolCallstrueTool calls the model emits, with scanToolCall (tool name and parsed arguments), before the tool runs. A block throws TawaBlockedError with stage: "tool-call"
// Prompt scanning only, as in 0.2.x
const model = tawa(openai("gpt-5"), tawaClient, {
  scanToolOutputs: false,
  scanToolCalls: false,
});
  • Tool outputs. Text and JSON outputs are scanned, both the typed { type, value } outputs and a raw output or result value. File and image content is ignored and an execution-denied output is skipped. Results in tool messages and provider-executed results in assistant messages are both read.
  • Not rescanned. A tool result is scanned once per middleware instance. Later steps of a multi-step turn resend history, and results that already cleared are not scanned again. A blocked result is scanned again on every attempt.
  • Tool calls with generateText. The call is scanned after the model returns and before the result is handed back, so before any tool executes.
  • Tool calls with streamText. The complete tool-call part is held until its scan clears, and any pending text is flushed first so order is kept. The AI SDK runs the tool when it sees that part, so nothing executes before the scan. The incremental tool-input-* parts stream through unscanned. A block ends the stream with an error part carrying the TawaBlockedError.
  • Review flag. onReview fires for tool results and tool calls with needsReview, exactly as for prompts. shouldBlock and policy apply to both stages.

shouldBlock and onReview receive the tool name as an optional fourth argument for the two tool stages:

const model = tawa(openai("gpt-5"), tawaClient, {
  onReview: async (result, stage, text, toolName) => {
    await reviewQueue.add({ stage, toolName, text, risk: result.risk });
  },
});

Cost

Each distinct tool result is one extra scan and each tool call is one more, so a turn with several tool steps makes several scans on top of the prompt scan. Each scan counts toward your usage. Set scanToolOutputs: false and/or scanToolCalls: false if you only want prompt scanning. With the defaults, streamText is wrapped even when scanResponses is off so tool calls can be checked; text still streams through unscanned in that case and backpressure is kept.

What is not covered

  • Tool calls the provider executes itself (providerExecuted) are not scanned before they run, because the provider has already run them. Their results are scanned when they reappear in the prompt on the next step.
  • Only the text of a tool result is scanned, not files or images.
  • Streamed tool arguments are visible to the caller as tool-input-* parts before the call is scanned.
  • Prompt shapes are tested against ai 7.0.54 and @ai-sdk/provider 4.0.5 (language model spec V4), where results are tool-result parts with a typed output. A raw output or result value (the older shape) is read too, but only V4 is covered by tests.

Gateway and network errors

By default the middleware fails open. If a scan itself fails (gateway down, network error, timeout, a non-2xx reply), the error goes to onError and the call proceeds unscanned, so a Tawa outage does not take your app down. Set failClosed: true to throw the original error instead (generateText) or emit it as the stream's error part (streamText).

const model = tawa(openai("gpt-5"), tawaClient, {
  failClosed: true, // 0.2.x behaviour: a failed scan aborts the call
  onError: (error) => logger.warn("tawa scan failed", error),
});

This applies to every scan the adapter makes: prompt, response, tool output and tool call. See Scanning tool outputs for which setting to pick.

Scanning the model's reply too

Off by default — it doubles latency/cost per call, and the primary threat model (prompt injection) lives on the input side. Turn it on with scanResponses: true, for both generateText and streamText:

const model = tawa(openai("gpt-5"), tawaClient, { scanResponses: true });

On streamText, the reply is scanned in windows rather than all at once — a chunk of text is only forwarded to the caller once the window it belongs to has cleared a scan (see Streaming caveat for how a block surfaces). Window size defaults to 200 characters; tune it with streamScanWindowChars:

const model = tawa(openai("gpt-5"), tawaClient, {
  scanResponses: true,
  streamScanWindowChars: 500, // scan less often, at the cost of more unscanned text in flight
});

A smaller window bounds how much of a blocked reply can reach the caller before the stream is cut, at the cost of more scanResponse calls per reply. A larger window scans less often but lets more text through before each check.

Options

Pass an options object as the third argument to tawa() (or to tawaMiddleware() if you're composing with other AI SDK middleware via wrapLanguageModel({ middleware: [...] })):

const model = tawa(openai("gpt-5"), tawaClient, {
  shouldBlock: (result) => result.action !== "allow", // stricter than the default
  scanResponses: true,
  extractPromptText: (prompt) => /* custom extraction */,
  scanOptions: { cuid: currentUser.id, sessionId },
});
OptionTypeDefaultDescription
shouldBlock(result: ScanResult, stage: TawaStage, text: string, toolName?: string) => booleansee aboveDecide whether a scan result should block the call. toolName is set for the two tool stages
scanResponsesbooleanfalseAlso scan the model's reply before it's returned, on both generateText and streamText
scanToolOutputsbooleantrueScan tool results in the prompt before the model reads them. New in 0.3.0. See Tool scanning
scanToolCallsbooleantrueScan tool calls the model emits before the tool runs. New in 0.3.0
failClosedbooleanfalseThrow on a failed scan instead of proceeding unscanned. New in 0.3.0. See Gateway and network errors
streamScanWindowCharsnumber200Streaming only — size (in characters) of each text window scanned before it's forwarded to the caller
extractPromptText(prompt) => stringscans the whole promptBuild the text handed to Tawa from the AI SDK's internal prompt array
scanOptionsScanOptions—Forwarded as-is to every scan call (cuid, tenantId, policyOverrides, language, sessionId)
onReview(result: ScanResult, stage: TawaStage, text: string, toolName?: string) => void | Promise<void>undefinedCalled once per scan whenever result.needsReview is true, whether or not the scan blocks, before the block decision. Errors thrown inside it go to onError, or are dropped when onError is not set. See The review flag
onError(error: unknown) => voidundefinedReceives an error thrown by onReview and, unless failClosed is set, the error from a failed scan the middleware failed open on
policy"org-active" | BuiltInPolicyTemplateIdundefined (off)Make blocking policy-aware — see below

Policy Studio integration (policy)

The default blocking policy above only looks at the scan's raw action/ suggestion — it never reads ScanResult.policy, the verdict from your org's Policy Studio rules. That verdict is already computed server-side on every scan regardless of this option; by default it's simply discarded by shouldBlock.

Set policy to turn this on — off by default, so existing integrations are unaffected:

// Block using your org's already-active Policy Studio policy, on top of
// the existing hard-block safety floor. No extra network call — that
// policy is already evaluated server-side on every scan.
const model = tawa(openai("gpt-5"), tawaClient, { policy: "org-active" });

// Additionally layer a built-in template's rules on top of your org's
// policy, for this integration point specifically.
const model = tawa(openai("gpt-5"), tawaClient, { policy: "strict-zero-trust" });

A built-in template ("baseline-protection" | "strict-zero-trust" | "tool-heavy-agent") is additive, not a replacement — its rules are unioned with your org's active policy server-side, so it can only make blocking stricter, never loosen your org's real policy.

policy and shouldBlock are mutually exclusive — tawa()/ tawaMiddleware() throw synchronously if both are set. If you need custom logic combined with the policy verdict, write your own shouldBlock that calls the exported policyShouldBlock (or reads result.policy directly) instead of setting policy.

Streaming caveat

streamText() itself never throws — it returns synchronously and runs the pipeline in the background, so a block (on the prompt, on a tool result, on a tool call, or on the response when scanResponses is on) surfaces as an error part inside the stream (visible via result.fullStream, or in toUIMessageStreamResponse()'s output), not as a catchable exception around the streamText() call or a top-level 403. Handle it via the stream's error channel rather than try/catch if you need to react to it.

This is easy to get wrong in a way that hides the block entirely, not just handles it differently. toUIMessageStreamResponse()'s onError option defaults to () => "An error occurred." — every error, including TawaBlockedError, is masked to that generic string unless you supply onError yourself. Without it, a blocked message reaches the browser as "An error occurred.", indistinguishable from any other crash: no action, no confidence, no rationale, no signal it was Tawa at all. Write the onError below yourself — there is no default that surfaces the block.

app/api/chat/route.ts
import { openai } from "@ai-sdk/openai";
import { TawaGuard } from "@tawaguard/sdk";
import { TawaBlockedError, tawa } from "@tawaguard/sdk/ai-sdk";
import { streamText } from "ai";

const tawaClient = await TawaGuard.create(process.env.TAWA_API_KEY!);
const model = tawa(openai("gpt-5"), tawaClient);

export async function POST(req: Request) {
  const { messages } = await req.json();

  const result = streamText({ model, messages });

  return result.toUIMessageStreamResponse({
    onError: (error) =>
      error instanceof TawaBlockedError
        ? `blocked:${error.result.action}:${error.result.rationale}`
        : "An unexpected error occurred.",
  });
}

On the client, useChat()'s error.message will be that string — parse the blocked: prefix to render it distinctly from a real failure. This is not optional boilerplate you can skip for a first pass: skip it and every block your users hit looks like your app broke, not like it protected them.

A block on streamText cannot short-circuit to an HTTP-level rejection before any bytes are sent — it always surfaces through the stream's error channel, as shown above.

On this page