How to constrain an AI coding agent's scope
Constrain a coding agent's scope with path fences, forbidden actions, exact verify commands, and a done-when stop rule.
Published here · 5 sources · Resume reading
Part 2 of 2 in the Guide the agent series. Part 1 is how to stop an AI coding agent from over-engineering.
Constrain a coding agent's scope by writing four constraints into the instruction file it reads: path fences, forbidden actions, verify commands, and a done-when stop rule. Claude Code docs target under 200 lines per CLAUDE.md [1]. Codex loads instruction files up to 32 KiB combined by default [3]. In Claude Code, use a PreToolUse hook for hard blocks [1].
Write four cards into the memory file
Put four blocks in CLAUDE.md or AGENTS.md before the next task:
- Paths. Name folders the agent may edit, and folders it must leave alone.
- Forbidden. Ban new dependencies, new top-level folders, and drive-by refactors unless the request asks for them.
- Verify. Paste the exact lint and test commands for this repo. Do not let the agent invent a parallel check.
- Done when. The request is met, the checks pass, then stop. If the request is ambiguous in a way that changes the design, ask one question and wait.
The CLAUDE.md docs ask for instructions concrete enough to verify, for example "Use 2-space indentation" instead of "Format code properly" [1]. Claude Code best practices put a rule that only matters in one part of the codebase in .claude/rules/ with a paths: filter, so it loads only when Claude works there [2]. Codex best practices say add rules after repeated mistakes rather than dumping a novel up front [4]. Anthropic's overeagerness prompting still belongs in the request when you need a one-shot scope fence; put durable rules in the repo file [5].
A teaching composite (not an official Anthropic or OpenAI template):
## Done when
- Only modify paths under: src/ and tests/ for this task
- Do not add dependencies, new packages, or new top-level folders
- Run: <exact test command> and <exact lint command>
- If the request is ambiguous in a way that changes the design, ask one question and wait
- Stop after the request is met; do not also improve nearby codeKeep the file under the product size cap
Validate that a draft constraints file stays small before you paste a bigger one:
python3 - <<'PY'
sample = '''## Scope
- Edit only files under src/
- No new dependencies
## Done when
- pytest -q passes for tests/ touched
- Do not refactor unrelated modules
'''
print('lines', sample.count('\n')+1)
print('bytes', len(sample.encode()))
assert len(sample.encode()) < 32*1024
print('under_codex_default_cap OK')
PYYou should see a small line count, a byte count under 32768, and under_codex_default_cap OK. Claude Code's under-200-lines target is separate guidance for CLAUDE.md [1]. Codex's 32 KiB default caps the combined size of the instruction files it loads, and you can raise it [3].
Catch vague rules before they ship
Claude Code best practices warn that if you fix every review finding, you end up with extra abstraction layers and tests for cases that can't happen [2]. The CLAUDE.md docs say that if two instructions contradict each other, Claude may pick one arbitrarily [1]. If a line keeps getting ignored, either the file has grown long enough that the line gets lost, or the rule belongs in a hook [2].
Pair this page with Part 1 for the paste-ready stop block and the bloated-vs-small diff review how to stop an AI coding agent from over-engineering. For the loop definition behind these tools, see what is an AI agent.
Paste the four cards, keep the file short, and reject the next diff that invents work outside the request.
Send this to the friend whose coding agent keeps editing files it was never asked to touch.


Keep this guide
Filed under AI agents, Claude Code
Questions about this guide
Does CLAUDE.md enforce file limits by itself?
No. Claude Code docs treat CLAUDE.md as project memory the model reads. Enforcement needs permissions, hooks, or CI. Keep the file short so the guidance still gets attention.
How big can Codex AGENTS.md get?
OpenAI's AGENTS.md guide says Codex stops adding instruction files once their combined size reaches project_doc_max_bytes, 32 KiB by default. Raise the limit or split instructions across nested directories when you hit the cap.
What fails when constraints stay vague?
Claude Code docs ask for instructions concrete enough to verify, such as "Use 2-space indentation" instead of "Format code properly". If two instructions contradict each other, Claude may pick one arbitrarily. In a long file a line can get lost, and a rule that cannot have exceptions belongs in a hook.
Sources
- Claude Code: CLAUDE.md, accessed 2026-11-13code.claude.com
- Claude Code best practices, accessed 2026-11-13code.claude.com
- OpenAI: AGENTS.md guide, accessed 2026-11-13learn.chatgpt.com
- OpenAI Codex best practices, accessed 2026-11-13learn.chatgpt.com
- Anthropic: Claude prompting best practices, accessed 2026-11-13platform.claude.com