Skip to content

Introduction to Arcjet content moderation for SvelteKit

Arcjet content moderation evaluates untrusted text for harmful content before it is stored, displayed, or forwarded to another service. Use it on user messages, tool-call results, and model outputs at the same action boundary as Agent guards.

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.

Content moderation is a Guard rule. There is no protect() / HTTP-request variant. Call it from guard() in JavaScript or Python, or Guard in Go.

Use content moderation when you need a decision on text that is about to leave your control or be shown to a user, for example:

  • User-facing chat and support assistants – block harmful messages before they are stored or displayed.
  • Agent tool results and model outputs – scan generated text before it is forwarded to another tool, a customer, or a third-party API.
  • Moderation at the action boundary – the same guard() call can combine content moderation with rate limiting or prompt injection detection.

Content moderation answers a different question from prompt injection detection (hostile instructions aimed at the model) and sensitive information detection (PII and other regulated data). Combine them when a single untrusted string needs more than one check.

  1. You configure moderateContent() (JavaScript), ModerateContent() (Python), or GuardModerateContent (Go) once and reuse the rule.
  2. At the action boundary, bind the untrusted text and submit it with guard() / Guard.
  3. Arcjet returns ALLOW or DENY. A denial has reason MODERATE_CONTENT.
  4. The per-rule result is a detected / Detected verdict, the categories that fired, and optional billing in text_units.

The result lists which categories fired as detectedCategories (JavaScript), detected_categories (Python), or DetectedCategories (Go): SEXUALLY_EXPLICIT, MISCONDUCT, HATE_SPEECH, and INSULTS. Arcjet may add categories later, so treat an unrecognized value as harmful content of an unknown kind. Categories arrive in a fixed order, not by score. Scores and thresholds are never returned. Treat detected / Detected as authoritative: the list can be empty even when harmful content was detected, for example from an older Arcjet server.

For what the default policy enforces and why it focuses on sexual content for payment processor compliance, see the content moderation policy.

SDKPublic APINotes
JavaScript / TypeScriptmoderateContent()mode is optional and defaults to LIVE.
PythonModerateContent()mode is optional and defaults to LIVE.
GoGuardModerateContentMode is required (ModeLive or ModeDryRun). An empty Mode returns ErrInvalidMode.

Configure the rule once, bind the untrusted text, and submit it with guard() / 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.reason === "ERROR" || decision.hasFailedOpen()) {
console.warn("Moderation did not complete", decision.id, decision.errorResults().map((result) => result.code));
throw new Error("Content check could not complete");
}
if (decision.conclusion !== "ALLOW") {
throw new Error("Message blocked by policy");
}
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);
// `detectedCategories` names what was found, such as "HATE_SPEECH" or
// "INSULTS". Decide on `detected`: the list can be empty even when it is true.
console.log(result?.detectedCategories);

For install steps and framework-specific wrappers, see the content moderation quick start.

mode: "DRY_RUN" / ModeDryRun evaluates the rule without blocking. Use this to measure detections before switching to "LIVE".

A moderation denial tells your application that policy blocked the message. When a required live check fails to finish, Guard also returns DENY, with reason ERROR. In either case, stop before storing or forwarding the text. The error results help you decide whether the caller should retry or correct the input. Use errorResults() in JavaScript, error_results() in Python, and ErrorResults() in Go. Go can also return a Go error alongside the decision.

Moderation reads the first 60 KB of a message. Longer text produces warning AJ1142, and the rest of the message goes unchecked. If you need to check the entire message, split it into parts and wait for an allow for each part before using the text. Inputs over 128 KiB are rejected in live mode and produce an error in dry run.

See Guard failure handling for the SDK versions that enforce this behavior and for wrapper options.

Content moderation runs on Arcjet, so the text that you pass to the rule is sent to the Arcjet Cloud API for evaluation. Arcjet doesn’t store the text. The decision keeps the verdict, the billing count, and the label and metadata that you attach to the call, so don’t copy the text into metadata. For what else Arcjet stores and for how long, see Arcjet privacy.

Content moderation is priced based on usage, measured in text_units when the service reports billing. A text unit is the length of the text you submit to a single moderation call, in characters, divided by 1,000 and rounded up. For a worked example, see how text units are calculated. For the price per text unit, see the pricing page.

Discussion