Best Practices Guide

Vercel AI SDK and Next.js Agent Security Checklist

Securing a Vercel AI SDK agent on Next.js requires layered controls across client, server action, tool-call, model provider, and deployment boundaries, since the SDK validates tool input structure but not authorization, and Next.js does not restrict who can invoke a Server Action or Route Handler by default. Teams should verify identity checks at every execution boundary, scope tool permissions to the minimum required, isolate credentials per tool, sanitize streamed outputs before use, and add runtime logging and enforcement where the SDK and framework leave gaps.

Five Layers of Agent Security Review

Each layer of a Vercel AI SDK and Next.js agent introduces its own trust boundary. A complete review checks all five independently rather than assuming controls at one layer cover another.

Layer 1

Client

Where streamed model output and tool-call arguments first reach the user.

Layer 2

Server Action / Route Handler

Externally reachable execution boundaries with no implicit access control.

Layer 3

Tool / Function Call

Developer code executed on model request, running with application-level privileges.

Layer 4

Model Provider

API keys, credential scope, and structured output validation.

Layer 5

Deployment

Runtime logging, policy enforcement, and audit coverage across environments.

Security Review Checklist

Use this list to evaluate an existing Vercel AI SDK and Next.js agent implementation before it reaches production.

  • Confirm streamed tool-call arguments (via streamText or streamUI) are validated before being rendered or acted upon, not just displayed as received
  • Verify partial or malformed intermediate tool-call data cannot be executed client-side before server-side validation completes
  • Check that model provider API keys are never passed to or reconstructable from client bundles
  • Confirm any client-rendered structured output still passes through schema validation server-side, since client rendering is not a trust boundary
  • Verify every Server Action that triggers agent tool execution independently checks session identity and authorization inside the function body
  • Confirm Route Handlers used for agent endpoints enforce the same authentication regardless of Node.js or Edge runtime
  • Check that Edge Middleware, given its restricted runtime, is not the sole point of authentication for agent-invoking routes
  • Confirm no Server Action or Route Handler assumes it is "private" because it is not directly linked from the UI
  • Confirm each tool's input schema (Zod or JSON Schema) validates structure, and confirm a separate authorization check validates permission to perform the action
  • Review each tool definition for excessive agency: does it expose more functionality or data access than the agent's specific task requires
  • Verify multi-step tool chaining (maxSteps or stopWhen) applies an authorization check at each step, not only at the initial request
  • Confirm tool execution logic uses per-user or per-session scoped credentials for downstream systems rather than shared service-level credentials

Security Review Detail

What This Checklist Covers

This checklist is organized by architectural layer rather than by generic AI risk category, because Vercel AI SDK and Next.js implementations fail in specific, structural ways. The AI SDK's tools parameter in generateText and streamText lets developers define functions with a description, an input schema, and an execute function that runs when the model requests it. That execute function runs with the same privileges as the surrounding application code. The SDK does not perform authorization checks, rate limiting, or sandboxing by default. Next.js contributes its own boundary conditions: Server Actions are exposed as public HTTP endpoints regardless of how they are called in application code, and Route Handlers running on Edge versus Node.js runtimes have different available security libraries. Reviewing an agent implementation means checking each of these boundaries independently, not assuming that passing a schema validation step is sufficient.

Model Provider and Credential Handling

Model provider API keys and any downstream service credentials used inside tool execute functions should be scoped server-side only. The AI SDK's client-server split does not automatically prevent key exposure; misconfiguration, such as passing provider clients into client components, can still leak credentials. Structured output generation through generateObject or streamObject validates model output against a provided schema, but this covers shape and type conformance only. It does not verify that the values are semantically correct or that the requesting user is authorized to receive or act on that output. Teams should treat schema validation as a baseline control and pair it with explicit authorization logic wherever tool output triggers a state-changing action, such as a database write, external API call, or financial transaction.

Schema validation is not authorization

A tool call can be structurally valid and still be an action the requesting user should not be permitted to perform. Treat these as two separate checks, not one.

Deployment and Runtime Auditability

Neither the AI SDK nor Next.js provides centralized audit logging of tool calls across a deployment. This is an application or platform-layer responsibility. In practice, this means a team can pass every item on a static code checklist and still lack visibility into which tool calls executed in production, with what arguments, on whose behalf, and with what outcome. OWASP's Top 10 for Large Language Model Applications identifies excessive agency, insecure output handling, and insufficient input validation as risk categories directly applicable to tool-calling architectures like this one. Static code review and schema validation address a portion of these risks. They do not address runtime enforcement: intercepting a tool call before execution, applying a least-privilege policy at that moment, and recording the decision for later audit. This is the point where code-level controls reach their limit and runtime governance tooling becomes necessary, particularly for regulated environments where tool-call history must be reconstructable after the fact.

Baseline Practices Before Production

  • Treat every Server Action and Route Handler that an agent can trigger as a public endpoint requiring its own authentication check
  • Scope each tool to the minimum data and system access its task requires, and document that scope explicitly per tool definition
  • Separate structural validation (schema conformance) from authorization validation (permission to perform the action) as two distinct checks
  • Assign distinct, narrowly scoped credentials per tool rather than a single shared service credential across all agent tool calls
  • Sanitize and validate streamed tool-call arguments before they are rendered to users or passed into execution
  • Establish a runtime logging mechanism for tool calls independent of the SDK, since none is provided by default

Close the Gap Between Code Review and Production Enforcement

Static checklists and schema validation reduce risk but do not provide runtime authorization, tool approval workflows, or audit logging once an agent is deployed. Trussed AI provides runtime governance for enterprise AI agents, including agent permissions, least privilege enforcement, and audit logging at the point of tool execution.

Explore Runtime Governance