Rules That Agents Actually Follow: Lessons from Guardrailing a Next.js and Sanity Site
Writing rules for an AI coding agent is easy. Writing rules it actually follows is the hard part. We learned that while locking down a Next.js 16 and Sanity site where SEO scores, hero animations and cache settings all had to survive every edit. These are the lessons that were worth the effort.

Writing rules for an AI coding agent is easy. Writing rules it actually follows is the hard part. We learned that while locking down a Next.js 16 and Sanity site where SEO scores, hero animations and cache settings all had to survive every edit. These are the lessons that were worth the effort.
1. Your agent may not read the files you wrote
We split our rules into focused files: seo.md, performance.md, hero.md, plus on-demand audit.md and think.md. Each had tidy frontmatter, with triggers like glob and model_decision. It looked professional.
Then a ChatGPT-based agent planned a homepage SEO task and never opened seo.md.
The cause was simple. Those trigger fields are conventions of one tool. Another agent treats them as plain text. The one file nearly every coding agent loads automatically is AGENTS.md, so that file has to act as the router and the safety net.
Two changes fixed it:
- A router section in
AGENTS.mdthat says which file to read for which task, and to read it before planning, not just before editing. - A receipt: every plan must start with
Rules read: <files or none>. If the line is missing, you know instantly that the agent skipped its homework.
The receipt turned out to be the most useful single line in the whole setup.
2. Lock behavior, not files
"Don't touch articles.ts" is a bad rule, because that file also holds legitimate data fetching. What you actually need to protect is behavior: canonicals, robots decisions, sitemap membership, cache lifetimes, animation timing.
We wrote two locks, one for SEO and one for performance, each listing protected behaviors and stating that unrelated edits in shared files must preserve them. The agent can still work in those files. It just can't change what they guarantee without permission.
3. Approval has to name its scope
The weakest point in most rule sets is the word "approved". A casual "go ahead" can end up authorizing anything.
Our fix: approval for locked work must name its scope, and a bare "go ahead" counts only if the plan it follows named that scope. We also gave the agent a shortcut trigger, implement:, for fully specified tasks. When we asked the agent to describe our triggers, it told us implement: could skip planning even for locked work if the scope was named. We had created a loophole ourselves.
The correction was one sentence: locked work always needs a plan first, and implement: never skips it.
4. Test your rules like code
That loophole surfaced because we ran small, read-only test prompts in fresh chats. Examples:
- "List the exact triggers I use and what each does." This checks that
AGENTS.mdwas read, and shows how the agent interprets it. - "Plan only: improve the homepage mobile LCP." The plan should start with a receipt, ask for a baseline and refuse to guess.
- "Plan only: remove
export const instant = falsefrom a route to simplify the code." The agent should push back.
Each test took a minute and found something real. Rules you haven't tested are just hopes.
5. Rule files go stale, and good agents notice
Our hero.md said the desktop marquees were hidden on mobile with CSS and still loaded. When we asked the agent to plan a mobile marquee optimization, it replied that the code already skips rendering below 1024px and loads the marquees dynamically, and that the rule file was out of date.
So write rules as claims that can be checked against the code, and encourage mismatch reports. A rule that quietly contradicts reality is worse than no rule.
6. Make "no change" a valid answer
One agent proposed adding explicit lazy loading to images that already lazy-load by default, then admitted it expected no speed improvement. Agents like to produce edits.
Add a rule that fixes this directly: if no measurable gain is expected, recommend no change. Pair it with an evidence rule. Optimize only with an audit or trace and a comparable baseline, and measure on a production build, in a clean browser profile, as the median of three runs. A single Lighthouse run on localhost is not evidence, and we caught reports that mixed localhost and production results.
7. Next.js 16 and Sanity: the cache is where mysteries live
Our longest debugging thread started with a simple symptom: article titles seemed stale. The investigation found several separate causes, any of which could produce it:
- The browser tab and social titles prefer a Sanity
metaTitlefield, so a filled-in value overrides an edited title. - Drafts never reach the public page, because the article query fetches published content only.
- Production cached articles for an hour, with no Sanity webhook configured and no revalidation secret set, so publishing couldn't trigger a refresh.
- The latest deployment had failed, so production was running older code, which made every other observation unreliable.
- On localhost, development mode skips most caching, and Sanity can't reach a localhost webhook, so some staleness can't be reproduced there at all.
The lesson is to separate "what the code does" from "what the live system is doing". Check the deployment first, then the content, then the cache.
8. Protect the things scores can't see
Performance scores reward removing things. An agent optimizing for a number will happily shorten animations, drop a slide or swap a visual effect. We wrote the protected list explicitly: the hero slider's cycle and crossfade, the marquees and their edge fade. We also forbade retiming them for a score.
The interesting part was what the agent did with that freedom. When a desktop report showed layout shift, it proposed reserving space for the marquee columns, a change that fixes the number without touching any visible behavior. That's the shape of fix the rules are meant to produce.
The short version
- Treat
AGENTS.mdas the router and the safety net. Treat other files as detail. - Demand a
Rules read:receipt in every plan. - Lock behavior, name the scope in every approval, and never let a shortcut skip planning on locked work.
- Test the rules with tiny prompts in fresh chats.
- Let the agent report where the rules and the code disagree.
- Allow "no change" as an answer, and trust only production measurements.
Good guardrails don't make an agent less capable. They make its mistakes small, visible and cheap to catch.