AI-Assisted Coding · Module 1Lesson 4

Study explanation · Lesson 04 · 14 min

Prompt patterns and the AI interaction loop

Three reusable patterns, role, few-shot and constraint-based prompting, cover most practical work, and you combine them when the stakes rise. Around every prompt runs one loop: ask, inspect, run, test, refine.

Listen to this lesson

Hear the lesson read aloud, with each sentence highlighted as you go.

Narrated by an AI voice.

Craft, not magic wording

The notes are plain about it: prompt engineering is not magic wording. It is a learnable craft built from a few practical patterns used consistently. The module teaches three, and in real work you often combine them.

Key ideaPrompt engineering is a learnable craft built from a few practical patterns used consistently.

Pattern 1: role prompting

A role assigns a specific professional lens. Avoid vague roles such as “expert.” Use roles that tell the assistant what to pay attention to: here, error handling, input validation and status codes.

Prompt from the notesYou are a senior FastAPI developer reviewing a REST API for production readiness. Review the following endpoint and identify issues related to error handling, input validation, and HTTP status codes. [paste endpoint code]

Pattern 2: few-shot prompting

Few-shot prompting gives the assistant one or more examples to imitate. The notes stress that the example sets the quality ceiling, not just the format: weak acceptance criteria in your example produce weak criteria in every story. Best uses are user stories, test cases, ADRs, API docs and commit messages.

Prompt from the notesGenerate user stories for the Task Tracker in this format. Example: Story: As a team member, I want to create a task so that I can track work. Acceptance Criteria: - Title is required; missing title returns 422. - Description is optional. - Created task appears in the list. Now generate 5 more stories in the same format.

Key ideaThe example sets the quality ceiling, not just the format.

Pattern 3: constraint-based prompting

Constraint-based prompting lists what the output must and must not include. It is the most direct way to prevent scope creep, unexpected dependencies and the wrong output shape.

Prompt from the notesWrite a function to validate a task object before saving it. Constraints: - Use only Python standard library and Pydantic. - Do not add logging. - Return: { valid: bool, errors: list[str] }. - Title must be non-empty after stripping whitespace. - Status must be ToDo, InProgress, or Done. - Priority must be Low, Medium, or High. Return only the function. No explanation.

When to use each pattern

Pick by what the task needs. When the output is complex or high-stakes, use all three together.

When to use each pattern · Lecture notes p. 10
PatternUse when...Example course use
Role promptingA specific professional perspective is needed.Review an API endpoint for validation, errors, and status codes.
Few-shot promptingThe desired structure or quality is easier to show than describe.Generate user stories matching a strong example.
Constraint-based promptingYou need to prevent extras, scope creep, dependencies, or wrong return shapes.Scaffold only the skeleton; do not add auth, database, or full CRUD.
All three togetherThe output is complex or high-stakes.Architecture analysis, ADR drafts, security reports, structured test cases.

The AI interaction loop

This is the module’s core workflow. Don’t submit and accept. Each step has a concrete meaning, and “it looked right” is not a test.

The loop: refine feeds the next ask · Lecture notes p. 10
  1. AskStructured prompt
  2. InspectRead before using
  3. RunExecute or apply
  4. TestVerify behavior
  5. RefineSmallest correction back

Do not submit and accept.

The AI interaction loop · Lecture notes p. 10
StepWhat it meansWhat it looks like in practice
AskWrite a clear structured prompt.Include role/context, task, constraints, and output format.
InspectRead the output before using it.Check imports, function names, assumptions, error cases, and return shape.
RunExecute the code or apply the artifact.Start the server, paste the function into the project, or review the requirement for buildability.
TestVerify behavior explicitly.Use test cases, curl, Swagger, or acceptance criteria. “It looked right” is not a test.
RefineFeed the smallest correction back.Use the exact error, failing case, or missing requirement as the next prompt.

The loop on a validation function

The loop also applies to non-code artifacts. For requirements, “run” means checking whether a story can actually be built and tested. For architecture, it means weighing trade-offs before accepting a proposal. Here is the loop on code, as the notes walk it.

Loop example: Task Tracker validation · Lecture notes p. 11
StepExample
AskWrite a validation function for title, status, and priority. Return { valid, errors }.
InspectDoes it trim whitespace? Does it use the exact allowed values? Does the return shape match?
RunPaste it into the project and check imports and execution.
TestTry empty title, whitespace-only title, invalid status, and a valid object.
RefineIf whitespace-only title is accepted, ask for a trim check and exact error message.

Exercise: apply all three patterns

Generate user stories for a Task Tracker that supports priorities and assignees. Write three separate prompts: one role prompt, one few-shot prompt and one constraint-based prompt. Submit them to your AI tool and compare the outputs for structure, acceptance criteria and edge cases. The library’s P3, P4 and P5 are worked versions of each pattern.

Key takeaways

  • Role prompting sets a lens; few-shot sets a quality ceiling; constraints set boundaries.
  • Combine all three when the output is complex or high-stakes.
  • Ask, inspect, run, test, refine, and never submit and accept.
  • Refine with the exact error or failing case, not a general complaint.

Built from the Module 1 lecture notes, prompt library and additional readings; the videos they accompany were not supplied.

Your study checkpoint

Ready to continue?

Mark the lesson complete once you have read it and tried its idea on something real.

Next · Lesson 5