AI Safety, Security, and Governance · Topic 1 of 7

Content Safety for Inputs and Outputs

Filter harmful prompts and responses with Bedrock Guardrails, add custom moderation with Step Functions and Lambda, and use text to SQL for exact answers.

  • 6 min
  • Includes a diagram
In a nutshell
  • Amazon Bedrock Guardrails filters both prompts and responses. Content filter categories are Hate, Insults, Sexual, Violence, Misconduct and Prompt Attack.
  • Denied topics block subjects; word filters block exact words; sensitive information filters block or mask PII.
  • ApplyGuardrail checks text without invoking a model, so guardrails can protect any model or any text.

What it is

Content safety stops harmful material from reaching a foundation model (FM) and stops harmful material from leaving it. Inputs can carry hate, violence, sexual content, insults, misconduct or attempts to steer the model. Outputs can contain the same, or confident answers the business cannot stand behind.

The managed tool for both directions is Amazon Bedrock Guardrails. Custom moderation with AWS Step Functions and AWS Lambda fills the gaps, and evaluations prove the filters work before and after release.

Userpromptinput checkGuardrailcontent filtersallowedFMAmazon Bedrockoutput checkGuardrailcontent filterssafeUserresponse

How it works

Amazon Bedrock Guardrails policies

Guardrail on input and output: the user prompt passes input policies, the model is called, the response passes output policies including contextual grounding and Automated Reasoning checks; if either side blocks, the caller gets the blocked message you configured. A guardrail is a reusable set of policies. You attach it to model calls (InvokeModel, Converse and their streaming variants), to Bedrock Agents and to knowledge base RetrieveAndGenerate calls. The ApplyGuardrail API evaluates text without calling a model, so it also protects self-hosted or third-party models.

PolicyWhat it catchesTypical use
Content filtersHate, Insults, Sexual, Violence, Misconduct, plus Prompt AttackGeneral toxicity, set per category from NONE to HIGH strength
Denied topicsSubjects you define in plain languageA bank assistant that must not give investment advice
Word filtersExact words and phrases, plus a managed profanity listCompetitor names, banned terms
Sensitive information filtersBuilt-in PII types and custom regexBlock or mask PII in prompts and responses
Contextual grounding checksResponses not grounded in the source or not relevant to the queryRAG answers that invent facts
Automated Reasoning checksResponses that break logical rules you definePolicy or eligibility answers that must follow written rules

Filter strength and action are set separately for the input and the output. When a policy blocks content, the caller receives the blocked message you configured instead of the model text. The NONE action (detect mode) returns what was found without blocking, which is useful for tuning a new guardrail on live traffic.

Guardrails are versioned. Applications should reference a numbered version, not the working draft, so a policy edit does not change production behavior until you publish and switch versions.

Custom moderation workflows

Some rules cannot be expressed as guardrail policies: a customer tier that allows different content, a lookup against a blocklist in DynamoDB, or a human decision for borderline cases. Build those as a workflow.

API GatewayrequeststartStep Functionsmoderation workflowcallLambdabusiness rulespassAmazon Bedrockguardrail and modelStep Functionsborderlinewait for task tokenHuman reviewerapprove or reject
  • Lambda runs fast synchronous checks, such as length limits, allowed languages or calls to Amazon Comprehend DetectToxicContent.
  • Step Functions chains checks, retries failures and branches on results. The callback pattern with a task token pauses the workflow until a human approves or rejects.
  • Real-time validation keeps the synchronous path short. Slow or human checks belong on an asynchronous branch, not in the chat request path.
Exam tip

Use an Express workflow for high-volume, short moderation steps and a Standard workflow when a human review can take hours.

Proving output safety with evaluations

Filters need evidence. Amazon Bedrock model evaluations can score responses with an LLM as a judge using built-in responsible AI metrics such as Harmfulness, Stereotyping and Refusal. Run the same prompt dataset against the model with and without the guardrail to measure what the guardrail changes.

Amazon Comprehend DetectToxicContent returns a toxicity score and category labels for English text. It works as a lightweight classifier in a batch evaluation of logged outputs or as an extra check in a Lambda function.

Text to SQL for deterministic results

When the answer is a number in a database, such as an order total or a balance, a free text answer from the model is risky. The safer pattern has the model write a SQL query, and the database returns the exact value.

Questionnatural languagegenerateFMSQL queryvalidateLambdaread only checksrunDatabaseexact rowsformatAnswer

Validate the generated SQL before running it. Allow only SELECT, run it with a read-only database user and limit the tables it can touch. Amazon Bedrock Knowledge Bases can also connect to a structured data store such as Amazon Redshift and generate the query for you.

Common mistakes

Filtering only the output. Harmful or manipulative prompts still reach the model, cost tokens and can steer agent actions. Configure input filters as well as output filters.

Writing a custom classifier for standard categories. Hate, violence, sexual content and insults are built-in guardrail categories. A custom model adds cost and operational overhead for no gain.

Letting the model state database facts. A model summarizing a balance from memory or from a large prompt can be wrong. Text to SQL returns the stored value, so the answer is repeatable.

Pointing production at the draft guardrail. Editing the draft then changes live behavior with no review. Publish a version and reference it.

What to remember for the exam

  • Amazon Bedrock Guardrails filters both prompts and responses. Content filter categories are Hate, Insults, Sexual, Violence, Misconduct and Prompt Attack.
  • Denied topics block subjects; word filters block exact words; sensitive information filters block or mask PII.
  • ApplyGuardrail checks text without invoking a model, so guardrails can protect any model or any text.
  • Step Functions plus Lambda builds custom moderation, and a task token pauses for human review.
  • Bedrock model evaluations with an LLM as a judge measure Harmfulness, Stereotyping and Refusal.
  • Text to SQL gives deterministic answers for structured data. Validate the query and run it read only.
Watch out

Content blocked by a guardrail still appears as plain text in Amazon Bedrock model invocation logs if logging is on. Protect those logs as sensitive data.

Check yourself

  • A support chatbot must never discuss legal advice, but the model is otherwise fine. What is the least effort control?

    Add a denied topic for legal advice to an Amazon Bedrock guardrail and attach it to the model calls.

  • How do you apply the same guardrail to a model hosted outside Amazon Bedrock?

    Call the ApplyGuardrail API on the prompt and on the response, because it evaluates text without invoking a Bedrock model.

  • Why use text to SQL instead of putting a sales table in the prompt?

    The database returns the exact stored values, so answers are deterministic and do not depend on the model reading numbers correctly.

Topic 1 of 7 in AI Safety, Security, and Governance

Test yourself on AI Safety, Security, and Governance

The full AWS AIP-C01 question bank has 827 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by AWS. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.