Dev.to Security 🔐 Cybersecurity 👁 0 📖 4 min read

Your AI chat needs a safety layer the system prompt cannot replace

Picture a small support chatbot that has been live for two weeks. Then the first odd messages arrive: a customer pastes her full IBAN into the chat, someone tries the classic "ignore all previous instructions", and anoth

Picture a small support chatbot that has been live for two weeks. Then the first odd messages arrive: a customer pastes her full IBAN into the chat, someone tries the classic "ignore all previous instructions", and another user wants to know who is legally at fault after a bike accident. None of that is what the bot was built for, and a longer system prompt is a fragile defence against all three.

My preferred answer is a separate safety layer that sits in front of and behind the model: Amazon Bedrock Guardrails, called from a Lambda function. I walk through the complete setup with TypeScript and the Serverless Framework in my article Bedrock Guardrails with Lambda: filter PII and prompt attacks. Here is the condensed version of what I learned.

A guardrail is its own resource, not a prompt

The key mental shift: a guardrail is not part of the model. It is a standalone resource with its own ID and versions, and it checks any text you hand it. It offers several filter families:

  • Content filters for hate, insults, violence and similar categories, with a strength per category
  • Prompt attack detection for jailbreaks and prompt injection
  • Denied topics that you describe in plain language, for example legal advice
  • Word filters for exact words and phrases
  • Sensitive information filters for predefined PII types plus your own regular expressions, which can block or mask
  • Contextual grounding to catch answers not backed by the provided source

Because the guardrail runs independently of the model, it keeps working even when the model is in an uncooperative mood. And unlike a prompt, you can cover it with tests.

The language trap most people miss

This was the point that surprised me most. Content filters, prompt attack detection and denied topics come in two tiers. The default Classic tier is, according to the AWS documentation, designed only for English, French and Spanish. If your chat runs in German, Dutch or Polish, the guardrail deploys fine, logs look calm, and it protects very little.

The Standard tier covers more than 60 languages, but it requires cross-Region inference through a guardrail profile (in Europe eu.guardrail.v1:0). That means checks may run in several EU Regions, which is worth clarifying with your data protection team early. PII filters have no tiers and support many languages directly, while word filters are documented only for English, French and Spanish.

Check before the model, and again after it

You can attach a guardrail to the Converse API call via guardrailConfig, or call the standalone ApplyGuardrail API yourself. For a Lambda chat I prefer the second option: you see the input verdict before paying for a single model token, you get the masked text back and send only that to the model, and you can reuse the same check for other text sources. The price is two extra API calls per message.

The handler flow is simple: check input, call the model with the cleaned text, check the output. A simplified sketch:

const input = await guard.check(message, 'INPUT');
if (input.blocked) return reply(input.text, true);

const answer = await callModel(input.text); // model only sees masked text

const output = await guard.check(answer, 'OUTPUT');
return reply(output.text, output.blocked);

Checking the output is not paranoia. Even a well-instructed model sometimes repeats data from its context or gets talked into a topic it should avoid.

The gap nobody warns you about: tool results

Once your assistant uses tools, there is a blind spot. The AWS documentation states that an inline guardrail does not evaluate tool results, tool definitions or tool arguments, and the prompt attack filter does not look into toolResult either. That is exactly where indirect prompt injection lives, for example a hidden instruction inside a fetched customer review. My rule: treat everything a tool brings in from outside like user input and run it through ApplyGuardrail as well.

A few more lessons from practice: never point production at the DRAFT version, never log the matched values from the response (that would write the very PII you wanted to protect into CloudWatch), and start with filters in report-only mode before you switch on blocking. Guardrails work with probabilities, so the most important defence stays boring: give your assistant only the tools and permissions it really needs.

What the full article covers

  • The complete serverless.yml with AWS::Bedrock::Guardrail, a fixed guardrail version and the IAM permissions for the Standard tier profile
  • A testable guard module that separates "pass", "masked" and "blocked", plus six tests that run without an AWS account
  • How to close the tool result gap in code, and where the new InvokeGuardrailChecks API fits for agents
  • Common mistakes, cost and latency considerations, and an FAQ

Read the full walkthrough with all code on my site: Bedrock Guardrails with Lambda: filter PII and prompt attacks

📖 This is the condensed version. The full article on sascha-ms.de goes deeper – with all code samples, diagrams and a short knowledge check.

👉 Next step: Not sure which AWS service fits your use case? My AWS service finder asks a few questions and points you to the right one.

📬 Stay updated: New articles on serverless, AI and leading tech teams via RSS, browser push or the weekly newsletter (in German).

Originally published on sascha-ms.de. More articles on serverless, AI in everyday development and leading tech teams: sascha-ms.de/en

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.