Skip to content
BH
All posts

The anatomy of a good prompt for an AI coding assistant

"Fix the bug" is not a prompt. A four-part structure, and seven habits, that get better work out of Claude Code and other AI coding assistants.

Last updated

Workshop slide: The Anatomy of a Perfect Prompt, a four-part prompt structure

"Fix the bug" is not a prompt. It is a wish.

The engineers who get the most from an AI coding assistant are not the ones with secret phrases. They write the kind of message they would send to a capable colleague who has just joined the team. This post covers the structure and the habits I teach in my Claude Code Best Practices workshop.

The four-part structure

Order matters. This is the structure Anthropic recommends for long prompts. In its tests, putting the question at the end improved the quality of the answer by up to 30%, most of all with long inputs made of several documents.

  1. Context and documents, at the top. The files, logs, specs or error output the assistant needs.
  2. Examples, in the middle. What good looks like.
  3. Instructions, near the bottom. The constraints and the way you want it done.
  4. Your question, at the very bottom. The actual ask, last.

Most people write it the other way round: the ask first, then a pile of pasted material. Flip it.

Seven habits that go with it

Be specific. Name the file, the function and the behaviour you expect. Use @file references so the assistant reads the right thing instead of guessing.

Describe the problem, not the steps. Delegate, do not dictate. Say what is wrong and what done looks like, and let the assistant propose the route. You will often get a better one than the steps you had in mind.

Explain why. "Keep the response under 200 ms because this runs on every page load" beats "make it fast". With the reason, the assistant can make sensible calls on things you did not mention.

Tell it how to check its work. Add "run the tests after" or name the command to run. An assistant that can verify its own work catches its own mistakes.

Use tags to separate things. Wrap long material in XML-style tags, such as <logs> or <spec>, so the instructions and the documents do not blur together.

Frame it positively, and stop using ALL CAPS. "Write plain functions" works better than "NEVER USE CLASSES". Shouting does not add emphasis. It adds noise.

Give three to five examples, not thirty rules. A few good examples inside <example> tags teach a style faster than a long list of rules.

The colleague test

Anthropic's prompting guide has the best prompt check I know: show your prompt to a colleague with minimal context. If they would be confused, the assistant will be too.

Try it on the last prompt you wrote. If your colleague would need to ask "which service?" or "what does done mean?", the answer to those questions belongs in the prompt.

Prompting is the first of five parts in the workshop. If your team wants the rest, see the AI training I run for engineering teams.