Implementing Custom Guardrails for Domain Constraints
This lesson explains how to implement custom guardrails for domain-specific rules, such as regulated advice, prohibited topics, transaction limits and data boundaries, that built-in classifiers do not detect.
Learning outcomes
After completing this lesson, you will be able to:
- Tailor content controls with thresholds, blocklists and custom categories.
- Enforce rules about actions and data in tool wrappers and middleware.
- Specify custom guardrails so they can be tested and audited.
Built-in customization options
Foundry and Azure AI Content Safety offer several ways to tailor content controls. Severity thresholds per harm category can be raised or lowered. Custom blocklists block or flag exact terms and patterns, such as competitor product names or internal code names. Custom categories in Content Safety detect a domain-specific type of content from a definition and examples, for example "personalized investment recommendations". PII controls detect and can redact categories of personal data.
Customizing content controls
Guardrails in code and middleware
Many domain constraints are rules about actions and data rather than about content categories, and they are enforced most reliably in code. Tool wrappers and middleware validate arguments against rules (maximum amounts, allowed destinations, permitted account types), redact or tokenize identifiers before they reach a model or a log, and block actions outside policy. A small classifier or LLM-based checker can enforce rules that need interpretation, such as "no statement that a claim will definitely be approved", applied to outputs before they are sent.
async def payments_policy(context: FunctionInvocationContext, call_next):
if context.function.name == "transfer_funds":
args = context.arguments # a mapping of argument names to values
if args["amount"] > 10_000 or args["destination_iban"] not in APPROVED_PAYEES:
context.result = {"error": "policy_violation", "action": "escalate",
"message": "Transfers above 10,000 EUR or to new payees need a person."}
audit.log("guardrail_block", rule="payments-limit", case=context.kwargs.get("case_id"))
return
await call_next()A payments guardrail with holes
Specifying custom guardrails
Each custom guardrail is specified so that it can be implemented, tested and audited, with the elements in the following list.
- Rule: a precise statement, with examples of content or actions that violate and that comply.
- Intervention point: input, tool call, tool response or output.
- Action: block, redact, escalate to a person, or annotate and allow.
- Owner: the person or team accountable for the rule and its exceptions.
- Logging: what is recorded when the guardrail fires, for audit and tuning.
- Tests: violating and compliant cases that run in CI.
Parts of a guardrail specification
Medical advice limits
A pharmacy's assistant may explain general medicine information but must never recommend dosage changes for an individual, and must direct such questions to a pharmacist. The built-in harm categories do not flag these responses.
Define a custom category (or a fine-tuned classifier) for individualized dosage advice, apply it to outputs with a block-and-escalate action that offers a pharmacist hand-off, log each occurrence and test it with violating and compliant examples. A sentence in the instructions would help but cannot be relied on in a regulated setting.
Where are rules about actions and data enforced?
Show answer
In tool wrappers and middleware, where every call passes through them.
Exam Tips
Remember for the exam
- Tailor thresholds, blocklists and custom categories for content rules.
- Enforce action and data rules in tool wrappers and middleware.
- Every custom guardrail has a rule, point, action, owner, logging and tests.