Skip to main content

Audit mode and policy replay

A new guardrail policy can block requests your users rely on. Audit mode lets you find out first. An attachment in audit mode runs the policy's guardrails on every matching request, records what each guardrail would have done, and lets the request through unchanged. When the recorded verdicts look right, you switch the attachment to enforce and the same policy starts blocking and redacting. Policy replay answers the same question without waiting for new traffic. It takes the newest stored requests of one team or one key and runs them through the policy on the spot.

In technical terms, an audit attachment turns the policy's guardrails into detect only pipeline steps. Each step scans its own copy of the request or response, so a masking guardrail cannot change what is sent, and every verdict lands in the request's guardrail_information with an audit_policy field naming the policy. A guardrail that already runs for the request through an enforcing attachment, the key, team or project, the model deployment, the request body or default_on keeps enforcing, so audit mode never weakens a guardrail that was already in force. An audit attachment also never switches off a default attachment, so a team keeps its baseline policy while a draft is watched next to it. When a guardrail is enforced through the model deployment, the audit attachment may record one extra verdict for it next to the enforced one.

A real-world scenario​

The support team wants a new pii-strict policy that blocks any prompt with a social security number. Before turning it on, the admin attaches it to the support team in audit mode and replays the last 24 hours of the team's traffic. The replay shows that 3 of 50 requests would have been blocked and that the masking guardrail would have redacted 11 more. The admin opens those requests on the Logs page, sees that two of the blocks are false positives from ticket numbers, tunes the guardrail, watches the live audit verdicts for a day, and then switches the attachment to enforce.

Attach a policy in audit mode​

  1. Open the Admin UI at /ui/policies, click the Attachments tab and click + Add New Attachment.
  2. Pick the policy, choose the scope, and turn on Audit only (shadow mode).
  3. Click Create Attachment. The table shows the attachment with an Audit only badge in the Mode column.

The same with curl:

curl -X POST http://localhost:4000/policies/attachments \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY" -H "Content-Type: application/json" \
-d '{"policy_name": "pii-strict", "teams": ["support"], "mode": "audit"}'

In config.yaml, set mode: audit on the attachment.

policy_attachments:
- policy: pii-strict
teams: [support]
mode: audit

Read the audit verdicts​

Open the Logs page at /ui/?page=logs, click a request from the team and open its guardrail details. Each audit verdict has the usual status, for example guardrail_intervened for a check that would have blocked, plus audit_policy: pii-strict. The request itself shows the model's normal answer, and the policy does not appear in the x-spanway-applied-policies response header, because it did not act on the request. The same entries are in the spend log of the request.

curl "http://localhost:4000/spend/logs?request_id=$CALL_ID" \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY"

Switch to enforce​

  1. On the Attachments tab, open the menu at the end of the attachment's row.
  2. Click Switch to enforce. The badge changes to Enforce and the next request is enforced.
curl -X PUT http://localhost:4000/policies/attachments/$ATTACHMENT_ID/mode \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY" -H "Content-Type: application/json" \
-d '{"mode": "enforce"}'

Switch to audit only in the same menu, or {"mode": "audit"}, moves an enforcing attachment back to audit. Each switch writes an updated row to the audit log with the attachment before and after. When one policy has both an enforcing and an audit attachment that match a request, the policy is enforced.

{{ops:^/policies/attachments/{attachment_id}/mode$}}

Replay recent traffic​

Replay needs stored request bodies, so turn on general_settings.store_prompts_in_spend_logs: true some time before you replay. Requests stored without a body are counted as requests_without_content and skipped.

  1. Open /ui/policies and click the Policy Simulator tab.
  2. Under Replay recent traffic, pick the policy or one of its stored versions, for example pii-strict (v2, draft), choose Team ID or Key hash under Traffic of, type the team id or the key's Key ID from the Virtual Keys page, and set Last hours.
  3. Click Replay traffic. The table shows, per guardrail, how many requests it would have blocked, redacted or passed.
curl -X POST http://localhost:4000/policies/simulate \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY" -H "Content-Type: application/json" \
-d '{"policy_name": "pii-draft", "team_id": "team-support", "hours": 24, "sample_size": 50}'

policy_name replays the production version of a policy. To replay a stored version, drafts included, send its policy_id instead, which is what the Admin UI does when you pick a version. To try a policy that does not exist yet, send guardrails with a list of guardrail names instead. hours goes up to 168 and sample_size up to 200, newest requests first.

MethodPathSummary
POST/policies/simulateReplay recent traffic through a policy

Expected result​

An audit attachment never blocks, masks or rewrites a request or a response, and streamed responses reach the client as the provider sends them. Every matching request gets one guardrail information entry per audited guardrail with audit_policy set, and the guardrail's own cost is still counted, because the check really ran. A replay answers with requests_considered, requests_replayed, requests_without_content, requests_blocked and per guardrail blocked, redacted, passed and errored counts. It calls no model and moves no spend, budget or rate limit counter, and it writes one invoked audit row with the request and the counts, never the replayed prompts.

Audit mode covers guardrails that run on requests (pre_call) and on responses (post_call). A guardrail that only runs during_call is left out of an audit attachment, and replay covers request guardrails only. A replay lists the guardrails it could not run under skipped_guardrails with the reason.

Errors​

StatusErrorMeaningCauseFix
403Only proxy admins can replay stored trafficReplay is refused.The key is not a proxy admin key. Replays read other users' stored prompts.Use a proxy admin key.
403Only proxy admins can change attachment modesThe mode switch is refused.The key is not a proxy admin key.Use a proxy admin key.
404Policy or policy version not foundReplay is refused.No production policy has that policy_name, or no stored version has that policy_id.Check the name or version on the Policies tab, or send guardrails.
404Attachment with ID <id> not foundThe mode switch is refused.The id is wrong, or the attachment comes from the config file.Copy the id from the table, or change mode in the config file.
422Value error, set exactly one of policy_name, policy_id or guardrails, Value error, set exactly one of team_id or key_hashThe replay body is not valid. The message is in the msg field of the detail list.None or more than one of the policy fields, or both or neither of the scope fields, were sent.Send one policy field and one scope field.
503No database connectedReplay or the mode switch is refused.The gateway runs without a database.Connect a database.
400Team '<alias>' does not exist. Reference an existing team or use a wildcard pattern ...Creating the audit attachment with POST /policies/attachments is refused.A team alias, key alias or model in the attachment does not exist.Fix the alias, or end it with * to make it a pattern.
404Policy '<name>' not found. Create the policy first.Creating the audit attachment is refused.No policy has that name.Create the policy on the Policies tab first.
500Database not connectedCreating the audit attachment is refused.The gateway runs without a database.Connect a database, or attach the policy in config.yaml.

Logs​

LevelLineMeaningAction
DEBUGPolicy engine: audit policies [...] run N detect only pipeline(s)The request matched audit attachments and that many audit pipelines were added.None. Set SPANWAY_LOG=DEBUG to see it.
ERRORPipeline: unexpected error from guardrail '<name>': ...An audited or replayed guardrail failed with something other than a block, for example a timeout. The request was not affected, and the verdict is counted as errored.Check the guardrail's provider and credentials.
WARNINGPipeline: guardrail '<name>' not found in callbacksThe policy names a guardrail that is not loaded. Audit records an error for it and the request goes through.Create the guardrail or remove it from the policy.

Guardrail policies explains policies and attachments. The request inspector shows every guardrail verdict, the routing and the budgets of one request before you send it. The Policies page describes every control on the page, and the audit log lists the rows these actions write.