Engineering Craft › AI-Assisted Development
Prompting for Code
Giving an AI enough context and constraints to produce useful code.
Also known as: prompt engineering for code, writing prompts for coding assistants
The quality of what an AI coding assistant produces depends a lot on what you give it. Prompting for code means supplying enough context and constraints that the answer is useful, instead of generic.
A weak prompt:
write a function to process orders
A better one:
In
orders/service.py(Python 3.12, SQLAlchemy 2), addcancel_order(order_id, user). It should: raiseNotFoundif the order doesn’t exist, raiseForbiddenif it belongs to another user, refuse to cancel orders already shipped, and set status tocancelledinside a transaction. Follow the style ofrefund_orderin the same file. Add pytest tests next to the existing ones.
What to include
- The goal and the context: what it’s for, what already exists, where the change goes. Paste or point to the relevant code (context windows).
- Constraints: language and versions, libraries to use or avoid, style, performance, security rules.
- Behavior and edge cases: inputs, outputs, errors.
- Examples: sample input and expected output, or an existing function to imitate.
- The format you want: just the diff, a plan first, tests included.
Habits that work
- Work in small steps. Ask for one change, check it, then continue. Big requests produce big mistakes.
- Ask for a plan first on anything non-trivial, and correct it before code is written.
- Show errors fully: paste the whole message and stack trace, and what you expected.
- Say what’s wrong when it misses, specifically, instead of repeating the request.
- Keep project rules in a file the assistant reads each session (agent instructions file).
- Start fresh when a long conversation drifts.
- Verify the result: run it, test it, read it (reviewing AI code).
Cautions
- Don’t paste secrets or private data into tools you aren’t cleared to use.
- Detail doesn’t guarantee correctness. It can still hallucinate.
- If you can’t describe the problem clearly, work on understanding it first. The prompt is a mirror of your thinking.