Anatomy of a Coding Prompt
Break a coding prompt into its six working parts, role, task, context, constraints, format, and examples, and learn what each one does.
TL;DR
- A strong coding prompt has up to six parts: role, task, context, constraints, output format, and examples.
- Task and context are mandatory; the rest are dials you add when they improve the result.
- Order the parts from general to specific so the model reads intent before detail.
The Six Parts
Role (optional)A persona that sets perspective and depth when it genuinely changes the answer.
You are a senior TypeScript engineer
reviewing for correctness and types.Task (required)The single, concrete thing you want done, stated as an imperative.
Write a function that paginates an
array into chunks of size n.Context (required)The facts the model cannot infer: stack, versions, existing code, data shape.
TypeScript 5.x, pure function, inputs
are always arrays, n >= 1.The Dials
ConstraintsHard rules the code must obey, as a bulleted list the model and you can check.
- no external dependencies
- do not mutate the input
- throw on n < 1Output FormatExactly what the reply should contain and nothing more.
Return only the function with JSDoc.
No prose, no usage example.ExamplesOne or two input/output pairs when a pattern is easier shown than described.
chunk([1,2,3], 2) -> [[1,2],[3]]
chunk([], 3) -> []Ordering
General To SpecificLead with role and task, then narrow into context, constraints, and format.
Role -> Task -> Context
-> Constraints -> Format -> ExamplesFront-Load IntentState the goal early so later detail is read as refinement, not a new request.
First line = what and why.
Rest = how and the rules.Keep It ScannableHeadings and lists help the model parse the prompt and mirror that structure back.
Structured input ->
structured, predictable output.Common Gaps
Missing VersionWithout a version the model may use outdated or future-sounding APIs.
Add: "Node 20, ESM, TypeScript 5.x".Missing FormatWithout a format you get prose you must trim down to the code.
Add: "Return only the code block."Hidden AssumptionsUnstated rules become the model's assumptions; make them explicit.
Add: "Inputs may be empty.
Handle that case."Tips
- If a prompt feels weak, check which of the six parts is missing rather than rewording the whole thing.
- Put hard requirements in a 'Constraints' list; models follow bulleted rules more reliably than prose.
Warnings
- Omitting the output format is the most common cause of answers you have to reshape by hand.
- A persona alone ('act as a senior engineer') does little without a concrete task and constraints.
In Practice
A fill-in-the-blanks template that assembles the six parts in order. Keep it in a snippet and delete any part you do not need for a given task.
- Role sets perspective only when depth or tone matters; otherwise skip it.
- Task is one imperative sentence naming the concrete deliverable.
- Context lists the stack, versions, and any existing code the model must match.
- Constraints, format, and examples turn a vague request into a verifiable one.
# Role (optional)
You are a {senior backend} engineer.
# Task
{Write / fix / refactor} {what}.
# Context
- Language/version: {TypeScript 5.x, Node 20}
- Framework: {Express 4}
- Existing code/types: {paste or describe}
- Data shape: {describe inputs and outputs}
# Constraints
- {no new dependencies}
- {do not change the public API}
# Output format
{Return only the changed function as a diff.}
# Examples (optional)
{input} -> {expected output}FAQ
No. Task and context are essential; the others are optional dials. Add a persona when tone or depth matters, examples when a pattern is hard to describe, and constraints whenever there are rules the code must obey.
After the task and context, as a short bulleted list. Models follow an explicit 'Constraints:' list more reliably than the same rules buried in a sentence, and a list is easy for you to audit later.
Anything the model cannot infer: the language and version, the relevant existing code or types, the framework, the data shape, error messages, and the surrounding architecture. Give what is relevant, not the whole repo.
It saves rework. Asking for 'just the function, no explanation' or 'a unified diff' means the reply drops straight into your workflow instead of needing cleanup.