← All cheatsheets

AI CLI

AI-Assisted Coding & Code Review — ship with an AI pair

Work with an AI pair the way you would a sharp junior: feed it the real context, demand a reviewable diff, prompt test-first, turn it loose on your own code as a bug/security reviewer, and never merge what you have not run and read.

Docs-checkedai-codingcode-reviewcopilotclaude-codeprompts

Quick reference — reach for these first

Give full contextpaste the file + exact error + constraintsThe model only knows what is in the prompt. Include the relevant code, the real stack trace, and the hard rules (runtime, deps, style) — not a paraphrase.
Ask for a diff, not prose"return a unified diff / patch, no explanation"A patch is reviewable and applyable. Prose forces you to re-transcribe and hides exactly what changed. Constrain the output shape up front.
Test-first prompting"write a failing test for X, then the fix"A concrete failing test pins the spec, gives the model a target, and gives you a pass/fail oracle instead of a vibe check.
Use AI as a real reviewer"find correctness, edge-case & security bugs, ranked by severity"Point it at bugs, not bikeshedding. Style is cheap; a missed null, a race, an injection is what a review is for.
Verify — run it, read itrun the tests + read every changed lineModels hallucinate APIs and are confidently wrong. Treat output as a draft from a stranger: execute it and read it before it touches main.

The AI pair-coding loop

The AI pair-coding loopContext to Prompt to AI draft to Verify to Commit, with a failing-test feedback arrow from Verify back to Context.Contextfile + exacterror + rulesPromptask for a difftest-first, smallAI drafta unifieddiff / patchVerifyrun testsread it allCommitsmall, 1purpose *fails: feed the failing test back & re-prompt* only step that changes the repo

Text equivalent: A left-to-right loop with five stages: Context (the file, the exact error, constraints), Prompt (ask for a diff or patch, test-first, small scope), AI draft (a unified diff), Verify (run the tests and read every line), and Commit (small, single-purpose). An arrow labeled 'fails: feed the failing test back' loops from Verify back to Context. Verify is drawn as the accented gate, and Commit is labeled as the only step that changes the repo.

Common tasks

Prompt · Review · Verify

PromptReviewVerify
Gather the exact context before promptingcollect: failing file, full error text, constraints (lang/version, deps, perf/security rules)you can paste a self-contained problem a stranger could act on
State the output format explicitly"Reply with a unified diff only. No prose. Keep the change under ~40 lines."response is a patch you can read top-to-bottom, not an essay
Prompt test-first for any non-trivial change"Write a failing test that reproduces BUG, then the minimal fix."test fails before the fix and passes after
Constrain scope to keep the diff reviewable"Change only FUNCTION in FILE. Do not refactor unrelated code."git diff touches only the files/lines you intended
Redact secrets before pasting codereplace API_KEY / DB_URL / tokens with CAPS placeholdersgrep the prompt for 'sk-', 'password', '.env' values → none present
Run an AI code review on your own diffpaste `git diff`, ask for correctness + edge-case + security issues ranked by severityyou get a ranked list with file:line references, not generic advice
Feed the failing test when a review misses the bug"This test still fails: TEST_OUTPUT. Re-review the diff against it."the model's next finding explains the actual failure
Ask for reasoning when output looks confidently wrong"Explain why this is correct and name the API you used + its source."reasoning is checkable and the named API exists in the docs
Verify every invented API against official docssearch the library's official reference for each new symboleach function/flag the model used appears in the docs with that signature
Run it locally before trusting itapply the patch, run the unit tests and the lint/typecheck gategreen gates; you read every changed line and understand it
Keep each AI change a small, single-purpose commitstage only the intended hunks; conventional-commit messageone logical change per commit, reviewable in under a minute
Sanity-check for false positives on a clean baselinerun the review prompt on an unchanged / known-good diffreviewer reports few or no issues — high noise means a weaker prompt

Run an AI code review that catches a planted bug

Review

1
Use a copy-ready review prompt that fixes the output shape and asks for real defects ranked by severity. A capable reviewer model (e.g. latest Claude Opus 4.x for depth, or Sonnet for speed/cost) handles this well.
You are reviewing a pull request diff. Find CORRECTNESS, EDGE-CASE, and SECURITY bugs only — skip style. For each issue output: severity (critical|high|medium), file:line, the bug in one sentence, and a minimal fix. Rank critical first. If you find nothing, say "no issues found". DIFF:
<PASTE_DIFF_HERE>
2
Sample input — a small diff with one real, planted bug (user input concatenated straight into SQL = injection) plus a quieter missing-null risk.
3
Expected findings — the reviewer flags the injection as critical and the unguarded index as a lower-severity edge case, each with file:line and a fix.
4
Evaluate the reviewer, don't just read it. Did it catch the planted SQL injection (true positive)? Then run the SAME prompt on a clean, known-good diff and count issues — a reviewer that invents problems on clean code (high false-positive rate) will drown real findings. Keep the prompt only if it is both sensitive (catches the plant) and specific (quiet on clean input).
# control run: paste a known-good diff → expect "no issues found"

Troubleshooting — symptom → first check → next check

SymptomFirst checkEstablishesNext check
AI invents an API / flag / method that does not existsearch the library's official docs for the exact symbol, then run the codewhether the symbol is real or a hallucinationreject it, paste the real API reference into the prompt, and ask it to redo the patch against that signature
Output is confidently wrong — plausible but incorrect logicask "explain your reasoning step by step and add a test that proves it"whether the model can justify the change or just asserted itrequire a failing-then-passing test; if the reasoning does not hold, discard and re-scope the prompt
Huge, unreviewable diff touching many fileslook at `git diff --stat` — count files and lines changedthe change exceeded the scope you can actually reviewrevert, re-prompt with "change only FUNCTION in FILE, nothing else", split into small commits
You pasted code that contains secrets into the promptgrep the prompt/history for keys, tokens, .env values, connection stringswhether credentials left your machinetreat the exposed secret as compromised and rotate it; redact with CAPS placeholders before any future paste
The review misses the real bug you are chasingre-run the failing test and capture its exact outputthe reviewer lacked the concrete failure signalpaste the failing test + output and ask it to review the diff specifically against that failure
Reviewer floods you with style nits, not real defectsre-read your prompt — did it ask for "issues" or specifically "correctness, edge-case, security bugs ranked by severity"?the prompt invited low-value feedbacktighten the ask to severity-ranked correctness/security findings and tell it to skip formatting
Tests pass but behavior is still wrong in productionread the test — does it assert the real contract or just that the code ran?the oracle was too weak to catch the defectstrengthen assertions on edge cases (empty, null, boundary, concurrency) and re-run
Everything looks fine and the review found nothingrun the same prompt on a deliberately buggy diff as a controlwhether the reviewer actually detects planted bugs or just rubber-stampsif it misses the planted bug, strengthen the prompt (give context, examples, severity rubric) before trusting a clean verdict

References

Renderer-agnostic data object; hrefs point at official Anthropic and GitHub docs. Verification is docs-checked (content verified against official documentation, not sandbox-executed). Model names kept generic (latest Claude Opus 4.x / Sonnet) per the brief.