Guardrails
A guardrail is a check that runs on the gateway around a model call. It reads the request before the model sees it, the response before the client sees it, or both, and it can let the content through, rewrite it, flag it for review, or block it. Because the check runs on the gateway, every application that calls through Spanway gets the same protection, nobody has to add code to each app, and every decision is written to the request log next to the cost and the routing.
Guardrails are for teams who need to promise something about model traffic: that personal data never reaches a vendor, that pasted credentials never leave the network, that a support bot never answers questions about competitors, or that tool calls from an agent stay inside a policy. You pick a guardrail, say when it runs, and decide who it applies to.
A real-world scenario
A customer support team puts a chat assistant in front of its agents. Agents paste whole email threads into it, and those threads carry customer names, email addresses and sometimes card numbers. Legal asks that card numbers never reach the model provider and that email addresses are masked. The platform team creates one pii-mask guardrail that masks email addresses and blocks card numbers, attaches it to the support team, and turns on a prompt-injection guardrail for everyone. From then on every request from the support team is cleaned before it leaves the gateway, a request that carries a card number is refused with a clear message, and the Guardrails Monitor page shows how many requests each guardrail evaluated, passed, blocked or flagged each day.
How a guardrail is described
A guardrail has a name, an integration, one or more modes and a switch that says whether it runs on every request.
| Field | Meaning |
|---|---|
guardrail_name | The name keys, teams, policies and request bodies use to refer to it. |
guardrail | The integration that implements it, for example presidio, lakera_v2, bedrock, spanway_content_filter, custom_code or generic_guardrail_api. |
mode | When it runs. pre_call checks the request before the model, post_call checks the response, during_call runs in parallel with the model call and holds the response, logging_only records without blocking, and pre_mcp_call, during_mcp_call and post_mcp_call run around MCP tool calls. A list runs it at several points. |
default_on | Run on every request without being attached to anything. Shown as Always On in the Admin UI. |
unreachable_fallback | For the providers that honour it, fail_closed (the default) refuses the request when the service cannot be reached, and fail_open lets it through and logs a critical error. Only some providers read it, and the others use their own switch, see guardrail providers. |
The gateway ships integrations for content filtering, PII detection, prompt injection detection, secret detection and policy services, including Presidio, Lakera, AWS Bedrock Guardrails, Azure AI Content Safety, Google Model Armor, OpenAI moderation, Pangea, Aporia, Guardrails AI, Prompt Security, Lasso, Noma, Pillar, Palo Alto Networks Prisma AIRS, Zscaler AI Guard, Cisco AI Defense, CrowdStrike AIDR and Microsoft Purview. Spanway's own guardrails include a keyword and pattern content filter, a code execution blocker, tool permission and tool policy guardrails for agents, an LLM as a judge guardrail, decision model guardrails and custom code guardrails. Any service you host can be plugged in with the generic guardrail API.
Set up a guardrail
These steps create the pii-mask guardrail from the scenario, attach it to a team and check that it works. Each step shows the Admin UI clicks and the equivalent API call. The Admin UI is at /ui on your gateway, and every curl example reads the master key or an admin virtual key from SPANWAY_API_KEY.
-
Create the guardrail. In the Admin UI open Guardrails under AI Gateway, click Add New Guardrail, then Add Provider Guardrail. On Basic Info enter
pii-maskas the Guardrail Name, pick Presidio PII as the Guardrail Provider, leave Mode atpre_call, set Always On to Yes, and click Next. On Configure PII Protection select the entities to mask and click Create Guardrail. The same call over the API: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: a
200with the stored guardrail, including itsguardrail_id, and a new row on the Guardrails page. The guardrail runs from this moment, no restart needed.You can also declare guardrails in
config.yaml, which is the right choice when the gateway is deployed from a repository:guardrails:- guardrail_name: pii-maskspanway_params:guardrail: presidiomode: pre_calldefault_on: true- guardrail_name: prompt-injectionspanway_params:guardrail: lakera_v2mode: pre_callapi_key: os.environ/LAKERA_API_KEYConfig file guardrails show on the Guardrails page too, but they can only be changed in the file.
-
Decide who it applies to. A guardrail with Always On runs for everyone. To run one only for some traffic, leave Always On at No and attach it. In the Admin UI open Teams, pick the team, click Edit and add the guardrail under Guardrails, or do the same on a key under Virtual Keys. 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"]}'Expected result: every key of the team runs
pii-maskon its next request. Settingguardrailson a team or a key needs an Enterprise license that includes theguardrailsfeature, and a gateway without one answers403. On any tier, turn Always On on, or group guardrails into a policy and attach it to the team, key, model or tag with a policy attachment, which needs no license. -
Send a request. A client can also ask for a guardrail by name in the request body, which is useful while you test:
curl -X POST http://localhost:4000/v1/chat/completions \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"model": "claude-sonnet-5-5","messages": [{"role": "user", "content": "Email jane.doe@example.com about her refund"}],"guardrails": ["pii-mask"]}'Expected result: the model receives
Email <EMAIL_ADDRESS> about her refundand answers normally. A request the guardrail blocks gets a400whose message names the guardrail and what it found, for exampleBlocked entity detected: CREDIT_CARD by Guardrail: pii-mask, and the model is never called. -
Check the result. Open Logs, click the request and read the Guardrails & Policy Compliance section, which lists each guardrail that ran, whether it passed, flagged or blocked, and the time it added. The Guardrails Monitor page under Observability counts the outcomes per guardrail and per day.
To check a guardrail on a piece of text without calling a model, use apply guardrail.
Which guardrails run on a request
A guardrail runs when any of these is true: it is Always On (default_on), it is attached to the calling key, team, project or organization through their guardrails field, a policy attached to the request includes it, or the client lists it in the request body under guardrails. A proxy admin can stop a team or key from skipping guardrails with the Disable Global Guardrails switch on the team or key, which is off by default and needs an Enterprise license to turn on.
Changing and removing guardrails
Database guardrails are edited from the Settings tab of the guardrail detail view, or with PUT /guardrails/{guardrail_id} for the whole definition and PATCH /guardrails/{guardrail_id} for a few fields. Stored secrets come back masked and are kept when you send the masked value back. An update that changes a destination such as api_base while keeping a masked secret is refused with a 400 until you enter the secret again, so a stored key never travels to a new host. Delete in the row menu, or DELETE /guardrails/{guardrail_id}, removes a guardrail at once. Keys, teams and policies that still name it are not changed, so review them first.
{{ops:^/guardrails(/list|/{guardrail_id}|/{guardrail_id}/info)?$|^/v2/guardrails/list$}}
Monitoring
GET /guardrails/usage/overview, GET /guardrails/usage/detail/{guardrail_id} and GET /guardrails/usage/logs report how often each guardrail was evaluated, passed, blocked or flagged, the cost of its provider usage and, per request in the logs, the time it took. A masked request counts as passed, and the average latency per guardrail is not tracked yet. The Guardrails Monitor page shows the same data. Every guardrail result is also stored with the request's spend log row.
Errors
| Status | Where | Meaning | Fix |
|---|---|---|---|
400 Blocked entity detected: ... by Guardrail: ... or Violated guardrail policy ... | Any model request | A pre_call or post_call guardrail blocked the content. The message names the guardrail and its reason. | Remove the content the message names, or change the guardrail's action from BLOCK to MASK where masking is enough. |
400 Guardrail configuration error: ... | POST /guardrails | The integration refused the parameters, for example a required URL is missing, or failed to start for any other reason. Nothing was stored and no audit row was written. | Fix the field the message names. The Admin UI shows the same text under Failed to create guardrail. |
400 {"error": "The destination (api_base) changed while the stored secret (api_key) was left out or sent back masked. Re-enter the secret to use it with the new destination."} | PUT and PATCH /guardrails/{guardrail_id} | api_base or another destination changed while the stored secret was sent back masked. | Enter the secret again in the same save. |
403 Admin access required to manage guardrails | Create, update, delete | The key is not a proxy admin. A view only admin gets this on every save. | Use an admin key. |
403 ... needs a Spanway enterprise license. | POST /guardrails | The provider needs a license, for example Hide Secrets. Nothing was stored. | Add a license, see secret detection. |
403 The 'guardrails' feature needs a Spanway enterprise license. ... | /team/update, /key/generate, /key/update with guardrails | Attaching guardrails to a team or key is licensed. | Add a license, or use Always On or a policy attachment instead. |
404 Guardrail with ID ... not found | Read, update, delete | No guardrail has this id. Config file guardrails cannot be changed through the API. | Check the id on the Guardrails page. |
422 Invalid guardrail configuration, update rejected: ... | PUT and PATCH /guardrails/{guardrail_id} | The new parameters do not initialize. The stored guardrail keeps running unchanged. | Fix the field the message names. |
500 Prisma client not initialized | Any write | The gateway runs without a database, so guardrails can only come from the config file. | Set DATABASE_URL, or declare the guardrail in config.yaml. |
500, 502, 503 or 408 with the provider's message, for example Akto guardrail service unreachable or Security scan failed - request blocked for safety | Any model request | A remote guardrail service could not be reached, timed out or failed, and it is set to block in that case. The log carries the provider's own line, for example Akto unreachable (fail-closed). | Fix the service, its key or its URL. Where letting unchecked traffic through is acceptable, set unreachable_fallback: fail_open for the providers that honour it, or the provider's own switch, both listed on guardrail providers. |
Gateway starts without a guardrail, ERROR line Skipping guardrail '<name>': invalid configuration, proxy is starting WITHOUT this guardrail: ... | Startup | A guardrail in config.yaml names an unknown provider, for example Unsupported guardrail: hide_secret, or has invalid fields. The gateway keeps starting without that guardrail. | Fix the value against the provider table and restart. |
Logs
| Level | Line | Meaning | Action |
|---|---|---|---|
| ERROR | Skipping guardrail '<name>': invalid configuration, proxy is starting WITHOUT this guardrail: <reason> | A guardrail from config.yaml did not start, so its traffic is not checked. | Fix the configuration and restart. |
| WARNING | ToolPolicyGuardrail: blocking tool(s) [...] (input_policy=blocked) | The tool policy guardrail refused a request that lists a blocked tool. | None, unless the block is a mistake. See tool registry. |
| WARNING or ERROR | Provider specific, for example Akto unreachable (fail-open) | A remote guardrail service could not be reached. With fail open the request went through unchecked. | Check the provider service. |
| INFO | Immediate sync: Successfully initialized guardrail '<name>' (ID: <id>) | A guardrail created through the API or the Admin UI runs now. | None. |
| WARNING | Rollback failed for guardrail '<id>': <error> | A create was refused and the row it wrote could not be removed. | Delete the guardrail on the Guardrails page. |
| ERROR | Error getting guardrails from db: <error> | The guardrail list could not be read and the call answered 500. | Check the database. |
Audit
Creating, replacing, changing and deleting a guardrail, and approving or rejecting a team submission, write audit rows on Spanway_GuardrailsTable with who made the change and the value before and after, secrets redacted. A refused change writes no row. See audit logs.
Settings
The gateway-wide guardrail settings are listed in the settings reference.
Related
- Guardrails quick start for a ten minute walkthrough with a test and a team attachment.
- PII masking with Presidio and secret detection for the two most used built-in guardrails.
- Guardrail policies to group guardrails and attach them to teams, keys, models and tags.
- Validate and simulate policies to see which guardrails a request would get before it is sent.
- Custom code guardrails and the generic guardrail API for your own checks.
- Pass-through guardrails for provider routes the gateway forwards as they are.
- Apply guardrail to run a guardrail on text without a model call.
- Guardrails page and Guardrails Monitor page in the Admin UI.