Prompt Engineering Cheatsheet: Patterns for Coding and Research Agents
Prompt patterns that reliably work on AI agents: the anatomy of a strong prompt, coding-agent patterns (scope, tests, diffs), research patterns (sources, structure, verification), and copy-ready templates.
1. Anatomy of a Prompt That Works
Vague prompts get vague output. A reliable prompt names the role, supplies the context, states one task, sets constraints, and defines the exact output format β in that order.
ROLE: You are a senior Python engineer on this codebase.
CONTEXT: FastAPI service. The failing test is pasted below.
Stack: Python 3.12, pytest, SQLAlchemy 2.
TASK: Find the root cause of the failure and fix it.
RULES: - Change only the file that owns the bug.
- Do not rename public functions.
- If you are unsure, say so instead of guessing.
OUTPUT: 1) Root cause in two sentences.
2) The patch as a diff.
3) The exact command to re-run the test.
2. Patterns for Coding Agents
Coding agents fail mostly from missing scope, not missing intelligence. These patterns pin down what may change, what "done" means, and how the agent must prove it.
| Pattern | What to write | Why it works |
|---|---|---|
Name the files | "Work in src/auth/ only" | Stops drive-by edits across the repo |
Acceptance test first | "Done when: pytest tests/test_auth.py passes" | Gives the agent a verifiable finish line |
Plan before code | "Propose a plan, wait for my OK, then implement" | Cheap to correct a plan, expensive to correct code |
Paste errors verbatim | Full traceback, not a summary | The model debugs evidence, not your memory of it |
Constrain the diff | "Smallest change that fixes it; no refactors" | Keeps reviews fast and regressions rare |
One task per prompt | Split "build the feature" into steps | Failures stay small and attributable |
Demand proof | "Run the tests and show the output" | Ends the session with evidence, not claims |
3. Patterns for Research Agents
Research agents invent facts when the prompt allows it. Force grounding: sources for claims, clear delimiters around source material, and a verification pass before the answer.
| Pattern | What to write | Why it works |
|---|---|---|
Cite or stay silent | "Every factual claim needs a source; if none, write UNKNOWN" | Fabrications become visible gaps |
Delimit the material | Wrap pasted text in triple quotes or XML tags | Separates your instructions from the data |
Structure the output | "Return a table: claim | evidence | source" | Comparable, scannable, auditable |
Verify before answering | "List your claims, then check each against the sources" | A self-review pass catches contradictions |
Few-shot example | Show one ideal answer before the real question | Format and depth transfer better than adjectives |
Define the audience | "Explain for a junior developer, skip basics of X" | Calibrates depth instead of guessing |
4. Copy-Ready Templates
Three templates worth saving. Replace the bracketed parts; keep the structure β the structure is the point.
# BUG FIX
Fix this bug. Error output:
"""
[paste the full traceback]
"""
Relevant file: [path]. Done when [test/command] passes.
Explain the root cause before changing code.
# CODE REVIEW
Review this diff like a senior engineer. Report only:
1) bugs, 2) security risks, 3) missing tests.
Quote the exact line for each finding. No style nitpicks.
"""
[paste the diff]
"""
# RESEARCH
Question: [your question]
Use only the sources below. Answer with a table:
claim | supporting quote | source name.
If the sources do not answer it, say UNKNOWN.
"""
[paste sources]
"""
π Associated Learning Path & Monetization Blueprints
Prompt Engineering and Structured Outputs with Pydantic
Take these patterns further: structured outputs, schemas and validated agent responses.
Read Full Guide βMulti-Agent Coding Squad
Package agent prompting skills into a multi-agent coding service for clients.
Read Blueprint βNeed another cheat sheet?
We add new reference guides every week based on community requests.
Request a Cheatsheet β