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 GeminigenerateContentthrough 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
- In app.ngrok.ai, go to Firewall and click New firewall policy.
- Enter a Name, such as
Coding agent guard. - Under Mode, select Dry run. New policies start in Enforce, so switch now and enforce after you’ve tested.
- 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.
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 aplugins 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:
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:
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.
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-Requestheader for request rules, such asflagged; rules=prompt-injection. - Read
safeguard_resultsin the response body for both request rules and tool calls.
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 owntier:
"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:
"mode": "enforce".
Next steps
- Firewall: How rules, tiers, and modes work.
- Firewall reference: Limits, headers, and response formats.
- Securing your gateway: Per-client keys and revocation.