Lesson 2 of 4 Input and Output Safety Controls

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.

Scenario questions

Screening input early

Exam skills 3.1.4 · 3.1.1

Practise designing pre-processing filters.

Log in to try this activity Interactive activities are free with an account.

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.

Layered moderation for a public community assistant. AWS WAF applies rate limits, Amazon Comprehend screens for toxicity and unsafe prompts, a Bedrock guardrail applies content and prompt attack filters, and a Lambda post-processor strips internal moderation notes from responses. Borderline posts go to human moderators.
Each layer catches what the others miss.

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.

LayerControlCatches
EdgeAWS WAF, API Gateway throttling and authenticationAbuse, floods, oversized requests
Pre-processingComprehend toxicity, PII and prompt safety; Lambda validationToxic or unsafe input, PII, invalid input
Model-based guardrailsBedrock Guardrails on input and outputHarmful content, denied topics, PII, ungrounded answers
Post-processingLambda schema and policy validationFormat errors, leaked internals, missing disclaimers
Response filteringAPI Gateway response handlingInternal fields leaving the system
Human reviewStep Functions task tokens or Amazon A2IBorderline and high-risk cases
Put in order

Layers of defence

Exam skills 3.1.4

Put the layers of a defence-in-depth request path in the order a request meets them.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.