Skip to content
AgenticAIWiki
  1. Learn
  2. Part 12
  3. 64. Guardrails in depth
  4. 64.6

Lesson 64.6 · 6 of 11 · 4 min read · Reviewed 24 Nov 2026

Content moderation pipelines

By the end, you can

  • Build a content moderation pipeline with an LLM.
  • Use categories, thresholds and human review.

LLMsLarge language model (LLM): A neural network trained on large amounts of text to predict the next token, which lets it generate and transform language. are good at applying written policies to text, which makes them useful moderators.

Start with policy

Write categories with definitions and examples: harassment, spam, self-harm, illegal goods, and so on. Include borderline examples (12.2). The model can only be as consistent as the policy.

The pipeline

  1. Cheap filters first: known spam patterns, duplicates.
  2. Fast model labels each item: category, severity, confidence, short reason, in a fixed schema.
  3. Route: clear violations actioned, clear passes published, borderline cases queued for people.
  4. Appeals go to people, and outcomes become new examples.

Measure

For each category, track precision (of items flagged, how many were right) and recall (of real violations, how many were caught) on a labelled set. Different categories need different trade-offs: missing self-harm content is worse than over-flagging spam.

Use the Message Batches APIMessage Batches: An asynchronous API for submitting many requests at once, processed at a discount, with results collected later. for backlogs that don’t need instant answers (14.5).

Key points

  • Define clear categories and policies first; the model applies them.
  • Use a fast model for first-pass labels with reasons, and send borderline cases to people.
  • Measure precision and recall per category on a labelled set, and revisit as policies change.
Hands-on labRuns in the Playground

Route posts through a moderation pipeline

Combine a cheap filter, a fast classifier and human review, and measure precision and recall per category.

You will have: A moderate() function and a per-category precision and recall report on eight labelled posts.

Steps

  1. Run the starter: every post is marked ok.
  2. Apply the cheap spam patterns before calling the classifier.
  3. Send low-confidence labels to review instead of acting on them.
  4. Compute precision and recall for spam and harassment against the true labels.

Done when

  • Spam and harassment both show 100% precision and recall.
  • The borderline 'idiotic, but' post goes to people, not to an automatic action.

Open in the Playground No API key needed.

Starter code (Python)
# A moderation pipeline: cheap filter, a pretend fast classifier, then routing. Measure per category.
POSTS = [  # (text, true label)
    ("Buy cheap watches at spam-shop dot example", "spam"),
    ("Great tips, thanks for sharing", "ok"),
    ("You are an idiot and everyone hates you", "harassment"),
    ("Click here for free credits!!!", "spam"),
    ("I disagree, the second method is faster", "ok"),
    ("Nobody wants you here, leave", "harassment"),
    ("Limited offer, click here now", "spam"),
    ("That idea is idiotic, but the code is fine", "ok"),
]
SPAM_PATTERNS = ["spam-shop", "free credits"]

def fake_classifier(text):
    # Stands in for a fast model returning a label and a confidence in a fixed schema.
    t = text.lower()
    if "click here" in t: return "spam", 0.9
    if "idiot" in t: return "harassment", 0.6 if "but" in t else 0.9
    if "nobody wants you" in t: return "harassment", 0.8
    return "ok", 0.95

def moderate(text):
    # TODO: cheap filter first (SPAM_PATTERNS -> "spam"), then the classifier
    # TODO: route: confidence below 0.7 -> "review", otherwise the label
    return "ok"

decisions = [(moderate(t), y) for t, y in POSTS]
print(decisions)
# TODO: print precision and recall for "spam" and "harassment", and how many went to review
Show a solution
POSTS = [
    ("Buy cheap watches at spam-shop dot example", "spam"),
    ("Great tips, thanks for sharing", "ok"),
    ("You are an idiot and everyone hates you", "harassment"),
    ("Click here for free credits!!!", "spam"),
    ("I disagree, the second method is faster", "ok"),
    ("Nobody wants you here, leave", "harassment"),
    ("Limited offer, click here now", "spam"),
    ("That idea is idiotic, but the code is fine", "ok"),
]
SPAM_PATTERNS = ["spam-shop", "free credits"]

def fake_classifier(text):
    t = text.lower()
    if "click here" in t: return "spam", 0.9
    if "idiot" in t: return "harassment", 0.6 if "but" in t else 0.9
    if "nobody wants you" in t: return "harassment", 0.8
    return "ok", 0.95

def moderate(text):
    if any(p in text.lower() for p in SPAM_PATTERNS):
        return "spam"
    label, conf = fake_classifier(text)
    return "review" if conf < 0.7 else label

decisions = [(moderate(t), y) for t, y in POSTS]
for cat in ["spam", "harassment"]:
    flagged = [y for d, y in decisions if d == cat]
    actual = [d for d, y in decisions if y == cat]
    precision = sum(y == cat for y in flagged) / len(flagged) if flagged else 0
    recall = sum(d == cat for d in actual) / len(actual) if actual else 0
    print(f"{cat}: precision {precision:.0%}, recall {recall:.0%}")
print("sent to people:", sum(d == "review" for d, _ in decisions))
Quick checkA moderation pipeline must handle millions of posts a day with nuanced policy. What's a good design?
How sure are you?
Pick an answer and how sure you are