
Secure AI Applications Without Invoking an LLM: Amazon Bedrock ApplyGuardrail API
Did you know you can apply Amazon Bedrock Guardrails to user prompts and generated text without invoking a foundation model?
Secure AI Applications Without Invoking an LLM: Amazon Bedrock ApplyGuardrail API
When building AI applications, connecting a foundation model to your backend is only one part of the engineering challenge.
You also need to decide what content should enter your application, what information can be processed, and what responses should be returned to users.
Many developers associate Amazon Bedrock Guardrails with model inference.
But there is another useful capability worth exploring:
You can apply Amazon Bedrock Guardrails independently, without invoking a foundation model.
This is possible through the
ApplyGuardrail API.Let's understand how it works and how to integrate it into an AI application using Python.
1. What is the ApplyGuardrail API?
Amazon Bedrock Guardrails provides configurable safeguards for generative AI applications.
Depending on your configuration, you can define policies for:
- Harmful content filtering.
- Denied topics.
- Personally identifiable information (PII).
- Custom and managed word filters.
- Prompt attack detection.
- Contextual grounding checks.
Normally, you can associate a guardrail with supported foundation-model inference operations.
However, the
ApplyGuardrail API allows your application to submit content directly for evaluation without invoking a foundation model.This makes it useful when you want to introduce an independent evaluation step into an existing AI architecture.
For example, you can evaluate a user's question before sending it to a retrieval system or an external AI model.
Official documentation: https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails-use-independent-api.html
2. Where does it fit in an AI architecture?
Consider an AI-powered document assistant built with a RAG architecture.
A traditional workflow might look like this:
USER QUESTION → KNOWLEDGE BASE → RETRIEVAL → FOUNDATION MODEL → RESPONSE
Now introduce an independent guardrail evaluation step:
USER QUESTION
↓
APPLYGUARDRAIL (INPUT)
↓
KNOWLEDGE BASE RETRIEVAL
↓
FOUNDATION MODEL
↓
APPLYGUARDRAIL (OUTPUT)
↓
FINAL RESPONSE
This gives your application two separate evaluation points.
Before retrieval: Evaluate the user's input before processing it further.
After generation: Evaluate the generated response before returning it to the user.
AWS explicitly documents the ability to integrate ApplyGuardrail at different points in an application flow, including before retrieval in a RAG application.
Importantly, the API evaluates the content you submit. It does not automatically inspect every document, database record, or external tool result in your architecture.
3. Implementing ApplyGuardrail with Python
Let's look at a simple implementation.
First, install Boto3:
pip install boto3Configure your AWS credentials and create an Amazon Bedrock Guardrail through the AWS Console or API.
Make sure the guardrail has the policies required by your application.
For production use, create a numbered guardrail version instead of relying on the working draft.
Step 1: Initialize the Bedrock Runtime client
import osimport boto3
bedrock = boto3.client( "bedrock-runtime", region_name=os.environ["AWS_REGION"])
GUARDRAIL_ID = os.environ["BEDROCK_GUARDRAIL_ID"]
GUARDRAIL_VERSION = os.environ[ "BEDROCK_GUARDRAIL_VERSION"]Boto3 provides the
apply_guardrail() operation through the Bedrock Runtime client.Step 2: Create a reusable evaluation function
def evaluate_text(text: str, source: str):
if source not in {"INPUT", "OUTPUT"}: raise ValueError("Source must be INPUT or OUTPUT")
response = bedrock.apply_guardrail( guardrailIdentifier=GUARDRAIL_ID, guardrailVersion=GUARDRAIL_VERSION, source=source, content=[ { "text": { "text": text } } ] )
return responseHere are the important parameters:
-
guardrailIdentifier: Identifies the guardrail.
-
guardrailVersion: Specifies the version to use.
-
source: Identifies whether the content is user input or generated output.
-
content: Contains the actual text to evaluate.
AWS supports two source values:
INPUT — for content supplied by the user.OUTPUT — for generated content that your application wants to evaluate.Step 3: Evaluate a user prompt
Now let's check a user question before allowing it into the next stage of an application.
user_query = "Explain how Amazon Bedrock works."
result = evaluate_text( text=user_query, source="INPUT")
if result["action"] == "NONE":
print("Input passed guardrail evaluation.")
else:
print("Guardrail intervention detected.")
for item in result.get("outputs", []): print(item.get("text", ""))The response contains an
action field.Two important values are:
-
NONE— the guardrail did not intervene.
-
GUARDRAIL_INTERVENED— the guardrail took an action.
The
outputs field can contain masked content or a configured blocked message, depending on the action taken.This example is deliberately conservative: it stops further processing whenever an intervention occurs. If your policy is designed to anonymize information and continue processing, inspect the returned content and explicitly implement that behavior.
Do not assume every intervention means the same thing.
4. Evaluate generated AI responses too
You can use the same function after a foundation model generates an answer.
For example:
model_response = ( "Here is the generated response from your AI model.")
output_check = evaluate_text( text=model_response, source="OUTPUT")
if output_check["action"] == "NONE":
print("Response passed guardrail evaluation.") print(model_response)
else:
print("Response requires intervention.")
for item in output_check.get("outputs", []): print(item.get("text", ""))This allows the application to evaluate generated text before serving it to a user.
The model invocation itself and the guardrail evaluation are separate operations in this architecture.
This approach can also be useful when integrating third-party foundation models because the standalone ApplyGuardrail API is not tied to a particular model invocation.
5. An important IAM detail
For an application using the standalone ApplyGuardrail API, AWS documents that the required invocation permission is:
bedrock:ApplyGuardrail You do not need foundation-model
bedrock:InvokeModel permissions solely to perform an independent guardrail evaluation.Your IAM policy should restrict access to the specific guardrail resource wherever practical.
This is particularly useful when separating responsibilities between application components.
Official documentation: https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails-permissions.html
6. Important limitations developers should understand
ApplyGuardrail is a useful safeguard, but it is not a complete security system.
There are several things to consider:
It does not replace authorization.
A guardrail does not determine whether a user is permitted to access a particular database record or perform a sensitive operation.
It does not guarantee factual accuracy.
Content filtering and denied-topic policies are not substitutes for application-level business rules or factual verification.
It only evaluates the content supplied to it.
Do not assume retrieved documents, external tool results, and every intermediate application payload are automatically evaluated.
Interventions need application-level handling.
Your backend must decide whether to block, mask, review, or otherwise handle content based on the configured policy.
Test before production deployment.
AWS recommends testing guardrail behavior and validating that the configured policies meet your application's requirements. For production, use a numbered guardrail version so that your application refers to a specific configuration snapshot.
Final thoughts
Building a secure AI application requires more than selecting a capable foundation model.
Developers need to think about the entire application flow: what enters the system, how information is processed, and what gets returned to users.
Amazon Bedrock's ApplyGuardrail API provides a useful way to introduce independent evaluation checkpoints into that architecture without invoking a foundation model for every guardrail check.
Whether you are building a RAG application, an AI assistant, or an agent-powered automation workflow, understanding where and how to apply safeguards is an important part of AI engineering.
Don't just build applications that use AI. Build applications that control how AI is used.
#AWS #AmazonBedrock #AIEngineering #AISecurity #GenerativeAI #RAG #Python #Serverless
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article