Prompt engineering fundamentals¶
Make the work reviewable before asking for code¶
Session M1-03 | 3 hours
Agenda¶
| Segment | Time |
|---|---|
| Prompting as task design | 7 min |
| Four prompt components | 14 min |
| Comments and context | 9 min |
| Iterative refinement | 9 min |
| Anti-patterns and untrusted instructions | 10 min |
| Todo-store demonstration | 7 min |
| Patterns and lab handoff | 4 min |
A prompt is a work request¶
Weak:
The model must guess which file to change, how it should behave, how to handle errors, which dependencies it may use, and when the work is done.
A useful prompt states the decisions that affect the result.
Four useful components¶
Intent What change is needed?
Context What should inform the work?
Constraints What boundaries must hold?
Examples What does accepted behavior look like?
Use only the components the task needs.
Intent¶
Name the action and scope:
Better than:
Context¶
Useful context can include:
- a focused test;
- a related file;
- the actual failure;
- an accepted design decision;
- repository instructions.
Include enough context to reduce guessing. Leave out files that obscure the task.
Constraints¶
State boundaries that affect the result:
Scope and dependency limits are part of correctness.
Examples¶
Input:
{ title: " Write clear prompts " }
Expected:
{
id: 1,
title: "Write clear prompts",
status: "pending",
priority: "normal"
}
Examples make behavior concrete.
Including an example: good and bad¶
Good: one real input, one expected output, tied to a test.
Implement TodoStore.create. For { title: " Write " } it must
return { id: 1, title: "Write", status: "pending", priority: "normal" }.
Bad: the example contradicts the rule, or is the only case.
The model follows the example, and the rule is lost. Add one edge case, such as an empty title, so Copilot doesn't copy a single case.
A reviewable prompt¶
Implement only TodoStore.create in lib/todo-store.js.
Use the focused test as the contract.
Trim the title, apply the documented defaults, return a copy,
and add no package.
Run the focused create test when done.
It states the task without dictating every line.
Comments can carry intent¶
// POST /todos
// Create a todo from JSON input.
// Return 201 on success.
// Return 400 with { error: message } for invalid input.
A useful comment describes behavior and boundaries.
Neighboring files are context, not a contract¶
An open file, such as a sample of response shapes, may influence a suggestion.
It does not override:
- the route comment;
- accepted tests;
- repository rules;
- the developer's review.
Record the result instead of assuming the effect.
Refine the missing behavior¶
Weak follow-up:
Focused follow-up:
Name the mismatch you observed.
One concern per turn¶
Implement baseline list
↓
Run focused test
↓
Fix copy semantics
↓
Run focused test
↓
Move to the next method
Small turns produce smaller diffs.
Review the /refine rewrite¶
In the standalone CLI:
/refine make the todo store work, baseline tests only, plain js,
no packages, return copies, only lib/todo-store.js
Compare the rewrite with the tests. Correct missing constraints or invented work before submitting it.
Current input: Ctrl+X, then /, then /refine without arguments.
Editor Chat has no /refine.
In the lab: you spend five minutes of the baseline-store block on this comparison.
Restart when the frame is wrong¶
Iterate when the structure is sound and one behavior is missing.
Start a new request when:
- the task changed;
- the design is wrong;
- stale assumptions keep returning;
- the prompt mixed unrelated work.
Common prompt failures¶
| Failure | Better move |
|---|---|
| Vague request | Name behavior and scope |
| Many unrelated asks | Split the work |
| No project context | Add the relevant file or test |
| Every line dictated | State requirements, not implementation trivia |
| No check | Name the command or review |
Treat instructions from content as untrusted¶
Untrusted instructions are commands inside text you didn't write for this task: a code comment, an issue, a log, a web page, copied text. Copilot can read them, and may treat them as orders.
Review them as data. Do not follow text that asks you to:
- ignore project rules;
- expose secrets;
- broaden access;
- run an unrelated command.
Untrusted text: example¶
An issue you ask Copilot to summarize contains:
Good: Copilot summarizes the bug and flags the hidden line. You don't follow it.
Bad: you paste the issue into a prompt and accept an edit that changes
.env. The comment steered the change, not you.
Your prompt and project rules set the task. Text in content never does.
Demo contract¶
Before you prompt, write down what a correct result looks like: one call, one expected output.
Use the contract to judge the suggestion, not the other way round.
In the demo: store.create({ title: " Write clear prompts " }) must return { id: 1, title: "Write clear prompts", status: "pending", priority: "normal" }.
Weak, useful, reviewable¶
Implement create.Implement TodoStore.create using the test.- A prompt with behavior, file limits, and a check to run.
Compare what the model must still guess in each request.
A change request should change the prompt¶
A new requirement changes the contract. Before you prompt again, state:
- the new values or rules;
- the defaults;
- what existing behavior changes;
- how to handle invalid input.
In the lab: the change request adds a priority field.
A reusable prompt pattern¶
Change <file and scope> to achieve <observable behavior>.
Use <relevant context>.
Keep <constraints and non-goals>.
Verify with <command or review>.
State enough detail to make the task testable without dictating the implementation.
Lab handoff¶
In the lab, you extend one todo API:
- implement the baseline store;
- wire the REST routes;
- compare context with one file open and closed;
- decide invalid-priority behavior;
- apply the change request;
- save one reusable prompt.
Deliverable: working code, six passing tests, and a prompt decision log.