← Back to Learn
sdkguardrailsbest-practicestutorial

Vercel AI SDK Safety Patterns

Authensor

The Vercel AI SDK powers many production chat applications and AI features. Its streaming-first architecture requires safety patterns that work with partial responses. This guide covers how to integrate Authensor safety checks while preserving the streaming user experience.

Input Validation

Check user messages before they reach the model. In your API route handler, pass the user's message through Authensor's Aegis scanner before calling the AI SDK's streamText or generateText functions.

import { AuthensorClient } from '@authensor/sdk';

const client = new AuthensorClient({ url: process.env.AUTHENSOR_URL });

export async function POST(req: Request) {
  const { messages } = await req.json();
  const lastMessage = messages[messages.length - 1];

  const scan = await client.scan({ content: lastMessage.content });
  if (scan.blocked) {
    return new Response('Message blocked by safety policy', { status: 400 });
  }

  // proceed with AI SDK streaming
}

Tool Call Safety

The Vercel AI SDK supports tool calling. Wrap your tool definitions with Authensor's safety layer to evaluate each tool call against your policy before execution.

Use the experimental_toToolResultContent pattern to intercept tool calls, run them through policy evaluation, and return either the tool result or a denial message.

Streaming Output Monitoring

For streaming responses, apply lightweight safety checks on the accumulated text buffer at regular intervals. Set up a transform stream that buffers chunks, runs regex-based content checks, and forwards approved content.

Heavy content classification runs after the stream completes. If the full response fails a safety check, you can log the violation and take corrective action (like sending a follow-up message) even though the response was already streamed.

Middleware Pattern

Create a reusable middleware function that wraps your AI route handlers with safety checks. This keeps safety logic centralized rather than duplicated across routes.

The middleware handles input scanning, tool call authorization, output monitoring, and audit logging. Individual route handlers focus on their specific AI logic.

Rate Limiting

Use Authensor's policy engine to enforce per-user rate limits on AI requests. This prevents abuse without building separate rate limiting infrastructure. The policy can limit requests per minute, total tokens per hour, or specific tool calls per session.

Error Handling

When safety checks block a request, return a user-friendly message. Never expose internal policy details or detection mechanisms in error responses. Log the full context to the audit trail for investigation.

Keep learning

Explore more guides on AI agent safety, prompt injection, and building secure systems.

View All Guides