Skip to main content
Guardrails help you enforce safety and policy checks before your agent calls an LLM. In the BB AI SDK, the integration uses NeMo Guardrails and routes guardrail model calls through the Backbase AI Gateway. The SDK provides:
  • init_guardrails(config_path): loads your NeMo configuration and returns an initialized LLMRails
  • GuardedAIGatewayModel: an Agno-compatible model that runs guardrail checks before each model response
  • bb-ai-sdk guardrails init: scaffolds a starter guardrails configuration

Prerequisites

Install the BB AI SDK with guardrails dependencies:
GuardedAIGatewayModel works with Agno-based agents. If you use this wrapper, ensure your project includes Agno. The wrapper supports adding input rails to your Agno agent.

Quick start

1

Scaffold the guardrails configuration

Create a starter guardrails configuration folder:
This creates:
Optional flags:
2

Set your guardrails model configuration

Update guardrails/config/config.yml with your model and agent_id:
The key part is engine: aigateway, which uses the SDK provider that init_guardrails registers.
3

Initialize guardrails in your app

This loads your NeMo configuration and returns an LLMRails engine ready for sync and async generation.
4

Protect an Agno model with guardrails

Use this model in your Agno agent. Before each response, the model runs a NeMo guardrail check.

How runtime blocking works

When GuardedAIGatewayModel receives a request:
  1. It forwards system and user messages to NeMo guardrails.
  2. If NeMo returns the configured refusal message, the SDK returns that refusal immediately.
  3. If NeMo doesn’t block the request, the SDK forwards it to AI Gateway and the model responds normally.
When NeMo blocks a request, the SDK also tags the active span with:
  • guardrails.blocked = true

Custom refusal messages

NeMo blocks via the bot refuse to respond flow. You can override that flow in Colang:
Then set GUARDRAILS_REFUSAL_MESSAGE to the exact same text so the SDK can reliably detect blocked responses:
If you don’t set this variable, the SDK defaults to:

Using NeMo Guardrails API to add input and output rails

Use the NeMo LLMRails API directly with the generate() or generate_async() methods to evaluate user inputs and model responses.

Configuration with input and output rails

Update your config.yml to include both input and output rails:
Define the rail flows in rails.co:
Add the main conversation flow in main.co:
Add the corresponding prompts in prompts.yml:

Using the NeMo API directly

CLI reference

bb-ai-sdk guardrails init

Scaffolds guardrails/config with template files.

Troubleshooting

Install the guardrails extra:
Check both of these:
  • Your config.yml uses engine: aigateway
  • GUARDRAILS_REFUSAL_MESSAGE exactly matches your custom bot refuse to respond output
Verify that you set agent_id correctly in:
  • guardrails/config/config.yml, for NeMo provider model calls
  • GuardedAIGatewayModel(..., agent_id=...), for protected model calls

Next steps

AI gateway

Learn how model requests are routed through the platform

Observability

Add tracing and monitor blocked requests

Get started

Build your first agent with SDK modules

Evaluation framework

Evaluate behavior and safety outcomes over datasets