Guardrails quick start
This guide takes a gateway with no guardrails to one that masks personal data for a whole team. You create a PII masking guardrail and a keyword filter, test one of them on a sentence without spending money on a model, attach both to a team so that they apply to every key of the team automatically, and read the result on the Logs page. It takes about ten minutes, needs a proxy admin key in SPANWAY_API_KEY, and for the PII guardrail two Presidio services the gateway can reach.
A real-world scenario
A product team is about to give an internal assistant to its support agents. Before the first agent types a customer's email address into it, the platform engineer wants two things in place: email addresses and phone numbers must be masked before any model sees them, and the words of an unannounced product must never appear in a prompt. Both rules must apply to every key the support team creates later, without anyone remembering to switch them on.
1. Create the two guardrails
In the Admin UI open Guardrails under AI Gateway and click Add New Guardrail, then Add Provider Guardrail.
For the PII guardrail, enter pii-mask as the Guardrail Name, pick Presidio PII as the Guardrail Provider, keep Mode at pre_call, enter the two service addresses in presidio_analyzer_api_base and presidio_anonymizer_api_base if they are not set as environment variables, and click Next. On Configure PII Protection tick EMAIL_ADDRESS and PHONE_NUMBER with the action MASK and click Create Guardrail.
For the keyword filter, click Add New Guardrail again, enter blocked-words, pick Spanway Content Filter, click Next through Topics and Patterns to Keywords, add the words to block, and click Create Guardrail.
The same two guardrails in config.yaml, for a gateway that is deployed from a repository:
model_list:
- model_name: claude-haiku-5-5
spanway_params:
model: anthropic/claude-haiku-5-5
api_key: os.environ/ANTHROPIC_API_KEY
guardrails:
- guardrail_name: pii-mask
spanway_params:
guardrail: presidio
mode: pre_call
pii_entities_config:
EMAIL_ADDRESS: MASK
PHONE_NUMBER: MASK
- guardrail_name: blocked-words
spanway_params:
guardrail: spanway_content_filter
mode: pre_call
blocked_words:
- keyword: project-falcon
action: BLOCK
Presidio reads its service addresses from PRESIDIO_ANALYZER_API_BASE and PRESIDIO_ANONYMIZER_API_BASE when they are not in the config. Restart the gateway after a config change.
The API form of the first guardrail:
curl -X POST http://localhost:4000/guardrails \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{
"guardrail": {
"guardrail_name": "pii-mask",
"spanway_params": {"guardrail": "presidio", "mode": "pre_call", "default_on": true},
"guardrail_info": {"description": "Masks personal data before the model sees it"}
}
}'
Expected result: both guardrails appear in the table on the Guardrails page, and GET /guardrails/list returns them:
curl http://localhost:4000/guardrails/list -H "Authorization: Bearer $SPANWAY_API_KEY"
2. Test a guardrail on a sentence
You do not need a model call to see what a guardrail does. In the Admin UI open the Test Playground tab of the Guardrails page, select pii-mask on the left, type Email jane.doe@example.com about her refund under Input Text and click Test 1 guardrail. Over the API, the same test is one call to apply guardrail:
curl -X POST http://localhost:4000/guardrails/apply_guardrail \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"guardrail_name": "pii-mask", "text": "Call me at 415-555-0100 or email jane.doe@example.com", "language": "en"}'
Expected result:
{"response_text": "Call me at <PHONE_NUMBER> or email <EMAIL_ADDRESS>"}
Test blocked-words the same way with a sentence that contains one of the words. Expected result: a 400 whose message names the guardrail and the word it found, which is exactly what a model request would get.
3. Attach both guardrails to the team
Rather than relying on every client to ask for guardrails, attach them to the team, and every key of the team runs them. In the Admin UI open Teams under ACCESS CONTROL, click the team, click Edit, pick pii-mask and blocked-words under Guardrails and save. Over the API:
curl -X POST http://localhost:4000/team/update \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"team_id": "team-support", "guardrails": ["pii-mask", "blocked-words"]}'
Expected result: the team's detail page lists both under Guardrails Settings as team-specific guardrails. Attaching guardrails to a team needs an Enterprise license that includes the guardrails feature, and a gateway without one answers 403. Without that license, set Always On to Yes on the guardrail (default_on: true) so it runs for everyone, or put both guardrails in a policy and attach it to the team with a policy attachment, which needs no license.
4. Send a request and read the result
Send a request with a key of the team:
curl -X POST http://localhost:4000/v1/chat/completions \
-H "Authorization: Bearer $SUPPORT_TEAM_KEY" -H "Content-Type: application/json" \
-d '{
"model": "claude-haiku-5-5",
"messages": [{"role": "user", "content": "Email jane.doe@example.com about her refund"}]
}'
Expected result: a normal chat completion. The model received Email <EMAIL_ADDRESS> about her refund, which you can confirm by opening Logs, clicking the request and reading the Guardrails & Policy Compliance section. It lists pii-mask and blocked-words, whether each passed, flagged or blocked, and the milliseconds each added. The Guardrails Monitor page under Observability adds the figures up per guardrail and per day.
Errors
| Status | Where | Meaning | Fix |
|---|---|---|---|
400 Guardrail configuration error: ... | POST /guardrails, or Failed to create guardrail in the Admin UI | The integration refused the parameters, for example Presidio has no analyzer address. Nothing was stored. | Fill the field the message names, or set the Presidio environment variables. |
400 with the guardrail's reason, for example Blocked entity detected: CREDIT_CARD by Guardrail: pii-mask | Step 2 and step 4 | The guardrail blocked the text. This is the expected answer for a blocked word or a blocked entity. | Nothing to fix when the block is intended. Otherwise remove the word from the keyword list. |
403 Admin access required to manage guardrails | Step 1 and step 3 | The key is not a proxy admin. | Use the master key or an admin virtual key. |
403 The 'guardrails' feature needs a Spanway enterprise license. ... | Step 3 | The gateway has no license with the guardrails feature. | Use Always On or a policy attachment, or add a license. |
404 Guardrail 'pii-mask' not found | Step 2 | The name in guardrail_name does not match a guardrail. | Copy the name from the Guardrails page. |
500 Prisma client not initialized | Step 1 | The gateway has no database, so guardrails can only come from the config file. | Set DATABASE_URL, or use the config.yaml form above. |
Request fails with Presidio PII analysis failed: <error type> | Step 2 and step 4 | The gateway could not reach the analyzer or anonymizer, so the request fails. | Check the two service addresses and that the gateway can reach them. |
Logs
The gateway lines these steps can write, such as Immediate sync: Successfully initialized guardrail, are listed on guardrails and in the log reference.
Related
- Guardrails for every mode, integration and the rules that decide which guardrails run.
- PII masking with Presidio for the entity list, block versus mask, and restoring values in the response.
- Guardrail policies to combine guardrails into reusable sets.
- Custom code guardrails and the generic guardrail API for checks the built-in integrations do not cover.
- Apply guardrail for the test endpoint used in step 2.
- Guardrails page for every control of the Admin UI page.