Custom Rules
Deterministic keyword and regex matching for patterns specific to your organization
A custom rule is a user-defined rule that searches text for specific patterns. It lets teams block inputs and outputs that match patterns they define themselves, with no model involved.
The GenAI Engine approach
The engine supports two kinds of custom rule.
- Regex rules detect patterns of text with regular expressions. Use them for custom risks such as API key formats, internal account IDs, or document reference numbers.
- Keyword and key phrase rules detect specific words or phrases. Use them to catch inappropriate words, confidential project names, competitor names, or any other fixed string.
Both are deterministic: the same input always produces the same result, with no threshold to tune and no model latency. They run locally inside the engine.
Requirements
Custom rules run on validate_prompt, validate_response, or both. For most checks, such as forbidden topics, we recommend both, though some use cases only need one.
| Prompt | Response | Context needed? | |
|---|---|---|---|
| Custom Rules | ✅ | ✅ |
Governance
Custom regex and keyword checks are among the first rules teams enable when adopting LLMs. Configuring them as default rules in the GenAI Engine gives you one place to see and manage every blocked pattern, keyword, and phrase across the organization, globally or per use case.
Configuration
Which type to use depends on what you are trying to catch.
Regex rules
To block an unknown string with a known shape, such as an account number, create a RegexRule. Provide one or more regular expressions in the regex_patterns field of config when calling POST /api/v2/tasks/{task_id}/rules or POST /api/v2/default_rules.
Encode the request with a JSON library so backslashes are escaped correctly; \d must be sent as \\d.
{
"name": "Account number pattern",
"type": "RegexRule",
"apply_to_prompt": true,
"apply_to_response": true,
"config": {
"regex_patterns": [
"\\d{3}-\\d{2}-\\d{4}",
"\\d{5}-\\d{6}-\\d{7}"
]
}
}When a pattern matches, the result lists each matched span and the pattern that matched it.
{
"id": "123c4a44-b367-4229-84d4-1a8e461cb132",
"name": "Account number pattern",
"rule_type": "RegexRule",
"scope": "default",
"result": "Fail",
"latency_ms": 1,
"details": {
"message": "Regex match in text.",
"regex_matches": [
{ "matching_text": "123-45-6789", "pattern": "\\d{3}-\\d{2}-\\d{4}" }
]
}
}Keyword rules
To block a fixed string, such as a specific word or phrase, create a KeywordRule. Provide the list in the keywords field of config.
Matching is case-insensitive and whole-word: secret matches "Secret" and "SECRET" but not "secretary". Multi-word phrases match as a unit. A keyword made only of punctuation, such as $$$, is matched literally without word boundaries.
{
"name": "Blocked keywords",
"type": "KeywordRule",
"apply_to_prompt": true,
"apply_to_response": true,
"config": {
"keywords": ["Blocked_Keyword_1", "Blocked_Keyword_2"]
}
}When a keyword matches, the result lists which configured keywords were found.
{
"id": "e5ee86e1-8369-4eca-83bd-577668fc3208",
"name": "Blocked keywords",
"rule_type": "KeywordRule",
"scope": "default",
"result": "Fail",
"latency_ms": 1,
"details": {
"message": "Keywords found in text.",
"keyword_matches": [
{ "keyword": "Blocked_Keyword_1" }
]
}
}See the Rule Configuration Guide for enabling or disabling custom rules by default or per task.
Updated 6 days ago