Skip to content

Prompt injection detection for Vercel AI SDK

Arcjet prompt injection detection evaluates each incoming prompt for injection patterns inside your application before it reaches the AI provider. Detected attacks are blocked before the AI call is made, protecting both your application behavior and your AI budget.

What is Arcjet? Arcjet is the AI agent runtime security platform. Discover the agents running in your organization, enforce policy across every action, prompt, and tool call, and keep the evidence to prove what happened. Detect prompt injection, authorize agent tool calls, redact PII, and block bots and abuse.

This example screens inbound user text for prompt injection before the model runs.

We assume you already have a Vercel AI SDK project set up. For helper options and denial behavior, see the Vercel AI SDK agent guard.

Install the dependencies:

Terminal window
# Export your Arcjet API key from https://console.arcjet.com
export ARCJET_KEY="ajkey_..."
npm install @arcjet/guard ai @ai-sdk/provider-utils

Create the example:

agent.ts
import { launchArcjet, detectPromptInjection } from "@arcjet/guard";
import { generateText } from "ai";
const arcjet = launchArcjet({ key: process.env.ARCJET_KEY! });
const inbound = detectPromptInjection();
export async function runAgent(userId: string, prompt: string) {
const decision = await arcjet.guard({
label: "message.received",
actor: userId,
rules: [inbound(prompt)],
});
if (decision.conclusion === "DENY" || decision.hasFailedOpen()) {
throw new Error("Prompt injection detected – rephrase your message");
}
return generateText({
model: "openai/gpt-4o-mini",
prompt,
});
}

Then start or invoke the agent with a test prompt.

Requests appear in your Arcjet dashboard in real time.

Need help with anything? Email support@arcjet.com to get support from our engineering team.

Discussion