Guardrails as code: the rules that matter belong outside the model
AI-assisted research. This note was prepared with the assistance of AI tools. It may contain errors and is not professional advice. See our Legal Disclosures.
Most teams begin governing agents by writing rules into the agent's instructions: never mention competitors by name, never promise specific results, always include the disclosure. These instructions help, but they are not guarantees.
Models follow instructions most of the time, not all of the time, and a determined input can sometimes talk them out of a rule. For rules that matter, we recommend a different approach: enforce them outside the model, in code. In our research, the line between what an agent is asked to do and what the system lets it do is one of the most useful distinctions in agent governance.
Why instructions are not enough
An instruction is one input among many. The model weighs it against the brief, the retrieved context, the examples, and whatever the user or a form submission says. Most of the time the instruction wins. Under pressure from a long context, an unusual request, or text designed to override it, the instruction can lose.
That failure rate may be small, but marketing agents run at volume. A rule that holds in almost every case will still break somewhere across thousands of outputs. For a style preference, an occasional miss costs an edit. For a missing legal footer or a message to someone who opted out, a single miss can be a real problem.
Instructions also leave no trace when they fail. If the model ignores a rule, nothing in the system records it. A check in code either passes or blocks, and both outcomes can be logged.
Two kinds of guardrails
We separate guardrails by where they live, because that decides how they fail.
Soft guardrails live in instructions. They shape behavior and are useful for style, tone, and preferences. When they fail, the result is usually a draft that needs editing. Our post on brand voice as a specification covers how to write them well.
Hard guardrails live in the system around the agent. They check outputs or restrict actions regardless of what the model decides. When they trigger, the action is blocked or routed to a person.
Mature agent programs use both: soft guardrails to guide, and hard guardrails to protect. The question for each rule is which kind it should be.
What belongs in code
Good candidates for hard guardrails share two traits: the rule is important, and it can be checked mechanically. We suggest starting with these six.
- Banned terms and claims: a list of words and phrases that must never appear, checked before anything ships.
- Required elements: disclosures, unsubscribe links, or legal footers that must be present in certain content types.
- Spending limits: caps on budget changes per action, per day, and per campaign.
- Audience restrictions: blocks on messaging contacts without valid consent or in excluded regions.
- Action allowlists: the specific tools an agent may call, with everything else denied by default.
- Rate limits: a maximum number of actions per period, which limits damage if something goes wrong.
Some of these sit on outputs and some on actions. Output checks read what the agent produced before it moves on. Action checks sit inside the tools, so a tool refuses to run when its limits are exceeded. Our post on tool design for agents describes how to build limits into tools rather than around them.
For legal and compliance rules, have counsel confirm the wording and scope before you encode them. A check is only as good as the rule it enforces, and a rule written from a general summary may not match what applies to you.
What stays with humans
Some rules need judgment and cannot be checked mechanically: whether a comparison is fair, whether a joke lands, whether an image suits the moment. These belong in review flows, not code.
The goal of hard guardrails is to catch the clear violations automatically so reviewers can spend their attention on the judgment calls. A reviewer who no longer has to scan every email for a missing footer can read for tone and accuracy instead.
A worked example
Consider a hypothetical team running a paid social agent. Its instructions say never to raise any campaign budget by more than 15% in a day. For weeks, the agent follows the rule.
Then a product launch brings a surge of strong results, and a brief from the campaign manager tells the agent to push hard on what is working. The agent weighs the brief against the instruction and raises one budget by 40%. Nothing stops it, because the rule existed only in the prompt.
With a hard guardrail, the budget tool itself rejects any change above 15%. The agent receives a clear message naming the rule, and the request goes to the campaign manager for approval. The manager may well approve it. The difference is that a person decided.
Design tips
A few design choices separate guardrails that help from guardrails that frustrate. We recommend four.
- Fail closed: when a check cannot run, block the action rather than allow it.
- Explain the block: tell the agent and the reviewer which rule triggered, so the issue can be fixed quickly.
- Log every trigger: guardrail logs show where agents struggle and where rules may be too strict.
- Version your rules: treat guardrail lists like code, with changes reviewed and recorded.
Versioning matters more than it seems. When a product name changes or a promotion ends, the rule set changes with it. If you cannot tell which version of the rules applied to a given output, you cannot explain why it passed.
What to measure
The trigger log is the main source of evidence. We suggest reviewing it monthly for three things: which rules trigger most, which agents trigger most, and how many triggers were false positives that a reviewer overrode.
A rule that triggers constantly on one agent usually points to a weak instruction, which is cheaper to fix in the prompt than to keep blocking. A rule with many overrides may be too broad. Test new rules against past outputs before switching them on, and add the attacks that get past them to your red-teaming library.
Hard guardrails are also what make higher autonomy safe. Our autonomy ladder diagram shows hard guardrails enforced in code from Level 2 upward. The question we are studying next is whether a second model can check judgment-heavy rules, such as fair comparison. We want to learn if that filter adds enough reliability to justify its cost and its false positives.
How to apply this: list the five rules your agents must never break, and ask whether each one can be checked mechanically. Move every rule that can be checked into code, confirm legal rules with counsel first, and keep the rest in human review.
Related research
Consent-aware personalization: agents should respect preferences by design
Agents make personalization cheap, so consent matters more. Preference rules need to be enforced by the system, not just requested in the prompt.
Approval queues that don't become bottlenecks: keeping review real and fast
Human review is the safety net for agent work until the queue grows faster than people can clear it. Good approval design keeps oversight real and fast.
Red-teaming marketing agents: find the failure modes before customers do
Deliberately trying to make your agents fail is one of the fastest ways to make them safer. Here is a practical red-teaming process for marketing teams.
Put this research to work
Marketix turns these patterns into software: agents, automated workflows, and a hub that keeps people in control. Sign up for early access.
