How to Get an AI Coding Agent to Write Smaller PRs

Open-ended prompts let AI coding agents refactor whatever they want. Scope-boundary prompts, diff budgets, and a task-splitting checklist keep their pull requests small and reviewable.

Steve Jefferson
Steve Jefferson
Developer Advocate
22 August 20261 min read

If you want to know how to get an AI coding agent to write smaller pull requests, the short answer is: stop asking it to "fix the bug" or "add the feature," and hand it a scope boundary it cannot cross. Coding agents default to maximal effort. Given an open-ended prompt, they'll refactor adjacent code, rename variables they find confusing, and "improve" files you never mentioned. The fix isn't a smarter model, it's a smaller instruction: name the exact files, cap the change, forbid drive-by edits. Below are the prompt templates, a before/after diff, and a checklist that make this repeatable.

Why AI agents default to sprawling diffs

Coding agents are reinforced to be helpful, and helpful gets interpreted as thorough. Ask an agent to fix a null-pointer error in a payment handler, and it notices the file has no tests, the naming is inconsistent, and a nearby function duplicates logic. A human contributor would leave a comment; an agent with write access and no explicit boundary just fixes all of it in the same commit. That's a mismatch of incentives: the model optimizes for "did I solve the underlying problem well," not "did I produce a diff a human can review in four minutes." A 400-line PR that also renames variables and touches unrelated files is, by the model's own judgment, better engineering. It's also a worse pull request. That's the core tension behind smaller ai generated diffs: the agent needs a definition of "done" that includes size and blast radius, not just correctness. State that constraint, or it won't be inferred, even when the context screams "one-line fix."

The scope-boundary prompt: your default template

Reach for this on almost any task. It forces three decisions before the agent writes a line: which files are in bounds, what "done" looks like, and what's explicitly out of bounds.

Task: <one sentence describing the exact change>

Files you may edit: <explicit file paths, nothing else>
Files you may read for context: <paths, if different from above>

Definition of done:
- <the specific behavior that must change>
- <the specific behavior that must NOT change>

Out of scope, do not touch even if you notice a problem:
- Renaming variables, functions, or files
- Reformatting code you did not otherwise change
- Adding tests for pre-existing untested code
- Refactoring anything outside the files listed above

If you think a change outside this scope is needed, stop and describe it in your
final response instead of making it.

That last line matters. Without it, an agent that spots a real problem outside the boundary will often fix it anyway, reasoning that flagging and not fixing is worse. A sanctioned place for that observation, a note in the response rather than a diff, satisfies the same instinct without the side effect.

Template two: the diff-budget prompt

For tasks where you know roughly how big the change should be, put a number on it. Agents respond to numeric constraints more reliably than qualitative ones like "keep it small," which they'll happily reinterpret.

Task: <description>

Diff budget: aim for under <N> changed lines across no more than <N> files.
If this genuinely can't be done within budget, stop before writing code, explain
why, and propose a split into smaller PRs instead.

Do not use extra room just because you have it. A smaller diff that solves 90% of
the problem beats a larger one solving 100%, if the remainder can be a follow-up.

That last line does real work. Left alone, an agent that finishes under budget with room to spare will often keep going, reading spare capacity as an invitation to be thorough. Telling it that unused budget is fine, not a failure, removes that pressure.

Template three: the task-splitting prompt

Sometimes the request is too big for one PR, and the agent should say so before writing anything. Use this when you suspect a task should become two or three pull requests but haven't decided how to split it.

Before writing any code, break this task into an ordered list of pull requests,
each independently reviewable and safe to merge on its own (or behind a feature
flag if it isn't). For each PR, give me:
1. A one-sentence description
2. The files it touches
3. Whether it depends on a prior PR in the list

Wait for me to approve the split before implementing PR 1.

This trades a little round-trip time for a large reduction in review burden. You see the shape of the work before any code exists, the cheapest point to catch a bad decomposition. If the agent proposes bundling a migration with a UI change, you split it before the diff exists.

A worked example: before and after

A generalized real pattern. The task: add rate limiting to a single API endpoint getting hammered by a misbehaving client.

Before: open-ended prompt

"Add rate limiting to the /api/export endpoint so it can't be abused."

With full repo access, a typical agent run adds a rate-limiting middleware module, wires it into the endpoint, also wires it into two other endpoints "for consistency," adds a config file, updates the README, and renames `handler.go` to `export_handler.go` for clarity. Total: 7 files changed, roughly 260 lines. A reviewer now has to verify middleware logic, two endpoints nobody asked to touch, a rename that breaks git blame, and a doc change, all in one sitting.

After: scoped prompt

"Task: add a token-bucket rate limiter to the /api/export endpoint only, capped at 10 requests per minute per API key. Files you may edit: api/export_handler.go, api/middleware/ratelimit.go (new file is fine). Do not touch any other endpoint, do not rename existing files, do not update the README. If you think other endpoints need this too, say so in your response instead of adding it."

Same underlying task, same model. The resulting diff: 2 files changed, 74 lines, one new middleware function and one call site wiring it in. The agent's response included a note: "The /api/import endpoint has a similar public-facing shape and might benefit from the same limiter, consider a follow-up PR." That's the sanctioned outlet from the templates above doing its job, the observation surfaces without becoming unrequested code. A two-file, 74-line diff is something most reviewers verify against the stated requirement in a few minutes; a 260-line diff spanning seven files, several untouched by the actual ask, takes longer, because each file needs its own justification.

A task-splitting checklist

Run through this before you send a task to an agent, especially one you expect to take more than a few minutes.

  • Can I name the exact files this touches, or only a feature area? If the latter, the agent picks the file list for you, generously.

  • Does this bundle more than one of: a schema change, a public API change, a UI change, a config change? Each is a reasonable place to split.

  • Is there a version that ships value without the "nice to have" parts? Ship that first, list the rest as follow-ups.

  • Reading this diff cold, would every changed line make sense? If a rename or reformat wouldn't pass that test, forbid it up front.

  • Have I told the agent what to do with out-of-scope observations? If not, expect them to show up as code instead of comments.

Why this makes reviews faster, not just diffs smaller

A smaller diff isn't automatically a faster review if it's incoherent, an unrelated rename mixed into a fix makes a 50-line diff harder to review than a clean 150-line one. The templates above target coherence as much as size: every changed line should trace back to the one-sentence task. When you review ai pull requests faster, you're mostly checking whether the diff does what it claims, and only that. Scoped prompts make both checks fast because there's no unexplained code to reconcile against the stated intent.

This connects to how you write a pull request description for AI-generated changes: a tightly scoped diff produces a description that's easy to write truthfully, since it only has to account for what's actually there. A sprawling diff gets a vague description hiding the extra changes, or an honest one reading like a changelog for three different tasks.

When a bigger diff is the right call

None of this means every PR should be tiny. Some changes are genuinely indivisible: a data model migration touching every place the old shape was assumed, or a dependency upgrade with breaking changes across a dozen call sites. Forcing those into artificial fragments creates a worse problem, a series of PRs where none individually compile or pass tests. The goal of prompt ai coding agent scope work isn't "small at all costs," it's "as small as the task honestly allows." The task-splitting template works because it makes the agent argue for the diff's size before writing it, not after.

Once the diff lands, the review itself still matters. See how to review AI-generated code before you ship it for what to check regardless of size, and whether AI agents should merge their own pull requests for where the human checkpoint belongs.

Putting it together

Start every non-trivial task with the scope-boundary template. Add the diff-budget template when you have a rough size in mind. Reach for task-splitting whenever a request feels like two or three tasks wearing a trench coat. None of this needs a different model, only saying out loud what you'd otherwise leave implicit. This also pairs well with how you use an AI coding agent to review a pull request: scoped prompts on the way in mean less surface area on the way out.

FAQ

Why does my AI coding agent keep changing files I didn't ask about?

Because you gave it read access to the whole repository but no boundary on write access. Without a stated file list, the agent treats "fix this" as license to touch anything it judges relevant. Naming the exact files it may edit removes the ambiguity. For more ways to enforce that boundary, see how to stop an AI coding agent from editing unrelated files.

How many lines should an AI-generated pull request be?

There's no universal number, but many teams find diffs under roughly 200 to 300 lines stay reviewable in one sitting. Use the diff-budget prompt to set a target per task rather than relying on a fixed rule.

Does limiting scope make the agent worse at fixing the actual problem?

No, it changes what counts as "done." A scoped prompt still asks the agent to solve the stated problem completely, it just removes permission to solve adjacent problems in the same commit. Follow-up issues surface in the response instead of silently landing in the diff.

What's the fastest way to tell if an agent's PR went out of scope?

Check the file list against the task description before reading code line by line. If a file appears that isn't obviously required by the one-sentence task, question it first.

How did this land?

About the author

Steve Jefferson
Steve Jefferson

Developer Advocate

Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.

Share

Get the next post in your inbox

One email a month. Product updates, engineering posts, and the best of Built with Swarmz.

I agree to receive emails about AI building tips and Swarmz product news. Unsubscribe any time.