Guardrails
Guardrails are rules your organisation sets on its own traffic. A rule can block a request, redact matching text, or flag it, and it can check what you send, what the model sends back, or both. Rules are managed under Guardrails in the dashboard by an organisation owner or admin, and apply to every API key the organisation owns.
What a rule checks
| Stage | When it runs | What it reads |
|---|---|---|
| Requests (input) | Before the request reaches the model | Every piece of text in the request body |
| Responses (output) | Before the model's answer reaches you | The answer's text, reasoning and tool-call arguments, for streamed and non-streamed responses |
Input rules read the text of these endpoints:
| Endpoint | Text that is checked |
|---|---|
/v1/chat/completions | Message content (strings and text parts) and tool-call arguments |
/v1/responses | instructions, and input as a string or as items (message content, function-call arguments and outputs) |
/v1/messages | system, and message content: text, tool inputs and tool results |
/v1/completions | prompt |
/v1/embeddings | input strings |
Output rules apply to /v1/chat/completions, /v1/responses and /v1/messages.
Images, audio, files and other endpoints are not checked by guardrails.
Rule types
| Type | What it does | Actions |
|---|---|---|
| PII Detector | Finds email addresses, phone numbers, card numbers, US SSNs, IP addresses, and names given after "my name is" | Redact, block |
| Keyword Blocker | Matches words or phrases you list, as a substring or as a whole word | Block, redact |
| Regex Pattern | Matches your own regular expressions | Block, redact |
| Length Limit | Caps the total characters or the number of messages in a request | Block |
| Content Filter | Matches a fixed list of phrases per category. It matches wording, not meaning: a paraphrase passes. For classification by meaning, use the Moderations API | Block, flag |
| Webhook | Asks an HTTPS service of yours for a pass or block verdict | Block |
Regex patterns
Patterns use RE2 syntax: character classes, groups, alternation, repetition and anchors all work, and matching time grows only in proportion to the text, whatever the pattern.
- Lookahead, lookbehind and backreferences are not supported. A pattern that uses them is refused when you save the rule.
- A pattern must not match empty text, may be up to 512 characters, and a rule may hold up to 20 patterns.
- Flags:
i(ignore case),m(^and$match at line breaks) ands(.matches line breaks).
Redaction
A redact rule replaces what it matches and lets the request continue. The model receives, and your request logs store, the redacted text:
| Match | Replaced with |
|---|---|
| Email address | [EMAIL] |
| Phone number | [PHONE] |
| Card number | [CREDIT_CARD] |
| US SSN | [SSN] |
| IP address | [IP_ADDRESS] |
| Name | [NAME] |
| Keyword or regex match | [REDACTED] |
Inside tool-call arguments, redaction changes string values only, so the arguments stay valid JSON.
Redacting streamed responses
To redact a stream, a few characters are held back until it is certain they are not the start of a match — typically the word currently being written. Your stream stays incremental; text arrives a word later than it otherwise would. Regex rules hold back up to 128 characters, so a regex match longer than that which is split across the stream can be missed.
Very large requests and responses
Rules check all of the text, whatever its size: a request at a model's full context window is checked the same way as a short one.
Checking a very large request or response takes longer, and has a time limit: the rule's Timeout, up to 10 seconds. If the check does not finish in that time, the rule is treated as one that could not evaluate. The request passes, without that rule's redactions — or is blocked if Fail Closed is on — and is listed in the rule's activity either way. Turn Fail Closed on for a rule that must never be skipped.
Ordinary rules finish in a small fraction of a second on the largest requests. Reaching the limit takes many slow regex patterns over hundreds of thousands of tokens.
When a rule blocks
A blocked request is refused with HTTP 400 before it reaches the model, and is not charged:
{
"error": {
"message": "PII detected: email address",
"type": "invalid_request_error",
"param": null,
"code": "invalid_prompt"
}
}
{
"error": {
"message": "PII detected: email address",
"type": "invalid_request_error",
"param": null,
"code": "invalid_prompt"
}
}
A blocked response is refused the same way. On a stream, text already sent stays sent and the stream ends with an error event carrying the same message. Because the model has already done the work, a response your own rule blocks is charged for the tokens it used.
Applying a rule
By default a rule applies to all requests your organisation makes. Nothing in a request can switch a rule off.
Turn off Apply to all requests to make a rule opt-in: it then runs only on requests that name it. The rule's name (for example guard-1a2b3c4d) is shown on its page.
from openai import OpenAI
client = OpenAI(base_url="https://api.scx.ai/v1", api_key="YOUR_API_KEY")
response = client.chat.completions.create(
model="MODEL_NAME",
messages=[{"role": "user", "content": "Summarise this ticket"}],
extra_body={"metadata": {"guardrails": {"enable": ["guard-1a2b3c4d"]}}},
)
from openai import OpenAI
client = OpenAI(base_url="https://api.scx.ai/v1", api_key="YOUR_API_KEY")
response = client.chat.completions.create(
model="MODEL_NAME",
messages=[{"role": "user", "content": "Summarise this ticket"}],
extra_body={"metadata": {"guardrails": {"enable": ["guard-1a2b3c4d"]}}},
)
curl https://api.scx.ai/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "MODEL_NAME",
"messages": [{"role": "user", "content": "Summarise this ticket"}],
"metadata": {"guardrails": {"enable": ["guard-1a2b3c4d"]}}
}'
curl https://api.scx.ai/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "MODEL_NAME",
"messages": [{"role": "user", "content": "Summarise this ticket"}],
"metadata": {"guardrails": {"enable": ["guard-1a2b3c4d"]}}
}'
A request can only enable rules belonging to its own organisation. metadata.guardrails is read and then removed; it is not stored with the request.
New and changed rules can take up to 30 seconds to apply.
Webhooks
A webhook rule sends a POST with a JSON body to your URL and expects a JSON verdict back:
{ "action": "block", "reason": "Not permitted by policy" }
{ "action": "block", "reason": "Not permitted by policy" }
Any other action passes the request. The body contains hook (pre_call or post_call), requestId, model, endpoint, the request's text fields (messages, input, instructions, system or prompt), and, for a response check, response.
- The URL must be a public
httpsaddress. - With a signing secret set, the body's HMAC-SHA256 is sent hex-encoded in
X-Guardrail-Signature. - If your service errors or does not answer within the rule's timeout, the request passes — unless Fail Closed is on, in which case it is blocked.
- A response webhook on a streamed response holds the stream until your service has answered.
Seeing what a rule did
Each rule's page lists the requests it blocked, redacted or flagged in the last 7 days. Requests a rule checked and passed are not listed.
Responses carry X-Guardrail-Count (how many rules checked the request) and X-Guardrail-Duration-Ms when input rules ran.