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

  1. A strong coding prompt has up to six parts: role, task, context, constraints, output format, and examples.
  2. Task and context are mandatory; the rest are dials you add when they improve the result.
  3. 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

    Constraints

    Hard 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 < 1
    Output Format

    Exactly what the reply should contain and nothing more.

    Return only the function with JSDoc.
    No prose, no usage example.
    Examples

    One 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 Specific

    Lead with role and task, then narrow into context, constraints, and format.

    Role -> Task -> Context
    -> Constraints -> Format -> Examples
    Front-Load Intent

    State 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 Scannable

    Headings and lists help the model parse the prompt and mirror that structure back.

    Structured input ->
    structured, predictable output.

Common Gaps

    Missing Version

    Without a version the model may use outdated or future-sounding APIs.

    Add: "Node 20, ESM, TypeScript 5.x".
    Missing Format

    Without a format you get prose you must trim down to the code.

    Add: "Return only the code block."
    Hidden Assumptions

    Unstated rules become the model's assumptions; make them explicit.

    Add: "Inputs may be empty.
     Handle that case."

Tips

  1. If a prompt feels weak, check which of the six parts is missing rather than rewording the whole thing.
  2. Put hard requirements in a 'Constraints' list; models follow bulleted rules more reliably than prose.

Warnings

  1. Omitting the output format is the most common cause of answers you have to reshape by hand.
  2. A persona alone ('act as a senior engineer') does little without a concrete task and constraints.

In Practice

FAQ