CompCode Academy

Lesson 2 of 6 · Free to read

Four prompt patterns that do most of the work

Four prompt patterns that do most of the work

Audience: Final-year students and engineers in their first three years
Prerequisites: Lesson 1
Time: about 25 minutes
What you will keep: four prompt templates you can reuse
This lesson does not cover: prompt engineering for building AI products (that is a separate module).

Why this matters

Most people prompt the same way every time: they describe the feature and hope. When the answer is wrong, they describe it again, louder. Skilled engineers switch patterns on purpose. Four of them cover nearly every coding situation.

The four patterns

PatternUse it whenWhat you add to the prompt
SpecifyYou know the contractInputs, outputs, errors, and what "done" means
IterateThe output is close but wrongWhat to keep, the one thing to change
ConstrainThe AI is overbuilding or wanderingWhich files, no new dependencies, no refactors
CritiqueYou need a review, not more code"List risks, rank them, do not rewrite yet"

Specify

Weak: "Add a tasks endpoint."

Strong: "Add POST /tasks to app/main.py. Body: title (non-empty string) and due (ISO date). Return 201 with the saved task including an id. Return 422 for an empty title. Add three pytest tests: valid task, empty title, missing due date. Run the tests and show me the output."

The strong version is longer, and it saves you three rounds of correction. A good specify prompt reads like a ticket a teammate could pick up.

Iterate

When an answer is 80% right, do not restate the whole request. Name what stays and what changes:

"Keep the function signature and the tests. Fix only the date comparison: tasks due before today are overdue. Do not rename anything."

Iterating in small steps keeps the parts that already work from drifting.

Constrain

When the AI keeps touching things you did not ask about, fence it in:

"Change only app/main.py and tests/test_tasks.py. Do not add packages. Do not reformat unrelated code. Output a diff."

Constraints are also how you keep an agent safe. A boundary the AI can read is better than a boundary you meant to set.

Critique

Sometimes the best next prompt asks for no code at all:

"Here is my diff. List the five biggest risks, ranked by severity. For each, say what test would catch it. Do not rewrite the code yet."

Critique turns the model from a code generator into a reviewer. It is especially useful just before you open a pull request.

Switching patterns

The skill is noticing which one you need. A typical sequence on a real bug:

  1. Specify: "Here is the failing test and the stack trace. List three likely causes. No fix yet."
  2. You pick the likely cause. Specify again: "Fix cause 2 in list_tasks only, and run the tests."
  3. The AI also "improved" two other functions. Constrain: "Revert everything outside list_tasks."
  4. Tests pass. Critique: "What edge case does this fix still miss?"

Four prompts, each with a clear job, beat ten vague ones.

Exercise: write your templates

Create a file called PROMPT-TEMPLATES.md. Write one template for each pattern, with blanks for the parts that change, for example:

## Specify
Add ___ to ___. Inputs: ___. Output: ___. Errors: ___.
Done means: ___. Run ___ and show me the output.

Use them in the lab in lesson 5 and adjust whatever did not work.

Key points

  • Specify the contract, iterate in small steps, constrain the scope, ask for critique.
  • Switch patterns on purpose when the current one stops working.
  • Ask to see test output; do not accept "done" without it.

New free lessons and course updates. Unsubscribe from any email.