Prompt injection detection for Strands Agents
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 runtime security platform that ships with your code. Enforce budgets, stop prompt injection, detect bots, and protect personal information with Arcjet's AI security building blocks.Quick start
Section titled “Quick start”This example screens inbound user text for prompt injection before the model runs.
We assume you already have a Strands Agents project set up. For helper options and denial behavior, see the Strands Agents agent guard.
Install the dependencies:
# Export your Arcjet API key from https://console.arcjet.comexport ARCJET_KEY="ajkey_..."
npm install @arcjet/guard @strands-agents/sdk zodCreate the example:
import { launchArcjet, detectPromptInjection } from "@arcjet/guard";import { strandsAgentContext } from "@arcjet/guard/strands-agents/v1";
const arcjet = launchArcjet({ key: process.env.ARCJET_KEY! });const inbound = detectPromptInjection();
export async function screenPrompt(conversationId: string, userText: string) { const invocationState = { sessionId: conversationId }; const decision = await arcjet.guard({ label: "message.received", rules: [inbound(userText)], ...strandsAgentContext({ invocationState }), });
if (decision.conclusion === "DENY" || decision.hasFailedOpen()) { throw new Error("Prompt injection detected – rephrase your message"); }}Then start or invoke the agent with a test prompt.
Requests appear in your Arcjet dashboard in real time.
This example screens inbound user text for prompt injection before the model runs.
We assume you already have a Strands Agents project set up. For helper options and denial behavior, see the Strands Agents agent guard.
Install the dependencies:
# Export your Arcjet API key from https://console.arcjet.comexport ARCJET_KEY="ajkey_..."export ARCJET_ENV=development
pip install "arcjet[strands-agents]"Create the example:
import os
from arcjet.guard import DetectPromptInjection, launch_arcjetfrom arcjet.guard.strands_agents import strands_agent_context
arcjet = launch_arcjet(key=os.environ["ARCJET_KEY"])inbound = DetectPromptInjection()
async def screen_prompt(conversation_id: str, user_text: str) -> None: app_context = {"session_id": conversation_id} derived = strands_agent_context(app_context)
decision = await arcjet.guard( label="message.received", rules=[inbound(user_text)], correlation_id=derived.correlation_id, ) if decision.conclusion == "DENY" or decision.has_failed_open(): raise RuntimeError( "Prompt injection detected – rephrase your message" )Then start or invoke the agent with a test prompt.
Requests appear in your Arcjet dashboard in real time.
What next?
Section titled “What next?”Get help
Section titled “Get help”Need help with anything? Email support@arcjet.com to get support from our engineering team.