Skip to content
mentoor

Topic 1 of 5 · 3 min read

Stating goals and constraints for agents

Goal: Write an agent instruction that states the goal, its reason and the constraints the change must respect.

Tell the agent the outcome you want, why you want it and what must stay untouched; an instruction with only a verb leaves the agent to guess the rest.

Check yourself · 1 of 3

Which instruction gives a coding agent the clearest goal?

Choose one answer

Why it matters

A coding agent such as Claude Code, Codex, Copilot's agent mode or Cursor starts each task knowing nothing about your intent. "Add caching" can be done in a dozen ways, and the agent will pick one confidently. If it picks a new dependency, a new pattern or a rewrite of a shared module, you pay for it in review. A goal plus constraints narrows the search before any code exists.

The four parts

  1. Goal. The observable outcome, in one sentence. "The invoice list loads in under one second for accounts with 10,000 invoices" beats "make it faster".
  2. Reason. One line on why. It lets the agent choose between two valid options. "Support tickets complain about timeouts" tells it to fix latency, not to tidy code.
  3. Must not change. Public function signatures, database schema, API response shape, files owned by another team, existing test expectations.
  4. Allowed and forbidden tools. Which libraries and patterns to use, and which not to add. "Use the existing redis client; add no new packages."

Keep constraints few and real. Each one should be something you would reject in review.

Before and after

Weak:

Add rate limiting to the login endpoint.

Better:

Goal: limit POST /login to 5 failed attempts per IP per 15 minutes, then return 429 with a Retry-After header.
Why: we are seeing credential-stuffing traffic, and the current limit is none.
Must not change: the response shape for successful logins, the users table schema, or any file under src/billing/.
Constraints: use the existing redis client in src/lib/redis.ts; add no new dependencies; follow the middleware style in src/middleware/.

The second version gives a reviewer a checklist. If the diff touches src/billing/ or adds a package, you can reject it by pointing at one line.

What goes wrong

  • Goal without a boundary. The agent "improves" nearby code, renames things and widens the diff beyond what you asked.
  • Constraints only in your head. "Obviously we do not add dependencies" is not obvious to a model. If it matters, write it down, or put it in a standing file such as AGENTS.md so every task inherits it.
  • Too many constraints. Twelve rules in one prompt compete with each other, and some get dropped. Keep the ones that would fail review.
  • Constraints as a wish. "Try to keep it small" cannot be checked. "Change at most two files" can.

Examples

A bug fix with a boundary

Instruction: Fix the crash when a cart has zero items at checkout. Why: it is the top error in this week's logs. Must not change the Cart type or any payment code. Add a test next to the existing ones in cart.test.ts.

The agent now has a goal, a reason and two boundaries. A diff touching payments/ is out of scope on sight.

Constraint as a standing rule

A rule you repeat in every task, such as "no new dependencies without asking", belongs in AGENTS.md or the tool's own instruction file. Per-task prompts then carry only what is specific: this goal, this reason, these files.

Key points

  • State the goal as an observable outcome, not just a verb like improve or fix.
  • Add one line of reason so the agent can choose between valid options.
  • List what must not change: signatures, schemas, response shapes and files owned by others.
  • Name allowed and forbidden libraries and patterns, because an agent will add a dependency if you do not say no.
  • Keep constraints few and checkable; each should be something you would reject in review.

Common mistakes

  • Writing only a verb and a noun, such as "add caching", and expecting the agent to share your design preferences.
  • Leaving out what must not change, so the agent rewrites shared code to make its own change easier.
  • Listing a dozen constraints so that important ones are lost among minor ones.
  • Writing unverifiable constraints like "keep it clean" that nobody can check in review.

Check yourself · 2 of 3

Why add a one-line reason to a coding instruction?

Choose one answer

Check yourself · 3 of 3

An agent added a new npm package to finish your task, though you never wanted one. What was the best fix to the instruction?

Choose one answer

Practice items, not counted towards your mastery.

Start here