Skip to main content
AI Pair Programming: Tips to Get Better Code

AI Pair Programming: Tips to Get Better Code

KU
· Founder & Chief Editor
• 8 min read•Updated •61 views

Practical habits that turn AI coding assistants from a slot machine into a reliable pair, from context and prompts to verification.

Key takeaways

  • Better AI pair programming comes from habits, not tools.
  • Give the assistant the real files, a pattern to copy and a project instructions file such as AGENTS.md, state acceptance criteria up front, and work in small steps you read and test.
  • After two failed corrections, clear the context and write a better prompt.

To get better code from an AI pair, give it the files and conventions it cannot guess, state what "done" means before it writes anything, and work in small steps you read and test. Most bad output is a model guessing at context it could have been shown. The rest is unread diffs that got merged.

Verified against

Claude Code (AGENTS.md support from v2.1.277), GitHub Copilot repository custom instructions, against the code.claude.com memory and best-practices docs, the docs.github.com Copilot instructions page and agents.md. Checked October 2026.

Same model, same editor, wildly different results: the gap between people who love AI assistants and people who think they produce garbage is almost always habits. These are the ones that move the needle.

Put your conventions where the tool reads them

The cheapest fix is a project instructions file, because it stops you correcting the same drift on every request. Every major tool reads one now. AGENTS.md is the cross-tool format, stewarded by the Agentic AI Foundation under the Linux Foundation and read by Codex, Cursor, Gemini CLI, Copilot's coding agent and others. Claude Code reads CLAUDE.md and, from v2.1.277, falls back to AGENTS.md when no CLAUDE.md exists. Copilot reads .github/copilot-instructions.md and also honors AGENTS.md.

Keep it short and concrete. Claude Code's docs suggest under 200 lines per file and instructions "concrete enough to verify":

AGENTS.md

## Commands
- Install: `pnpm install`
- Test one file: `pnpm vitest run path/to/file.test.ts`
- Typecheck: `pnpm tsc --noEmit` (must pass before you say "done")

## Conventions
- TypeScript strict. No `any`. Errors are `AppError` from `lib/errors.ts`.
- Data access goes through `lib/db.ts`; never import the driver directly.
- New dependencies need a note in the PR description explaining why.

## Never
- Edit files under `migrations/` that are already merged.
- Read or print `.env*` files.

If your team uses Claude Code alongside other tools, keep one source of truth and import it from CLAUDE.md with a line containing @AGENTS.md. Remember these files are context, not enforcement: the docs say so plainly and point to hooks for anything that must never happen.

Feed sharp context, not long prompts

Before you ask for code, make sure the tool can see the types it will touch, a similar function to copy, the test file and the config. In Cursor or Claude Code you point at files or let it search; in a chat window you paste them. Ten lines of the real interface beat three paragraphs describing it.

Models imitate well and guess house style badly, so name a pattern: "write this the way handlers/orders.ts is written." Your codebase is the best style guide you have.

State intent and acceptance criteria

Vague requests get vague code.

Before:

add caching to the user service

Tweet this ↗

After:

Add an in-memory cache to getUserById in user-service.ts. Use the existing LRUCache from lib/cache.ts (see how product-service.ts uses it). TTL 60 seconds, max 1000 entries. Invalidate the entry on updateUser. Keep the signature unchanged. No new dependency. Done means pnpm vitest run user-service passes and pnpm tsc --noEmit is clean.

Tweet this ↗

The second names the file, the pattern, the constraints, and the check that proves it worked. Claude Code's best-practices page puts it the same way: name the file, describe the symptom, say what "fixed" looks like.

For anything non-trivial, ask for a plan first: "Outline the approach and the files you'll change, then wait." A plan is cheap to correct; a 400-line diff built on a wrong assumption is not. This is the instinct behind spec-driven development.

Small steps, each one verified

AI output looks equally confident when right or wrong, so confidence carries no signal. Keep the loop tight: generate, read, run, correct. Ask for one function, not one feature. A check the tool can run itself closes the loop:

Terminal
pnpm vitest run src/user-service.test.ts
pnpm tsc --noEmit
git diff --stat
 src/user-service.ts      | 18 ++++++++++++++----
 src/user-service.test.ts | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 4 deletions(-)

That --stat line is the cheapest review there is. If a "small cache change" touched nine files, stop and ask why before reading any of them. Never merge code you have not read; if you would not approve it from a colleague, do not approve it from a model.

Tests are the spec and the net

Written first, tests give the model a concrete target. Written after, they catch wrongness that reads fine. Review generated tests like any code: assistants tend to assert whatever the code currently does, bug included. Look for real edge cases (empty input, the cache-miss path, the invalidation) rather than "returns something."

The failure modes, with the errors they produce

What you seeWhat happenedFix
error TS2339: Property 'findByEmail' does not exist on type 'UserRepo'.The model invented a method it assumed existedShow it the real interface file and rerun
error TS2307: Cannot find module './lib/cache' or its corresponding type declarations.Guessed path, no file contextPoint at the actual file; add the layout to AGENTS.md only if it is non-obvious
npm error 404 Not Found - GET https://registry.npmjs.org/...A hallucinated package nameNever install an unknown package on a model's word; check the registry and maintainers first
ModuleNotFoundError: No module named '...'Same problem in PythonSame fix; pin dependencies you actually chose
Tests pass but behavior is wrongTests were written to match the codeWrite or review the assertions yourself

The hallucinated-package case is a security issue, not a typo: attackers register names models commonly invent. Our guide to securing AI-generated code covers it with the other patterns to look for, such as string-built SQL and missing validation.

Know when to stop prompting

When you have corrected the same thing twice and it is still wrong, a third correction mostly adds noise to a conversation that already holds two failed attempts. Claude Code's docs give the same rule: "after two failed corrections run /clear and write a better prompt instead of a third." Read the code yourself, find the real problem, and either write the fix or hand over a much narrower request.

Clear between unrelated tasks too. Long sessions accumulate stale files and abandoned approaches that pull the model off course, and you pay to resend all of it on every turn. In Claude Code, Esc stops a drifting action and keeps the work so far; /rewind rolls code and conversation back to an earlier prompt.

Where this breaks

These habits assume a codebase with tests and a typechecker. Without them, "verify each step" means manual testing, and the speedup shrinks fast. Instructions files are guidance the model can ignore, so anything that must never happen (touching production credentials, force-pushing) belongs in permission rules or hooks, not prose. Exploratory spikes and throwaway scripts do not need this discipline; the rules matter once the code is going to be maintained. And none of it fixes a bad architecture decision you asked for clearly; more generated code is not more shipped software, as we argue in why AI agents write more code but teams do not ship more.

Questions people ask

How do I get better code from AI coding assistants?

Give the assistant the context it cannot infer: the real files and types, a similar function to copy, and a short instructions file like AGENTS.md with commands and conventions. State acceptance criteria, including the test or typecheck that proves it works. Then work in small steps you read and run. Most bad output comes from guessed context or unread diffs.

What should go in an AGENTS.md or CLAUDE.md file?

Build and test commands, the conventions a newcomer would get wrong, and a short list of things never to do. Keep each instruction specific enough to check, such as "run pnpm tsc --noEmit before saying done." Claude Code's docs recommend staying under 200 lines. Skip architecture descriptions the tool can read from the code itself, and use hooks for hard rules.

When should I stop prompting and write the code myself?

After two failed corrections on the same problem. At that point the conversation contains two wrong attempts the model keeps rereading, so a third rarely helps. Clear the session, read the code to find the actual cause, and either fix it yourself or start fresh with one narrower prompt that includes what you learned.

The call we'd make

Treat the assistant like a fast junior who has read everything and remembers nothing about your project. Write an AGENTS.md this week, give every request a definition of done the tool can run, and keep diffs small enough to read. If you are still picking a tool, our roundup of the best AI coding assistants compares them; the habits matter more than the pick.

Sources

Explore topics:AI
React
Share this post
Cite this article
Plain text

Kiril Urbonas (2026). AI Pair Programming: Tips to Get Better Code. DevOpsNess. Updated 2026-11-15. https://www.devopsness.com/blog/ai-pair-programming-tips

Markdown

[AI Pair Programming: Tips to Get Better Code](https://www.devopsness.com/blog/ai-pair-programming-tips) (Kiril Urbonas, DevOpsNess, updated 2026-11-15)

KU

Kiril Urbonas

Founder & Chief Editor

604 articles
View all articles by Kiril Urbonas

You might have missed

Evergreen posts worth revisiting.