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
| Moderation | Topic guardrail (Module 3) | |
|---|---|---|
| The question | Is this message harmful? | Is this message about travel? |
| What it is | OpenAI's Moderation API: a free web service you send text to, not a chat model | A small chat model with a prompt you wrote |
| Speed | About 0.25 seconds | About 1 to 1.4 seconds |
| Catches | Threats, harassment, hate, self-harm, requests to do something illegal | Off-topic messages and prompt injection |
| Misses | Anything harmless but off topic, such as a maths question | Harm that is dressed up as a travel question, on some runs |
| Gives the same answer every time? | Yes, in the runs so far | No, 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
| Lesson | What it covers |
|---|---|
| Lesson 2: The Moderation API | The call, what comes back and what it can't see |
| Lesson 3: Two checks in the chat | Moderation first, then the guardrail, in the chat loop |
| Lesson 4: Testing both checks | Test cases with two labels, and a runner that compares the checks |
| Lesson 5: What the results showed | Which check caught what, and how steady each one was |
| Lesson 6: Checking the labels | How 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