Guardrailing Bot Reasoning Content
Guardrailing Bot Reasoning Content
Reasoning-capable large language models (LLMs) expose their internal thought process as reasoning traces. These traces reveal how the model arrives at its conclusions, providing transparency into the decision-making process. However, they may also contain sensitive information or problematic reasoning patterns that need to be monitored and controlled.
The NeMo Guardrails library helps you set up guardrails to inspect and control these reasoning traces by extracting them. With this feature, you can configure guardrails that can block responses based on the model’s reasoning process, enhance moderation decisions with reasoning context, or monitor reasoning patterns.
This guide uses Colang 1.0 syntax. Colang 1.0 currently supports bot reasoning guardrails only.
The examples in this guide range from minimal toy examples (for understanding concepts) to complete reference implementations. These examples teach you how to access and work with bot_thinking in different contexts, not as production-ready code to copy-paste. Adapt these patterns to your specific use case with appropriate validation, error handling, and business logic for your application.
Accessing Reasoning Content
When an LLM generates a response with reasoning traces, the NeMo Guardrails library extracts the reasoning and makes it available through the bot_thinking variable. You can use this variable in the following ways.
In Colang Flows
The reasoning content is available as a context variable in Colang output rails. For example, in config/rails.co, you can set up a flow to capture the reasoning content by setting the $captured_reasoning variable to $bot_thinking.
In Custom Actions
When you write Python action functions in config/actions.py, you can access the reasoning through the context dictionary. For example, the following is an example action function that checks if the reasoning retrieved through context.get("bot_thinking") contains the word "sensitive". It returns False if the bot reasoning contains the word "sensitive".
In Prompt Templates
When you render prompts for LLM tasks such as self check output, the reasoning is available as a Jinja2 template variable. For example, in prompts.yml, you can set up a prompt to check if the reasoning contains the word "sensitive" and block the response if it does.
Always check if reasoning exists before using it, as not all models provide reasoning traces.
Guardrailing with Output Rails
You can use the $bot_thinking variable in output rails to inspect and control responses based on reasoning content.
-
Write a basic pattern-matching flow that uses the
$bot_thinkingvariable inconfig/rails.coas follows: -
Add the flow to your output rails in
config.ymlas follows:
This demonstrates how to set up a basic pattern-matching flow for learning purposes. Production implementations must use more comprehensive validation and consider edge cases.
Guardrailing with Custom Actions
For complex validation logic or reusable checks across multiple flows, you can write custom Python actions. This approach provides better code organization and makes it easier to share validation logic across different guardrails.
-
Write the custom action function in
config/actions.pyas follows: -
Write the flow that uses the custom action function in
config/rails.coas follows: -
Add the flow to your output rails in
config.yml.
Using Reasoning in Self-Check Output
The following steps show how to use bot_thinking in a self-check output rail. This pattern provides reasoning traces to your moderation LLM, allowing it to make more informed decisions by evaluating both the response and the reasoning process.
This extends the self check thinking configuration examples in the NeMo Guardrails library repository.
-
Write the
config.ymlfile as follows: -
Write the
prompts.ymlfile as follows:
The {% if bot_thinking %} conditional ensures that the prompt works with both reasoning and non-reasoning models. When reasoning is available, the self-check LLM can evaluate both the final response and the reasoning process.
Related Guides
Use the following guides to learn more about the features used in this guide.
- Model Configuration: Configure LLM models in config.yml.
- Self-Check Output Example: Complete working configuration example in the NeMo Guardrails library repository.
- Custom Actions: Guide on writing custom actions.