Module 4, Lesson 1: Moderation Overview

Why one check is not enough: moderation looks for harm, the guardrail looks for topic, and this module combines them.

Published: 11/27/2026

Welcome to Module 4

In Module 3 you built a guardrail that blocks messages that aren't about travel. That leaves a gap. A message can be about travel and still harmful, and a message can be harmless and still off topic. One check can't be good at both, so this module adds a second one.

Two questions, two checks

ModerationTopic guardrail (Module 3)
The questionIs this message harmful?Is this message about travel?
What it isOpenAI's Moderation API: a free web service you send text to, not a chat modelA small chat model with a prompt you wrote
SpeedAbout 0.25 secondsAbout 1 to 1.4 seconds
CatchesThreats, harassment, hate, self-harm, requests to do something illegalOff-topic messages and prompt injection
MissesAnything harmless but off topic, such as a maths questionHarm that is dressed up as a travel question, on some runs
Gives the same answer every time?Yes, in the runs so farNo, it can change its mind on borderline messages

Neither check replaces the other. Moderation has never heard of travel. The guardrail has never been told what counts as harm.

Using both

Moderation goes first because it is free and fast. A message it flags never costs you a guardrail call.

A message has to pass both checks. If either check fails with an error, the message is blocked too.

What you'll build

LessonWhat it covers
Lesson 2: The Moderation APIThe call, what comes back and what it can't see
Lesson 3: Two checks in the chatModeration first, then the guardrail, in the chat loop
Lesson 4: Testing both checksTest cases with two labels, and a runner that compares the checks
Lesson 5: What the results showedWhich check caught what, and how steady each one was
Lesson 6: Checking the labelsHow a wrong label made a check look broken, and what to take away

Extra setup

There are no new packages. Moderation uses the openai package you already installed.

You do need an OpenAI API key even for the Anthropic chat, because the Moderation API is an OpenAI service. The Anthropic chat in this module reads both ANTHROPIC_API_KEY and OPENAI_API_KEY from your .env file.

Add two scripts to package.json. Lesson 4 shows the key parts of the files they run. You build them by adapting your Module 3 generator and runner.

"scripts": {
  "moderation:generate": "tsx src/utils/generate-moderation-cases.ts",
  "moderation:eval": "tsx src/utils/evaluate-input-moderation.ts"
}

A note on the test messages. To test a moderation check you need harmful messages. The ones in this module are short and not graphic. The lessons describe them in words instead of quoting them, apart from one mild threat used in lessons 2 and 3.

👉 Continue to Lesson 2: The Moderation API