Suryanand/ Bootcamp
suryanand.com ↗Let's talk
← Safety and governanceAI guideIntermediate#safety#moderation#trust-and-safety#classifiers
Frame · ai-engineering / safety-governance / 07-content-moderation-pipelines.mdGuide 088 / 103

Content moderation pipelines

Any product where users type into a model, or a model writes text other people will see, needs a moderation pipeline. That covers chat assistants, community features with AI summaries, generated images and anything user-to-user. This doc covers how I design one: start from a written policy, layer fast classifiers with slower judgement, route edge cases to humans, and close the loop so the system improves.

Why it matters

Model providers apply their own safety training, but their policy is not your policy. A children's education app, a medical platform and an adult fiction tool need very different lines. Regulation also increasingly expects you to take responsibility for what your system produces: the EU AI Act's transparency obligations, platform rules from app stores, and laws on illegal content all land on the deployer, not just the model vendor.

The tooling is good now. OpenAI's Moderation API with omni-moderation-latest classifies text and images and is free to use; Meta's Llama Guard 4 is an open 12B safety classifier aligned to the MLCommons hazards taxonomy that handles text and images; and OpenAI's open-weight gpt-oss-safeguard models (October 2025) classify content against a policy you write in plain language at inference time, with an explanation of the decision.

How it works

Moderation is a classification problem with a policy on top. The pipeline below runs on both user inputs and model outputs.

Rendering diagram…

Write the policy first. Define categories, examples on both sides of each line, and actions per category (block, warn, allow with logging, escalate). Without this, threshold tuning is guesswork.

Fast layer. A hosted moderation endpoint or a self-hosted classifier returns per-category scores. OpenAI's docs describe the response as a flagged boolean plus per-category booleans and scores, and advise treating scores as signals for your own policy rather than an automatic block decision.

Policy layer. For borderline cases and categories specific to your product, a policy-following model reads your written policy and the content, and returns a decision with rationale. This is slower but adapts to policy changes without retraining.

Human layer. Reviewers handle the genuinely ambiguous cases, appeals and high-severity categories. Their decisions become labelled data for threshold tuning and evals.

Reference architecture

At scale, separate synchronous checks (must finish before showing content) from asynchronous ones (run after, can trigger takedown or account action).

Rendering diagram…

Severity-based priority matters: anything involving child safety or credible threats goes to the top of the queue and often has legal reporting obligations, while mild profanity can wait or be handled automatically.

Code

A two-stage check in TypeScript: the OpenAI Moderation API as the fast layer, with category-specific thresholds from your policy, escalating borderline scores to a slower policy check.

import OpenAI from "openai";

const openai = new OpenAI();

// thresholds come from your written policy and labelled data, not defaults
const BLOCK: Record<string, number> = { "self-harm/intent": 0.5, "sexual/minors": 0.1, violence: 0.85 };
const REVIEW: Record<string, number> = { harassment: 0.4, "self-harm": 0.3, violence: 0.5 };

export async function moderate(text: string) {
  const res = await openai.moderations.create({ model: "omni-moderation-latest", input: text });
  const scores = res.results[0].category_scores as unknown as Record<string, number>;

  for (const [cat, t] of Object.entries(BLOCK)) {
    if ((scores[cat] ?? 0) >= t) return { action: "block", category: cat, score: scores[cat] };
  }
  for (const [cat, t] of Object.entries(REVIEW)) {
    if ((scores[cat] ?? 0) >= t) return { action: "escalate", category: cat, score: scores[cat] };
  }
  return { action: "allow" };
}

The escalate path would call your policy model (for example a self-hosted gpt-oss-safeguard with your policy text) and, if still uncertain, enqueue for human review.

Trade-offs and pitfalls

  • Default categories are not your policy. Map your policy to provider categories explicitly, and fill gaps with a policy model or custom classifier.
  • Over-blocking. Health, sexuality education, security research and fiction are frequently over-flagged. Measure false positives on real, legitimate content.
  • Language and dialect gaps. Classifiers perform unevenly across languages. Evaluate on the languages your users actually use.
  • Context matters. A message that is fine in one conversation is harmful in another. Moderate conversation windows, not just single messages.
  • Reviewer wellbeing. Human moderation of harmful content takes a toll. Rotate, limit exposure, blur by default and provide support.
  • No appeals path. Users who are wrongly blocked need a way to contest it, and appeals data is your best signal of false positives.

Checklist

  • Write a moderation policy with categories, examples and actions.
  • Moderate both user inputs and model outputs, including images.
  • Tune per-category thresholds on labelled samples of your own traffic.
  • Use a policy-following model or reviewers for borderline cases.
  • Prioritise review queues by severity and handle legal reporting duties.
  • Provide an appeals workflow and track reviewer agreement.
  • Re-evaluate when you change models, languages or markets.

Further reading

Why this exists README

I learn best by explaining things, so I record the explanations. Everything here is free, with no sign-up. If a lesson helps you ship something, I'd love to hear about it.