Layering Custom Moderation for Defense in Depth
This lesson covers custom moderation workflows and a layered architecture that adds independent checks, human review and controls at other layers, so one failure does not expose users.
Learning outcomes
After completing this lesson, you will be able to:
- Screen input with Amazon Comprehend before prompting.
- Build custom moderation workflows with Step Functions.
- Validate and filter output before returning it.
Pre-processing filters with Amazon Comprehend
The cheapest harmful request to handle is one that never reaches the model. Before a prompt is built, Amazon Comprehend can screen user input. Toxicity detection scores text for categories such as hate speech, harassment, insults, profanity, sexual content and violence or threats. PII detection finds personal data and can redact it. The prompt safety classifier flags unsafe prompts, such as attempts to obtain harmful information or to manipulate the model. Language detection rejects languages the application does not support.
These checks run in a Lambda function before the model is called. Their thresholds are set in configuration, so behaviour is predictable and easy to tune, they are inexpensive compared with a model call, and they are independent of the generation model, so they keep working unchanged when the model is replaced. They complement Bedrock Guardrails, which apply the organisation's content policies at the model boundary.
Screening input early
Custom moderation workflows
Some moderation needs several steps or human judgement, such as user-generated content on a public platform. A Step Functions workflow can run checks in parallel (Comprehend toxicity, a guardrail evaluation with ApplyGuardrail, a domain-specific classifier), combine their scores, automatically allow or block clear cases, and route borderline cases to a human moderator with a task token or Amazon A2I. Real-time validation in Lambda enforces rules such as maximum length, allowed file types and per-user rate limits before any model is called.
The following diagram shows the layers of moderation that a request and its response pass through in a public community assistant.

Post-processing and API response filtering
After generation, a Lambda post-processor validates the output: schema compliance, allowed values, absence of internal URLs or identifiers, presence of required disclaimers, links only to approved domains. At the edge, API Gateway response handling (through the integration or a response-transforming Lambda) removes internal fields such as retrieved chunk text or trace data before the response leaves, and AWS WAF blocks abusive request patterns and oversized payloads before they reach the application.
Each layer logs its decisions with the request ID, so blocked content can be analysed and thresholds tuned. The following table shows a typical layered design.
| Layer | Control | Catches |
|---|---|---|
| Edge | AWS WAF, API Gateway throttling and authentication | Abuse, floods, oversized requests |
| Pre-processing | Comprehend toxicity, PII and prompt safety; Lambda validation | Toxic or unsafe input, PII, invalid input |
| Model-based guardrails | Bedrock Guardrails on input and output | Harmful content, denied topics, PII, ungrounded answers |
| Post-processing | Lambda schema and policy validation | Format errors, leaked internals, missing disclaimers |
| Response filtering | API Gateway response handling | Internal fields leaving the system |
| Human review | Step Functions task tokens or Amazon A2I | Borderline and high-risk cases |
Layers of defence
A public community assistant
A gaming company launches a public assistant that also moderates user posts. Requirements include blocking abuse, catching attempts to make the assistant produce harmful instructions, keeping internal moderation notes out of responses and sending ambiguous posts to staff.
The design layers WAF rate limits, a Comprehend toxicity and prompt safety pre-filter, a Bedrock guardrail with content and prompt attack filters, a Lambda post-processor that strips internal notes, and a Step Functions workflow that routes posts with intermediate scores to human moderators.
Which service supports human review steps in a moderation workflow?
Show answer
Amazon Augmented AI (A2I), usually orchestrated by Step Functions.
Exam Tips
Remember for the exam
- Pre-filter input with Comprehend toxicity, PII and prompt safety classification.
- Multi-step moderation with human review: Step Functions and A2I.
- Post-validate outputs in Lambda; strip internal fields at the API layer.
- Layer controls so a single miss does not reach users.