Validate and simulate policies
Policies decide which guardrails a request gets, and a mistake in one of them is expensive in both directions: a typo in a guardrail name silently protects nobody, and an attachment that is too broad blocks traffic for teams it was never meant for. The gateway offers a set of checks that answer these questions before a change goes live, without sending a request to a model. You can validate a configuration, simulate a request and see which policies match it, run a pipeline on a sample message, count the keys and teams an attachment would reach, and run a batch of prompts through a policy to see which ones are blocked. Each check is a button in the Admin UI and one API call, so it fits both a review in the browser and a deployment pipeline.
A real-world scenario
A platform engineer is about to attach a new frontier-safety policy to the research team. Before saving anything she validates the policy configuration and finds that prompt-injectoin is misspelled, so the policy would have protected nobody. After fixing it she simulates a request from the research team to a frontier model and sees both pii-mask and prompt-injection in the effective guardrails. She estimates the attachment and sees it would reach twelve keys across two teams, one more team than expected, and tightens the team pattern. Finally she runs the team's twenty test prompts through the policy on the Compliance tab, confirms the three injection attempts are blocked and the seventeen normal prompts pass, and only then creates the attachment.
1. Validate a policy configuration
Validation checks that every guardrail named by a policy exists, that every team and key alias without a wildcard exists, that parents exist and inherit without a cycle, and that scope patterns are well formed. Nothing is stored.
In the Admin UI open Policies under AI Gateway, open the Policies tab and click Validate policies. The gateway checks the production version of every policy and every policy from the config file. Over the API you send the policies map you are about to deploy, in the shape of the config file:
curl -X POST http://localhost:4000/policy/validate \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{
"policies": {
"global-baseline": {"guardrails": {"add": ["pii-mask"]}, "scope": {"teams": ["*"]}},
"frontier-safety": {
"inherit": "global-baseline",
"guardrails": {"add": ["prompt-injectoin"]},
"scope": {"teams": ["research"]}
}
}
}'
Expected result:
{
"valid": false,
"errors": [
{
"policy_name": "frontier-safety",
"error_type": "invalid_guardrail",
"message": "Guardrail 'prompt-injectoin' does not exist",
"field": "guardrails.add",
"value": "prompt-injectoin"
}
],
"warnings": []
}
errors block the configuration and warnings do not. The Admin UI shows Enforced policies are valid, or the count of blocking errors with one line per error. The error_type values are invalid_guardrail, invalid_team, invalid_key, invalid_model, invalid_inheritance, circular_inheritance, invalid_scope and invalid_syntax. Team and key aliases are only checked when the gateway has a database.
2. Simulate a request
The simulator resolves a team alias, key alias, model and tags against every attachment and reports the guardrails the request would run and why each policy matched.
In the Admin UI open the Policy Simulator tab, pick research under Team Alias, pick the model under Model, add any Tags and click Simulate. Every field is optional. Over the API:
curl -X POST http://localhost:4000/policies/resolve \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"team_alias": "research", "key_alias": "research-batch", "model": "claude-sonnet-5-5", "tags": ["health-eu"]}'
Expected result:
{
"effective_guardrails": ["pii-mask", "prompt-injection"],
"matched_policies": [
{"policy_name": "global-baseline", "matched_via": "scope:*", "guardrails_added": ["pii-mask"]},
{"policy_name": "frontier-safety", "matched_via": "team:research", "guardrails_added": ["prompt-injection"]}
]
}
The tab shows the same under Effective Guardrails and Matched Policies, or No policies matched this context. The simulator reads the in-memory copy of the attachments. Add ?force_sync=true to read them from the database first, for example right after another gateway instance changed them.
POST /policy/test is the older form of the same question for the enforced policies loaded from the config file. It takes the same team_alias, key_alias, model and tags and answers matching_policies and resolved_guardrails, and GET /policy/list and GET /policy/info/{policy_name} list those enforced policies with the guardrails each one resolves to.
3. Run a pipeline on a sample message
A policy with a pipeline runs its guardrails in order with an action per outcome. The pipeline test runs those steps on sample messages and reports each step's outcome and the final decision. The guardrails really run, so one that calls a paid service is billed.
In the Admin UI open a policy in the Flow Builder with Edit policy, click Test Pipeline, keep Test with at Quick chat (custom message), type a message and click Run Test. Over the API:
curl -X POST http://localhost:4000/policies/test-pipeline \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{
"pipeline": {"mode": "pre_call", "steps": [{"guardrail": "pii-mask", "on_pass": "next", "on_fail": "block"}]},
"test_messages": [{"role": "user", "content": "My card is 4111 1111 1111 1111"}]
}'
Expected result:
{
"terminal_action": "block",
"step_results": [
{
"guardrail_name": "pii-mask",
"outcome": "fail",
"action_taken": "block",
"modified_data": null,
"error_detail": "Blocked entity detected: CREDIT_CARD by Guardrail: pii-mask. This entity is not allowed to be used in this request.",
"duration_seconds": 0.084
}
],
"modified_data": null,
"error_message": "Blocked entity detected: CREDIT_CARD by Guardrail: pii-mask. This entity is not allowed to be used in this request.",
"modify_response_message": null
}
The panel lists each step with a PASS, FAIL or ERROR badge, the action taken and the time in milliseconds, and a Result box with the final action. Choosing a compliance dataset under Test with sends each prompt of the dataset in turn and reports how many matched the expected outcome.
4. Estimate an attachment's reach
Before creating an attachment, count the keys and teams it would apply to. Team patterns also count the keys of the matched teams, and keys and teams without an alias are counted separately.
In the Admin UI open the Attachments tab, click + Add New Attachment, pick the policy, choose Specific under Scope Type, fill the teams, keys, models or tags, and click Estimate Impact. The Impact Preview box shows the counts and up to five sample names of each. The eye button on an existing attachment row shows the same as Blast Radius. Over the API:
curl -X POST http://localhost:4000/policies/attachments/estimate-impact \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"policy_name": "frontier-safety", "teams": ["research*"], "models": ["claude-sonnet-*"]}'
Expected result:
{
"affected_keys_count": 12,
"affected_teams_count": 2,
"unnamed_keys_count": 1,
"unnamed_teams_count": 0,
"sample_keys": ["research-batch", "research-eval"],
"sample_teams": ["research", "research-eu"]
}
For a default attachment the counts are an upper bound, because it only applies when no other attachment matches.
5. Run prompts through policies and guardrails
This check runs the guardrails of one or more policies, or named guardrails, over a list of inputs and reports per input what was blocked or masked, without a model call. It is the batch form of apply guardrail and the engine behind the Compliance tab.
In the Admin UI open Playground under AI Gateway, open the Compliance tab, pick the policies and guardrails to test under Policies, select the prompts, and click Simulate. Over the API:
curl -X POST http://localhost:4000/utils/test_policies_and_guardrails \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{
"policy_names": ["frontier-safety"],
"inputs_list": [{"texts": ["My card is 4111 1111 1111 1111"]}, {"texts": ["Hello, can you help me?"]}],
"input_type": "request"
}'
Expected result:
{
"results": [
{
"inputs": {"texts": ["My card is 4111 1111 1111 1111"]},
"guardrail_errors": [
{"guardrail_name": "pii-mask", "message": "Blocked entity detected: CREDIT_CARD by Guardrail: pii-mask. This entity is not allowed to be used in this request."}
]
},
{"inputs": {"texts": ["Hello, can you help me?"]}, "guardrail_errors": []}
]
}
A blocked input is reported inside guardrail_errors, not as an HTTP error, so one bad prompt does not stop the batch. A masking guardrail returns the rewritten text in inputs. With agent_id set to a model or agent name, each input that passes is also sent to it and the reply is included. The Compliance tab shows the rows with their expected and actual outcome and can export them as CSV.
Policy templates have their own test: POST /policy/templates/test builds the template's guardrails in memory and runs them on one text, which is what Test Suggestions in the AI Policy Suggestion dialog uses.
{{ops:^/policy/(validate|test|list|info/{policy_name}|templates/test)$|^/policies/(resolve|test-pipeline|attachments/estimate-impact)$|^/utils/test_policies_and_guardrails$}}
Errors
| Status | Where | Meaning | Fix |
|---|---|---|---|
200 with valid: false | POST /policy/validate | The configuration has blocking errors. This is the check working. | Fix the field each error names and run it again. |
400 Invalid pipeline: ... | POST /policies/test-pipeline | The pipeline definition is not valid, for example an action other than next, allow, block or modify_response, or a mode other than pre_call and post_call. | Fix the step the message names. |
400 Policy '...' not found | POST /utils/test_policies_and_guardrails | A name in policy_names or guardrail_names does not exist. | Copy the names from the Policies and Guardrails pages. |
404 Policy '...' not found | GET /policy/info/{policy_name} | No enforced policy has this name, or no policies are loaded. | Check GET /policy/list. |
| 403 | Any of these routes | The key is not a proxy admin. A view only admin can read policies but not run checks. | Use an admin key. |
500 Database not connected | POST /policies/resolve, POST /policies/attachments/estimate-impact | The gateway has no database, so attachments and aliases cannot be read. | Set DATABASE_URL. |
Failed to resolve policies. Check the proxy logs. | Policy Simulator tab | The resolve call failed. | Read the gateway log for the error. |
| A guardrail reports an error in a pipeline or compliance test | Steps 3 and 5 | The guardrail's service could not be reached or refused the call. | Check the guardrail on the Guardrails page. In a pipeline, on_error decides what happens in production. |
Related
- Guardrail policies for creating policies, attachments, versions and pipelines.
- Apply guardrail to run one guardrail on one text.
- Policies page for the Validate policies button, the Policy Simulator tab, the Flow Builder test panel and the impact preview.
- Playground for the Compliance tab, its datasets and the CSV export.
- Guardrails Monitor page for what the policies did on real traffic afterwards.