Prompt injection detection for Claude Managed 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 Claude Managed Agents project set up. For helper options and denial behavior, see the Claude Managed Agents agent guard.
Install the dependencies:
# Export your Arcjet API key from https://console.arcjet.comexport ARCJET_KEY="ajkey_..."
npm install @arcjet/guard@git+https://github.com/arcjet/arcjet-js.git#cb35c8f92c3a2fb63fbeb9b386d79b1878c19d92 @anthropic-ai/sdkCreate the example:
import Anthropic from "@anthropic-ai/sdk";import { launchArcjet, detectPromptInjection } from "@arcjet/guard";import { claudeManagedAgentsContext, guardEvents,} from "@arcjet/guard/claude-managed-agents/v0";
const arcjet = launchArcjet({ key: process.env.ARCJET_KEY! });const client = new Anthropic();
// sessionId is the Anthropic session `id` from// `client.beta.sessions.create`. conversationId is your own id, which is// what Arcjet correlates on.export async function sendTurn( sessionId: string, conversationId: string, userText: string,) { // guardEvents screens the turn and only then sends it, so an injected // prompt never reaches the session. const inbound = await guardEvents( arcjet, { events: [ { type: "user.message", content: [{ type: "text", text: userText }] }, ], inbound: { action: "message.received", rules: ({ text }) => [detectPromptInjection()(text)], }, context: claudeManagedAgentsContext({ correlationId: conversationId }), }, (body) => client.beta.sessions.events.send(sessionId, body), );
if (!inbound.allowed) { throw new Error(inbound.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 Claude Managed Agents project set up. For helper options and denial behavior, see the Claude Managed 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[claude-managed-agents]" anthropicCreate the example:
import os
from anthropic import AsyncAnthropicfrom arcjet.guard import ( ArcjetDeniedError, DetectPromptInjection, launch_arcjet,)from arcjet.guard.claude_managed_agents import guard_events
arcjet = launch_arcjet(key=os.environ["ARCJET_KEY"])
# guard_events runs an inbound check before each user.message reaches the# session, so use the async client: the sync one can't be awaited here.client = AsyncAnthropic()inbound = DetectPromptInjection()
# guard_events wraps send. On DENY it raises and never calls the real send,# so an injected prompt never reaches the session.send = guard_events( guard=arcjet, send=client.beta.sessions.events.send, action="message.received", rules=lambda arguments: [inbound(arguments["prompt"])],)
# session_id is the Anthropic session id from client.beta.sessions.create.async def send_turn(session_id: str, user_text: str) -> bool: try: await send( session_id, events=[ { "type": "user.message", "content": [{"type": "text", "text": user_text}], } ], ) except ArcjetDeniedError: return False return TrueThen 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.