Skip to content

Content moderation for CrewAI

Arcjet content moderation detects harmful content in untrusted text before it is stored, displayed, or forwarded. It is a Guard rule – call it from guard() / Guard, not protect().

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 text with content moderation before the agent continues.

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

Install the dependencies:

Terminal window
# Export your Arcjet API key from https://console.arcjet.com
export ARCJET_KEY="ajkey_..."
export ARCJET_ENV=development
pip install "arcjet>=1.0.0" "crewai>=1.15.3,<2"

Create the example:

agent.py
import os
from arcjet.guard import ModerateContent, launch_arcjet_sync
# CrewAI hooks are synchronous, so use launch_arcjet_sync.
arcjet = launch_arcjet_sync(key=os.environ["ARCJET_KEY"])
moderate = ModerateContent()
def screen_prompt(user_text: str) -> None:
decision = arcjet.guard_sync(
label="message.received",
rules=[moderate(user_text)],
)
if (
decision.conclusion == "DENY"
and decision.reason == "MODERATE_CONTENT"
):
raise RuntimeError(
"Harmful content detected – rephrase your message"
)

Then start or invoke the agent with a test prompt.

Requests appear in your Arcjet dashboard in real time.

Terminal window
npm install @arcjet/guard
import { launchArcjet, moderateContent } from "@arcjet/guard";
const arcjet = launchArcjet({ key: process.env.ARCJET_KEY! });
const moderate = moderateContent();
const decision = await arcjet.guard({
label: "tools.chat",
rules: [moderate(userMessage)],
});
if (decision.conclusion === "DENY" && decision.reason === "MODERATE_CONTENT") {
throw new Error("Harmful content detected – rephrase your message");
}
const result = moderate.result(decision);
// `detected` is true when harmful content was found. Billing is undefined
// when the service does not report usage. Content moderation uses text_units.
console.log(result?.detected, result?.billing?.unit, result?.billing?.count);

Keep the response generic. Do not leak detector details or explain exactly what was flagged.

Discussion