Skip to main content

Guardrail policies

A policy is a named set of guardrails. It separates what runs from where it runs: the policy says which guardrails belong together, and an attachment says which traffic gets the policy, globally or for some teams, keys, models or tags. A policy can inherit another policy and add or remove guardrails, apply only to models matching a pattern, and run its guardrails in a fixed order with a pipeline. Policies are versioned, so you can draft a change, test it, publish it and promote it to production without touching the version that is running. This keeps large deployments manageable, because one policy change replaces editing guardrails on hundreds of keys.

A real-world scenario​

A company runs forty teams through one gateway. Everyone must get PII masking, and the research team, which uses the most capable models, must also get prompt injection detection on those models only. The platform team creates global-baseline with pii-mask and attaches it to everything, then creates frontier-safety, which inherits global-baseline, adds prompt-injection and applies only to models matching claude-sonnet-.*, and attaches it to the research team. When a new guardrail is approved three months later, the team adds it to global-baseline as a draft, tests the draft, promotes it, and all forty teams get it at once.

Create and attach a policy​

  1. Create the baseline policy. In the Admin UI open Policies under AI Gateway, open the Policies tab and click + Add New Policy. Keep Simple Mode and click Create Policy. Enter global-baseline as the Policy Name, pick pii-mask under Guardrails to Add, leave Inherit From empty, and click Create Policy. A new policy is saved as version 1 in production. Over the API:

    curl -X POST http://localhost:4000/policies \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{"policy_name": "global-baseline", "description": "Base guardrails for all requests", "guardrails_add": ["pii-mask"]}'
  2. Create the inheriting policy. Click + Add New Policy again, enter frontier-safety, pick global-baseline under Inherit From, pick prompt-injection under Guardrails to Add, choose Custom Regex Pattern under Model Condition Type and enter claude-sonnet-.*, then click Create Policy. The Resolved Guardrails box under the fields shows the final set, pii-mask and prompt-injection. Over the API:

    curl -X POST http://localhost:4000/policies \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{
    "policy_name": "frontier-safety",
    "inherit": "global-baseline",
    "description": "Extra checks for frontier models",
    "guardrails_add": ["prompt-injection"],
    "condition": {"model": "claude-sonnet-.*"}
    }'

    Expected result: a 200 with the stored policy, version_number 1 and version_status production. GET /policies/{policy_id}/resolved-guardrails returns ["pii-mask", "prompt-injection"].

  3. Attach them. Open the Attachments tab and click + Add New Attachment. Pick global-baseline under Policies, keep Scope Type at Global (applies to all requests) and click Create Attachment. Then add a second attachment with frontier-safety, Scope Type set to Specific, research under Teams, claude-sonnet-* under Models and 10 under Priority. Estimate Impact shows how many keys and teams it will reach before you save. Over the API:

    curl -X POST http://localhost:4000/policies/attachments \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{"policy_name": "global-baseline", "scope": "*"}'

    curl -X POST http://localhost:4000/policies/attachments \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{"policy_name": "frontier-safety", "teams": ["research"], "models": ["claude-sonnet-*"], "priority": 10}'

    Expected result: both rows appear in the attachment table, the first with Global (*) under Scope. From the next request on, every key runs pii-mask, and research team requests to a matching model also run prompt-injection.

  4. Check what a request would get. Open the Policy Simulator tab, pick research under Team Alias and a claude-sonnet-5-5 model, and click Simulate. Effective Guardrails lists pii-mask and prompt-injection, and Matched Policies shows global-baseline matched via scope:* and frontier-safety via team:research. The API form and the other checks are on validate and simulate policies.

Policies and attachments can also live in config.yaml, which suits a gateway deployed from a repository:

policies:
global-baseline:
description: Base guardrails for all requests
guardrails:
add: [pii-mask]
frontier-safety:
inherit: global-baseline
description: Extra checks for frontier models
guardrails:
add: [prompt-injection]
condition:
model: "claude-sonnet-.*"

policy_attachments:
- policy: global-baseline
scope: "*"
- policy: frontier-safety
teams: ["research"]

Config file policies show on the Policies page with a Config badge and can only be changed in the file. A database policy with the same name takes over once its production version exists.

Versions​

Every policy saved through the API or the Admin UI is versioned. A version has a version_status of draft, published or production, and only the production version is enforced.

  1. Open a draft. On the Policies tab choose Edit policy in the row menu, which opens the Flow Builder, and click + New Version in the Versions sidebar. Over the API:

    curl -X POST http://localhost:4000/policies/name/frontier-safety/versions \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{"source_policy_id": "0b3f6c2d-8e4a-4f1b-9c7d-5a6e8f9b0c1d"}'

    Expected result: a copy of the production version with the next version_number and version_status draft. Leave out source_policy_id to copy the current production version.

  2. Change the draft. In the Flow Builder change the steps and click Save as New Draft. Over the API only drafts can be changed:

    curl -X PUT http://localhost:4000/policies/f2c4a6e8-1b3d-4f5a-9c7e-8d0b2a4c6e81 \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{"description": "Extra checks for frontier models", "guardrails_add": ["prompt-injection", "pii-mask"]}'
  3. Compare and promote. In the policy details view click Compare versions, pick the two versions and click Compare to see the changed fields, or call GET /policies/compare?version_a=...&version_b=.... In the Flow Builder click Publish on the draft, then Promote to production. Over the API:

    curl -X PUT http://localhost:4000/policies/f2c4a6e8-1b3d-4f5a-9c7e-8d0b2a4c6e81/status \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{"version_status": "published"}'

    curl -X PUT http://localhost:4000/policies/f2c4a6e8-1b3d-4f5a-9c7e-8d0b2a4c6e81/status \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{"version_status": "production"}'

    Expected result: the new version is enforced on the next request and the previous production version drops back to published. A production version cannot be set back to published directly, which answers 400. To change what is enforced, promote another published version, or delete the policy to stop enforcing it.

{{ops:^/policies(/list|/{policy_id}|/{policy_id}/resolved-guardrails|/{policy_id}/status|/compare|/name/{policy_name}/versions|/name/{policy_name}/all-versions)?$}}

Attachments​

An attachment connects a policy to traffic. Its fields are combined, so an attachment with teams and models applies only to those models for those teams.

FieldMeaning
policy_nameThe policy to attach.
scope* applies the policy to every request.
teamsTeam aliases, or patterns ending in *. An alias without a wildcard must exist.
keysKey aliases, or patterns ending in *.
modelsModel names or patterns such as claude-sonnet-*.
tagsRequest tag patterns, with wildcards such as health-*, matched against key and team metadata tags and tags sent in the request.
priorityExecution order. Lower runs first, and attachments with a priority run before those without.
defaultApply only when no non-default attachment matches the request.
modeenforce (the default) blocks and redacts. audit runs the policy's guardrails in detect only mode and records what they would have done. See audit mode.
MethodPathSummary
POST/policies/attachmentsAttach a policy to teams, keys, models or tags
POST/policies/attachments/estimate-impactEstimate how many keys and teams an attachment would reach
GET/policies/attachments/listList policy attachments
GET/policies/attachments/{attachment_id}Read one policy attachment
DELETE/policies/attachments/{attachment_id}Delete a policy attachment
PUT/policies/attachments/{attachment_id}/modeSwitch an attachment between enforce and audit

Pipelines​

A policy can carry a pipeline with a mode of pre_call or post_call and ordered steps. Each step names a guardrail and says what happens on_pass (next, allow, block or modify_response), on_fail and, for a technical error such as a provider outage, on_error. With a pipeline the guardrails run one after another in that order instead of all at once. The Flow Builder in the Admin UI draws the steps and its Test Pipeline panel runs them on a sample message, see validate and simulate policies.

{"mode": "pre_call", "steps": [{"guardrail": "pii-mask", "on_pass": "next", "on_fail": "block"}]}

Testing policies​

To try a policy on real traffic, attach it in audit mode or replay recent traffic through it with POST /policies/simulate. To see everything one request would go through, including routing, cost and budgets, use the request inspector. POST /policies/test-pipeline and POST /utils/test_policies_and_guardrails run a sample request through the matching policies and report which guardrails would run and what they would do, without calling a model. Policy templates for common needs are available from /policy/templates. Policy usage per guardrail is shown on the Policies page in the Admin UI.

Errors​

StatusWhereMeaningFix
400 Policy with name 'frontier-safety' already existsPOST /policies, Failed to save policyThe name is taken.Open a new version of the existing policy instead, or pick another name.
400 Only draft versions can be updated. Publish or create a new version to change published/production.PUT /policies/{policy_id}The version is published or in production.Create a draft with POST /policies/name/{policy_name}/versions and change that.
400 Cannot promote draft directly to production. Publish the version first.PUT /policies/{policy_id}/statusA draft cannot go straight to production.Publish first, then promote.
400 Only draft versions can be published. Current status: '<status>'.PUT /policies/{policy_id}/statusOnly a draft can be published, so a production version cannot go back to published.Promote another version instead.
400 Team '<alias>' does not exist. Reference an existing team or use a wildcard pattern ...POST /policies/attachmentsA team alias, key alias or model without a wildcard does not exist. The message names a key or a model the same way.Fix the alias, or end it with * to make it a pattern.
400 Parent policy '<name>' not found or Policy '<name>' inherits in a cycle: <name> -> <name> -> <name>GET /policies/{policy_id}/resolved-guardrailsThe inherit chain names a policy that has no production version, or loops. The route resolves the version you name, even a draft, and its parents at their production versions.Fix inherit on the draft. Validate policies on the Policies tab finds this before it bites.
404 Policy 'frontier-safety' not found. Create the policy first.POST /policies/attachmentsNo policy has this name.Create the policy, then attach it.
404 Policy with ID ... not found or Attachment with ID ... not foundRead, update, delete by idThe id does not exist.Copy the id from the list routes or the Admin UI tables.
401 Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. ...Any routeThe key is not a proxy admin.Use an admin key.
403 user not allowed to access this route, role= proxy_admin_viewer. ...Any writeA view only admin can read but not change policies.Use an admin key.
500 Database not connectedAny writeThe gateway has no database, so policies can only come from the config file.Set DATABASE_URL, or use the config.yaml form.
Attaching a policy to a key or team is refused as a premium featurePolicies field on a key or teamAttaching policies through key and team metadata needs a license that includes guardrails.Attach through POST /policies/attachments instead, which needs no license, or add a license.

Audit​

Every policy change writes an audit row on Spanway_PolicyTable or Spanway_PolicyAttachmentTable with who made it and the value before and after. Promoting a version writes one row for the promoted version and one for the production version it replaced, each with its status before and after. Deleting every version of a policy keeps all the deleted versions in the row's before value. See audit logs.

Logs​

LevelLineWhat it meansAction
ERRORError creating policy: <error> (also updating, deleting, creating policy version, updating version status, deleting all versions)A policy route failed and answered with the error.Read the error. A 500 usually means a database problem.
WARNINGCircular inheritance detected for policy '<name>'A policy's inherit chain loops, so the loop is cut while traffic is resolved.Action needed. Fix inherit.
INFOSynced <n> production policies and <n> draft/published (by ID) from DB to in-memory registry ...The gateway reloaded policies from the database.Normal.
  • Validate and simulate policies to check a policy configuration, see which guardrails a request gets, run a pipeline on a message and estimate an attachment's reach.
  • Guardrails for the guardrails a policy groups.
  • Policies page for every control of the Admin UI page, including templates and the Flow Builder.
  • Teams and virtual keys for attaching a policy to one team or key through its metadata.
  • Guardrails Monitor page for how often each policy's guardrails ran and blocked.