Skip to content

Best Practices for Coding With Agents

The patterns that make the difference between a frustrating agent session and one that actually ships something useful.

  • 2 min read
  • Theory and practice
  • Day 3 of 7

Theory

Agents are not just faster autocomplete. They require a different way of working. The engineers who get the most out of them share a few consistent habits.

Start with a plan

Before asking an agent to write code, ask it to plan. Most tools support a dedicated plan mode or you can simply ask: "Before you write anything, describe what you would do step by step and wait for my approval." This surfaces misunderstandings early, when they're cheap to fix.

A study from the University of Chicago found that experienced developers are more likely to plan before generating code. The same applies when directing agents.

Keep context clean and intentional

Agents don't magically know your codebase. They use what's in the context window. Some guidelines:

  • Tag specific files when you know they're relevant. Don't dump everything in.
  • Let the agent search for context when you're not sure what's relevant. Modern tools have good codebase search.
  • Use rules files (.cursor/rules/, Github instructions, or equivalent) to provide persistent project-level context like coding conventions, commands, and architectural decisions.
  • Start fresh sessions when you move to a new task. Long sessions accumulate noise and the agent can lose focus.

Know when to stop and redirect

If you see the agent heading in the wrong direction, press Escape and redirect immediately. Don't let it keep going and hope it corrects itself. The longer it goes in the wrong direction, the more expensive it is to unwind.

Use tests as the target

Agents perform best when they have a clear, verifiable goal. Tests provide that. The TDD loop works especially well with agents:

  1. Ask the agent to write tests first.
  2. Confirm the tests fail.
  3. Ask the agent to write code that passes the tests, without modifying the tests.

This gives the agent a concrete signal for "done" and lets it iterate without needing your input at every step.

Review every diff

AI-generated code can look right while being subtly wrong. Read the diffs. The faster the agent works, the more important your review process becomes. Don't approve changes you haven't read.

Run agents in parallel for hard problems

For difficult problems, run the same prompt against two or three different models and compare the results. Picking the best output from multiple independent attempts often produces better results than iterating on a single one.

Practice

Run this in a repository you already know, not a toy project. The point is to feel where the practice helps and where it gets in the way on code that has history.

Pick a real task from your current work, something small but non-trivial (a new endpoint, a refactor of a function, a new test suite).

  1. Before writing a single prompt, write the task as a one-paragraph spec: what you want, what constraints apply, what "done" looks like.
  2. Run the agent in plan mode first (or ask it to plan before acting). Review the plan and make at least one correction before approving.
  3. Implement using the TDD loop: tests first, then implementation.
  4. At the end, review the entire diff before accepting anything.

Reflect: where did having a plan help? Where did the agent need the most correction? What would you do differently next time?

The rest of day 3

  1. 01What AI Coding Agents Are Out There
  2. 02Getting Started With an AI Coding Assistant
  3. 03From Chat to Plans: A First Step Into Spec-Driven Development
  4. 04Best Practices for Coding With AgentsYou are here
  5. 05Cloud Agents: Delegating Work in the Background
  6. 06Automated Code Review With AI
Where this comes from

This path is the shortest route to the ideas. AI-Native Software Engineering (O'Reilly Media) is where each one is worked out in full, with the patterns, the trade-offs and the failure modes. The pillars cover the foundations one long essay at a time.