WriteMyAIPromptFree, no sign-up

Prompting AI for code

Published

A coding prompt should state the language and version, supply the surrounding code the model must fit into, name the error cases to handle, and require complete runnable output with no placeholder comments. Add an instruction to say so when unsure an API exists — that is where fabricated method names come from.

What does a coding prompt need that other prompts do not?

Code has a property most output does not: it either runs or it does not. That makes the requirements sharper, and it makes several kinds of vagueness immediately expensive.

  • Environment. Language, version, runtime, framework. "Write a script" without this gets Python 3 code using a library you do not have.
  • Surrounding code. What it has to fit into: existing types, naming conventions, the calling code.
  • Error cases. Which failures matter and how they should be handled.
  • Completeness requirement. An explicit ban on placeholder comments, which models emit freely otherwise.
  • Uncertainty permission. Explicit licence to say "I am not sure this method exists" instead of inventing one.

How do you stop the model inventing API methods?

Fabricated method names are the most common and most annoying failure in AI-generated code. They happen because the model is asked for something specific it does not reliably have, and producing a plausibly-named method is the default behaviour. The name looks exactly right, which is what makes it costly.

Two things help, and both are one line each:

If you are unsure whether an API, method, or library version exists,
say so explicitly rather than guessing at a plausible-looking name.

After the code, list every external API or library method you used
that you are not fully certain about.

The second instruction is the more useful of the two: it produces a checklist to verify rather than requiring you to read every line suspiciously.

How much surrounding code should you include?

Enough to constrain the interface, not the whole repository. In practice that usually means the type definitions the new code must satisfy, the function that will call it, one nearby function that demonstrates house style, and any relevant configuration.

Pasting an entire codebase is context stuffing: it costs more, responds slower, and the important part gets lost among the irrelevant part. The relevant fifty lines beat the irrelevant five thousand.

How do you prompt for a code review?

Review prompts fail differently from generation prompts: rather than producing something wrong, they produce something useless — a list of style opinions when you wanted correctness bugs.

The fix is to name the lens and demand specificity:

Review the diff below for correctness only. Ignore style, naming and
formatting entirely.

For each finding, give:
- the exact line
- a concrete input that triggers the wrong behaviour
- what happens instead of what should happen

If you cannot produce a triggering input, do not report the finding.
Rank findings by severity. If there are none, say so — do not pad.

That last constraint matters more than it looks. Without explicit permission to find nothing, a model asked to review code will always find something, and the something is usually noise.

How do you prompt for debugging?

Supply the evidence, not your theory. A prompt that opens "I think the bug is in the parser" biases the model toward confirming that, which is sycophancy applied to debugging.

  • The exact error message and full stack trace, not a paraphrase
  • The code path involved
  • What you expected and what actually happened
  • What you have already ruled out, and how
  • The environment: versions, OS, relevant configuration

Then ask for candidate causes ranked by likelihood, with a way to test each — rather than for the answer. Debugging is a search, and the model is better at generating hypotheses than at knowing which one is true.

Should you ask for tests at the same time?

Ask for them separately, and preferably first. Generated in the same response as the implementation, tests tend to assert whatever the implementation happens to do — including its bugs.

Writing the tests first, from the specification rather than from the code, gives you something that can actually fail. It is the same reasoning behind test-driven development, and it applies more strongly here because the implementation author has no memory of what the tests were meant to check.

Common questions

Why does AI-generated code use functions that do not exist?
Because it was asked for something specific it does not reliably know, and generating a plausible name is the default. Add an explicit instruction to flag uncertainty, and ask for a list of any API calls the model is not fully certain about.
How do I stop the model writing placeholder comments?
Ban them explicitly: "Return complete runnable code. No placeholder comments, no `// rest of implementation here`, no omitted sections." Models emit them freely otherwise, particularly on longer files.
How much of my codebase should I paste in?
The type definitions the new code must satisfy, the calling code, one nearby function showing house style, and relevant configuration. More than that dilutes attention across code the task does not depend on.
Should I ask for tests and implementation together?
No. Tests written alongside an implementation tend to assert what the implementation does, bugs included. Ask for tests first, derived from the specification, then the implementation that satisfies them.
Does chain-of-thought help for coding prompts?
Yes for debugging and algorithm design, where tracing intermediate state surfaces the fault. Less so for routine implementation where the approach is obvious. With reasoning models the instruction is unnecessary either way.
What is the single most useful line to add to a coding prompt?
"Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle." It converts silent omissions into a documented list you can act on.

Terms used in this guide

  • Chain-of-thought prompting

    Chain-of-thought prompting asks a model to reason before answering. Where it measurably helps, where it actively hurts, and why reasoning models change the advice.

  • Getting structured output from AI

    How to make a model return JSON that parses every time: schema placement, the null rule, prefill, and why "return only JSON" is not enough on its own.

  • The ten most common prompting mistakes

    The mistakes that make AI output inconsistent: uncheckable adjectives, missing context, leading questions, buried instructions, and six more — with the fix for each.

Put this into practice in the prompt builder, or see all guides.