AI agent rules files (declarative guardrails written in YAML or JSON) and runtime enforcement (checks applied during execution) are two layers of the same safety system: rules files define what an agent should do, while runtime enforcement verifies what it actually does. Neither alone is sufficient — a rules file without runtime checks is just documentation, and runtime checks without a rules file are ad hoc and unmaintainable. The real answer is that agents stay in bounds only when both layers work together, with the rules file as the source of truth and runtime enforcement as the mechanism that catches violations the rules didn’t anticipate.
Formula
The risk score is a simple heuristic: Risk = min(100, (steps / 10) + (tool calls / 4)). It combines two common agent limits — the number of reasoning steps and the number of external tool calls — into a single 0–100 score. t is the maximum allowed steps (default 50), m is the maximum allowed tool calls (default 20). The formula is not a standard, but a way to visualize how quickly an agent approaches a danger zone as it does more work.
Worked Example
With the defaults (t = 50 steps, m = 20 calls):
Risk = min(100, (50 / 10) + (20 / 4)) = min(100, 5 + 5) = 10.
So a default-config agent scores 10 out of 100 — low risk. If you raise the limits to 200 steps and 80 calls, the score becomes min(100, 20 + 20) = 40. The score grows linearly, so it is easy to reason about, but it does not capture the type of tool call (e.g., reading a file vs. deleting a database).
Background: Why Two Layers Exist
Early AI agents (chatbots with tool use) had no formal safety model. Developers wrote prompts like “be carefulâ€� and hoped for the best. As agents gained the ability to execute code, send emails, and modify files, the industry moved toward structured guardrails. Rules files emerged from infrastructure-as-code practices — you define allowed actions in a declarative format (e.g., allowed_tools: [read_file, search_web]) that can be version-controlled and reviewed. Runtime enforcement came from the observation that even perfect rules are useless if the agent doesn’t follow them. A rules file is static; runtime enforcement is dynamic. The two are complementary: rules files are easy to audit, runtime checks are hard to bypass.
How Rules Files Work
A rules file is a structured document (YAML, JSON, or TOML) that an agent reads at startup. It typically contains:
- Allowed tools — a list of functions the agent may call (e.g.,
search_web,read_file). - Denied domains — URLs or IP ranges the agent cannot access.
- Rate limits — max calls per minute or per session.
- Output constraints — required format, max length, prohibited content.
- Environment variables — API keys, timeouts, and other runtime parameters.
For example, a simple rules file for a customer-support agent might look like:
allowed_tools: - search_kb - get_order_status denied_domains: - "*.internal" max_steps: 30 max_tool_calls: 10
The agent loads this file before starting. The file is the contract — it defines the agent’s permissions in a way that humans can read and approve. However, nothing in the file itself stops the agent from violating it; that is the job of runtime enforcement.
How Runtime Enforcement Works
Runtime enforcement is the active layer that monitors the agent as it executes. It includes:
- Pre-execution checks — before each tool call, the system verifies the call is allowed by the rules file. If not, the call is blocked and logged.
- Real-time monitoring — the system tracks the agent’s state, including the number of steps, tokens used, and the content of outputs. If the agent exceeds a limit, it is paused or terminated.
- Sandboxing — the agent runs in a restricted environment (e.g., a container) where it cannot access files or network resources outside its allowed set, regardless of what the rules file says.
- Post-hoc auditing — after the run, logs are reviewed to detect violations that weren’t caught in real time.
A concrete example: an agent is told (via rules file) it can only read files in /data. At runtime, the enforcement layer checks the actual path of every read_file call. If the agent tries read_file("/etc/passwd"), the call is rejected with an error, and the event is logged. The rules file alone would not have prevented this — it only declared the policy. Runtime enforcement made the policy real.
Choosing the Right Balance
There is no one-size-fits-all answer; the right balance depends on your threat model and operational constraints. Consider these factors:
- Agent autonomy — if the agent operates without human approval (e.g., auto-reply emails), you need heavier runtime enforcement. If a human approves every action, a rules file may suffice.
- Complexity of actions — an agent that only reads web pages is low-risk; one that writes to a database needs strict runtime checks.
- Failure tolerance — if a violation is merely annoying (e.g., a wrong search query), a simple rules file is fine. If a violation could cause data loss or legal issues, runtime enforcement is mandatory.
- Development speed — runtime enforcement adds overhead and can slow down iteration. For prototyping, a rules file alone may be acceptable, but you must accept the risk.
A common middle ground is to use a rules file for initial configuration and then layer runtime checks on top of the most critical operations (e.g., any tool that modifies state). This gives you the auditability of declarative rules and the safety of active checks.
Common Problems and Misconceptions
One misconception is that a rules file is a “policy� that the agent will follow out of respect. In reality, agents are not inherently obedient — they are optimized to complete a task, and they may find ways around restrictions if not physically blocked. Another issue is rules-file bloat: as you add more and more rules, the file becomes unreadable and conflicts arise. For example, one rule might allow write_file and another might deny writing to /tmp — the agent may not know which takes precedence. Runtime enforcement can resolve conflicts by applying the most restrictive rule, but only if it is designed to do so.
A second problem is enforcement gaps — places where the runtime layer doesn’t check what the rules file declares. For instance, a rules file might say “no external network access,â€� but if the runtime sandbox allows DNS lookups, the agent can exfiltrate data via DNS. The solution is to test your enforcement layer with adversarial prompts, not just happy-path tests.
Safety and Best Practices
Regardless of which layer you emphasize, follow these safety practices:
- Least privilege — give the agent only the tools and permissions it needs for its task. If it doesn’t need to send emails, don’t include that tool in the rules file.
- Defense in depth — use both layers, and add a third: human oversight for high-stakes actions (e.g., “require approval before any write operation�).
- Log everything — both rules violations and normal operations. Logs are your only way to debug a violation after the fact.
- Regularly review rules — as the agent’s tasks evolve, update the rules file. A stale rules file is often worse than none, because it gives a false sense of security.
- Test with red-teaming — deliberately try to make the agent violate its rules to find gaps in enforcement.
Remember that no system is perfect — there will always be edge cases. The goal is to reduce risk to an acceptable level, not to eliminate it entirely.
Frequently Asked Questions
Can a rules file alone keep an agent safe?
No. A rules file is just a declaration of intent. Without runtime enforcement, the agent can (and sometimes will) ignore it, either due to a bug, a prompt injection, or a misunderstanding. You need at least basic runtime checks (e.g., tool allow-lists) to enforce the rules.
What is the most important runtime check to implement first?
Start with a tool allow-list that blocks any tool not explicitly listed in the rules file. This prevents the agent from calling unexpected functions, which is the most common way agents cause damage. Next, add a step limit to prevent infinite loops.
How do I handle prompt injection that tries to override rules?
Runtime enforcement should treat the rules file as immutable — the agent cannot modify it during execution. Also, separate instructions from data: if the agent reads untrusted content, ensure that content cannot change the agent’s system prompt or the rules file. Use output filtering to block any attempt to print or exfiltrate the rules.
Should I use a sandbox (container/VM) or just code-level checks?
Sandboxing is stronger because it restricts what the agent can do at the OS level, regardless of code bugs. Code-level checks are easier to implement and debug, but they can be bypassed if the agent finds a way to execute arbitrary code. For high-risk tasks, use both: code-level checks for fine-grained control, and a sandbox for coarse-grained isolation.
How often should I update the rules file?
Update it whenever the agent’s task changes, or after any incident that revealed a gap. A good rule of thumb is to review it at least once per release cycle. Also, monitor logs for “near-missâ€� events (e.g., blocked calls) and use them to refine the rules.
What is the difference between a rules file and a policy-as-code system?
Policy-as-code (e.g., OPA, Cedar) is a broader concept: it uses a programming language to define policies that can be evaluated at runtime. A rules file is usually a simple data format. Policy-as-code is more expressive and can handle complex conditions, but it requires more engineering. For most agent applications, a simple rules file plus runtime checks is sufficient.
Related
Formula used
in t | Max steps | steps | 50 | 1 | 1000 in m | Max tool calls | calls | 20 | 1 | 500 out risk | Risk score | 0-100 | min(100, (t / 10) + (m / 4)) | 0
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Mac Start Up Automatically After Power Failure?
- Expose Self-Hosted Service Securely Without Cloudflare Tunnel
- Nano Delivery for Functional Foods: What It Can and Can’t Do4.00
- Disable Apple Watch Turn-by-Turn Vibration While Driving
- Convert Play FLAC in iTunes: Full Guide
- Soda Foundation Charter: What It Means