how to write better prompts for AI coding assistant

How to Write Better Prompts for an AI Coding Assistant

Introduction

Most bad answers from an AI coding assistant are not a problem with the model. They are a problem with the prompt. A vague request – “fix my code” or “make this better” – leaves the assistant guessing about your project, your goal, and what a good result looks like. So it guesses, and you spend the next half hour undoing the guess. If you want to know how to write better prompts for AI coding assistant work, start with the prompt, not the model.

A good prompt does the opposite. It tells the assistant what you are working on, what you want changed, and how you will check the result. That takes an extra minute to write, and it usually saves far more than that in corrections. This guide shows how to write better prompts for AI coding assistant tasks, with examples you can copy and change for your own project.

1. Start with context, not with the request

Before you ask for code, give the assistant three facts: what the code does, what language and framework it uses, and what is already true about the project. That is the first step in how to write better prompts for AI coding assistant projects: give the facts before the request.

A weak prompt says: “Add login to my app.”

A stronger version says: “This is a small Node.js app using Express. It already has a users table with email and password hash, and no login route yet. Add a POST /login route that checks the email and password, returns a simple token on success, and returns a 401 on failure. Do not change the users table.”

The second version works better because the assistant does not have to invent your stack or your database. It can spend its effort on the actual task. If your project has rules – a style guide, a test setup, a way you name files – mention the one rule that matters for this task. Leave the rest out. OpenAI also publishes more examples in its prompt engineering guide.

2. Ask for one small change at a time

Long prompts that ask for five features at once usually produce code that is half-finished in five places. Break the work down instead. Splitting the work is one of the simplest ways to learn how to write better prompts for AI coding assistant tools — one clear task gets one clear answer.

Ask for the login route first. Read it, run it, and fix what is wrong. Then ask for the next piece, such as a logout route or a protected page. Each step is easier to review, and when something breaks you know exactly which step broke it.

A useful rule: if you cannot describe the change in one sentence, it is probably two changes. Split it before you send the prompt.

3. Show the assistant what “done” looks like

An AI coding assistant does its best work when it knows how you will judge the result. Tell it in plain words. Knowing how to write better prompts for AI coding assistant work means telling it how you will judge the result, not just what to build.

For example: “The function should take a list of prices and return the total with tax added. Tax is 18%. If the list is empty, return 0. Add a small test for an empty list and for a list with two prices.”

Now the assistant has a test to aim at, not just a vague idea of “correct”. You can also paste an example of the input and the output you expect. Two lines of example data often clear up more confusion than a paragraph of explanation.

4. Paste the error, not your summary of it

When something fails, do not write “it doesn’t work”. Paste the actual error message, and the few lines of code around the line it points to.

For example: “I ran npm test and got this error: TypeError: Cannot read properties of undefined (reading ‘name’) at line 42 in user.js. Here is the function around line 42: [paste the code].”

The error message tells the assistant where the problem is. Your summary usually leaves that part out, because you already know what you meant and the assistant does not. The same applies to behaviour: describe what happened, what you expected, and the steps to reproduce it.

5. Tell it what not to do

A short list of limits prevents the most common frustration: the assistant rewriting files you did not ask it to touch. A short limits list is part of how to write better prompts for AI coding assistant projects on a real codebase.

Add one line to your prompt, such as: “Only change user.js. Do not change the database, do not install new packages, and do not rewrite the whole file – change only the login function.”

This matters most on a real project, where a confident rewrite can break working code. If the assistant needs a new package, let it tell you first, so you can decide.

6. Check the code before you trust it

Never paste generated code straight into your project without reading it. Read it the way you would read a colleague’s code: does it handle the empty case, does it use the right variable names, does it do anything you did not ask for?

Run the tests. Try the edge case – an empty list, a wrong password, a missing file. If the code looks right but you do not understand a line, ask the assistant to explain that line in plain words before you keep it. Understanding the code is part of the work, not an extra step.

7. Keep the prompts that work

When a prompt gives you a good result, save it. Keep a short note with the prompt, what it was for, and what you changed after. Next time you face a similar task, start from that note instead of from a blank box. Saving prompts that work is the quiet part of learning how to write better prompts for AI coding assistant work: the library grows one good prompt at a time.

Over a few weeks this becomes a small personal library: one prompt for fixing a bug, one for writing a test, one for explaining an error. That is the same idea behind the workflow skills developers are sharing right now — a clear, repeatable way of working beats a clever one-off prompt every time. For examples of reusable skills you can try, see the trending GitHub AI skills post on Botnex.

Common mistakes to avoid

  • Asking for a whole feature in one prompt, then fixing five half-done pieces.
  • Leaving out the language, framework, or file names and expecting the assistant to guess.
  • Saying “make it better” without saying better in what way – faster, simpler, easier to read.
  • Accepting code you have not run. If it has not run, it is not done.
  • Pasting passwords, API keys, or private data into a prompt. Remove secrets before you share code.

How to write better prompts for AI coding assistant — a quick checklist

  • Give context first: what the code does, the language and framework, and what is already true.
  • Ask for one small change, in one sentence if you can.
  • Say what “done” looks like, with an example or a test.
  • Paste the real error message, not a summary of it.
  • Add limits: which files to change, and what not to touch.
  • Read and run the code before you keep it, and save the prompt if it worked.

Frequently asked questions

How to write better prompts for AI coding assistant?
Start with context, ask for one small change, and say what done looks like. Give the language and framework, the file or function the task is about, and an example of the expected output. Then read and run the result before you keep it.

How long should a prompt be?
Long enough to include the context, the task, and what done looks like. For most small tasks that is three to six sentences. If your prompt is one sentence, it is probably missing context. If it is a full page, it is probably two tasks.

Should I paste my whole project into the prompt?
No. Paste the file or function the task is about, and mention the rest by name. Pasting everything makes the important part harder to find, and it can include private data you did not mean to share.

What if the assistant keeps giving the wrong answer?
Stop and add the missing fact. Usually that fact is the error message, an example of the expected output, or a rule of your project. If three tries still fail, split the task into a smaller one – often the task itself was too big.

Do I still need to learn to code if I use an AI assistant?
Yes. The assistant writes the first draft, but you decide whether it is correct, safe, and worth keeping. That decision needs your understanding, especially before the code goes live.

What to try next

Pick one small task in your own project – a function that needs a test, or a bug with a clear error message. Write the prompt using the steps above for how to write better prompts for AI coding assistant work: context, one task, what done looks like, and what not to touch. Run the result, keep the prompt if it worked, and change one thing about it if it did not. For more detail, see Anthropic’s prompt engineering guidance.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top