Skip to main content
In this guide, you create a Firewall policy that stops your agents from force-pushing, test it on one request, watch what it catches in Dry run, and then enforce it on your access keys.

What you’ll need

  • An ngrok.ai account and an access key.
  • An agent or app that calls /v1/chat/completions, /v1/responses, /v1/messages, or Gemini generateContent through the gateway with tools.
  • To apply a policy to some keys and not others, an access key configuration for those keys.

1. Create a policy

  1. In app.ngrok.ai, go to Firewall and click New firewall policy.
  2. Enter a Name, such as Coding agent guard.
  3. Under Mode, select Dry run. New policies start in Enforce, so switch now and enforce after you’ve tested.
  4. Under On error, keep Block to block what Firewall can’t check once you enforce, or select Continue to let it through.

2. Add rules

The new policy comes with one example rule under Response rules. Edit it or remove it, then click Add rule for each rule you need. For each rule, pick:
  • Rule name: a short description. The agent sees it when the rule blocks something, so make it clear, such as Force push.
  • Type: Tool names, Regex, or Decision. See Rule types.
  • Tier: Hard block, Soft block, or Allow. See Tiers.
  • Reads: for a request rule, which parts of the request the rule reads.
Put a rule under Request rules to check what the agent sends before it reaches the model. Put it under Response rules to check each tool call the model returns. For a decision rule, describe what to catch in plain language, and leave Min. confidence % empty to use the default: 60 for a block rule and 70 for an allow rule. Raise it if the rule blocks too often, and lower it if it misses things. Under Environment details, add facts that help decision rules judge a call, such as Deploy target: staging only or Trusted repo: github.com/acme/app. Environment details aren’t rules, so they never block anything by themselves.

Example rules

Regex rules are case-sensitive. Start a pattern with (?i) to ignore case. Click Create policy to save it.

3. Try it in one request

Before you apply the policy to any keys, test it on a single request. Under Try it in one request, the policy editor shows a plugins field that carries the same rules. Add it to the body of a chat completions or messages request, and the gateway checks that request with those rules. This request offers the model a bash tool and asks it to push, so the model returns a tool call for the rule to check:
If the model calls bash with a force push, the response still returns 200, but the tool call is gone. In its place, the assistant message says the call was blocked, finish_reason is stop, and safeguard_results reports the verdict:
The response is trimmed to the fields Firewall changes. Change mode to audit to see what Dry run reports: the tool call comes back unchanged, and safeguard_results still shows flagged. See the Firewall reference for the full plugins format and every response field.

4. Apply the policy to keys

Open the policy, find Apply to, and pick one:
  • All access keys: the policy becomes your baseline and checks every key in your account, including keys you create later. Your account has one baseline, so this replaces the current one.
  • Only these configurations: the policy checks keys that use the configurations you pick.
Click Save changes. Saved changes reach the gateway within seconds. You can also pick the policy from an access key configuration. Open the configuration, go to its Firewall section, and select the policy under Policy.
Policies combine. A key whose configuration picks a policy is also checked by your baseline. Each policy keeps its own mode, so a Dry run policy only reports, even next to an enforced baseline. See How policies combine.

5. Watch, tune, and enforce

While the policy is in Dry run, check what it would block:
  • Read the Ngrok-AIG-Firewall-Request header for request rules, such as flagged; rules=prompt-injection.
  • Read safeguard_results in the response body for both request rules and tool calls.
When a rule flags calls it shouldn’t, raise its confidence, narrow its pattern, or add an allow rule next to a soft block rule. When the results look right, open the policy, select Enforce under Mode, and click Save changes.
Once a key has an enforced policy with On error set to Block, the gateway rejects that key’s requests to endpoints Firewall can’t check, such as the Gemini Interactions API. Use a separate key for those requests, or set On error to Continue to let them through unchecked.

Use the API instead

You can do every step in this guide with the AI Gateway API. Authorize each request with your AI Gateway API key. Create a policy with the Firewall Policies API. The API uses the same fields as the dashboard, and each rule sets its own tier:
Attach the policy to an access key configuration:
Send "firewallPolicyId": null to detach it. Or make it your baseline with the gateway configuration API. Send expectedFirewallPolicyId with the baseline you read, or null if you expect none, so the update fails with a 409 instead of replacing a baseline someone else just set:
To enforce the policy, update it with "mode": "enforce".

Next steps