← Blog

Vercel AI SDK Prompt Injection: Control Every Agent Step

Parminder Singh
Parminder Singh··4 min read
Summarize with AI

Vercel AI SDK agents can add tool results and new context before each model step, so the first input check sees only part of the eventual prompt. Limit active tools, validate tool inputs, require approval for consequential actions, inspect each assembled model request, and keep application-side execution controls separate from the HTTP LLM boundary.

Problem-Awareai-securityagentic-aiprompt-injectionllm-securitypolicy-enforcement
Vercel AI SDK Prompt Injection: Control Every Agent Step

A Vercel AI SDK agent can begin with a clean user message and reach its third model call carrying a web result and tool error. It can also carry an earlier model response. Vercel's ToolLoopAgent documentation says each tool execution triggers another generation where the model can call a tool again or return text. Prompt-injection review therefore belongs at every generation step, after the application has assembled the current context.

TL;DR

  • Treat every ToolLoopAgent generation as a new prompt-injection boundary because tool results can change the next model request.
  • Use activeTools and strict input schemas to reduce what a compromised step can do. Require server-side approval policy for consequential actions.
  • Inspect the complete authenticated HTTP request before each model call, with identity and route details plus provenance attached.
  • Keep tool execution and approval integrity under application control. Apply the same rule to downstream output encoding.

The dangerous prompt is assembled after the first turn

OWASP separates direct injection from indirect prompt injection. The indirect form arrives through external sources such as files and websites. In an AI SDK agent, tool results create the same trust problem: text returned by search, email, a ticketing system, or an MCP server can become context for the next generation.

The first user message may occupy one line on the incident screen. By step three, the model can see several pages of retrieved text. It can also see a tool result containing an instruction to ignore the original task. Checking only the API route's initial input misses the payload that matters.

Record the source of every context segment. The assembled request should identify user text and server instructions separately. It should do the same for retrieved documents and tool results. Flattening them into one anonymous message destroys the evidence needed to explain why the model chose its next action.

This is the Vercel-specific implementation of the broader prompt-injection defense-in-depth model: inspect the actual context sent at each model boundary, rather than trusting the run's opening message.

Tool exposure should change at each step

Vercel's tool-calling documentation states that all registered tools are active by default. The activeTools property limits which named tools are available for a generation. toolChoice controls automatic, required, disabled, or named selection.

Use that control as a capability budget. A retrieval step needs read-only search, so expose only that capability. Keep sendEmail and deleteFile out of the model's tool set. Keep processPayment out as well. After untrusted content enters the context, shrink the active set again. This is also the practical value of AI agent tool scoping: a successful injection inherits only the tools exposed to that step.

Strict input schemas validate the shape of a generated call when the provider supports strict tool calling. Business intent remains unchecked. A perfectly formed payment request can still name an attacker-controlled recipient. Authorization must follow schema validation.

I would reject an agent design that registers every tool once and leaves the full set active for twenty steps. That arrangement turns a framework default into the application's authorization policy.

Tool approval needs server-side policy

Vercel documents that tools with an execute function run automatically by default. Its Tool Approvals guide lets ToolLoopAgent make an automatic decision or request a user's decision before execution. An automatic decision can approve or deny the call. Approval functions can inspect the parsed input and runtime context. They can also inspect messages and the complete tool call.

That policy should read authenticated server-side facts. A user's role and tenant belong in trusted runtime context. The same applies to the transaction limit and destination allowlist. The model can propose a call. Code decides if it runs.

The same guide draws two sharp boundaries. Provider-executed tools bypass AI SDK tool approvals, and the standard useChat pattern treats client-supplied message history as controlled by the client. Sensitive approvals need the documented signing secret so a forged or modified approval fails before execution.

Reserve human approval for consequential actions. Applying it to every read creates review fatigue, which is an attack surface. Automatic denial for forbidden operations produces a cleaner queue. So does explicit approval for unusual but permitted operations.

Response handling remains an application responsibility

Prompt injection can influence ordinary text as well as tool calls. A model response placed into HTML, SQL, a shell, or an outbound URL reaches a separate sink with its own encoding and authorization rules. Inspecting the model response can classify suspicious content, but the application still owns destination-specific handling.

Keep the records separate and correlated. The model request record should show identity and route. It should also show context provenance, model, and policy decision. A tool record should show the selected function and validated arguments. It should also show the approval result and execution outcome. The AI agent observability model becomes useful during an incident only when those two records share a run and step identifier.

This split also prevents a common architecture mistake: treating a clean model-request decision as permission to execute whatever follows. A routed prompt decision governs transmission to the LLM. Tool authorization governs an application-side side effect.

DeepInspect

DeepInspect can inspect authenticated HTTP requests that a Vercel AI SDK application deliberately routes to an LLM endpoint. At each routed generation, the application can supply user identity and role. It can also supply route and data-classification context. DeepInspect evaluates that assembled model request before transmission and records the request and response policy decision.

ToolLoopAgent execution and activeTools remain outside that HTTP boundary. So do approval signatures, provider-executed tools, and sink encoding. Local-tool authorization and context provenance stay with the application. Direct model calls that bypass the routed endpoint bypass the gateway as well.

Book a demo today.

Frequently asked questions

Does `activeTools` prevent prompt injection?

activeTools limits the damage available to a compromised generation. It removes tools from the set presented to the model for that step. The injected text can still alter model output, so teams also need provenance and request inspection. They need response handling and approval policy as well.

Does strict tool calling authorize the generated action?

Strict tool calling checks whether the call conforms to the declared input schema when the selected provider supports it. Authorization asks if this identity may perform this action with these arguments under current policy. A schema-valid call can still violate tenant and role rules. It can also violate amount or destination rules.

Can one input filter protect the whole agent loop?

One input filter sees the opening context. Later tool results and retrieved content can introduce new instructions before another generation. Evaluate the assembled request at each model call and tie the decision to its run and step. Record the context sources.