Getting Useful Output From GitHub Copilot Starts With Better Prompts

Generative AI coding tools are changing how developers approach everyday tasks, from documenting codebases to writing unit tests. But there's a learning curve, and even experienced developers can feel frustrated when an assistant like GitHub Copilot doesn't produce the expected output. A common example: asking Copilot to draw an ice cream cone in p5.js returned irrelevant suggestions—or nothing at all. The problem wasn't the model; it was the way the request was phrased.

Understanding how Copilot processes information is the first step to communicating with it effectively. What follows are practical, field-tested guidelines for prompt crafting with GitHub Copilot, based on experience building browser extensions, games, and other projects with the tool.

What Is a Prompt, Anyway?

The term "prompt" means different things depending on who you ask. For machine learning researchers building and fine-tuning these tools, a prompt has a specific technical definition. For developers using generative AI in an IDE, it's something more practical: the instructions and context you provide to get the code you want.

From a developer's perspective, prompt engineering is the practice of crafting those instructions so that the AI understands your goal and produces useful, accurate code.

Prompts Prompt engineering Context
Developer Code blocks, individual lines of code, or natural language comments a developer writes to generate a specific suggestion from GitHub Copilot Providing instructions or comments in the IDE to generate specific coding suggestions Details that are provided by a developer to specify the desired output from a generative AI coding tool
ML researcher Compilation of IDE code and relevant context (IDE comments, code in open files, etc.) that is continuously generated by algorithms and sent to the model of a generative AI coding tool Creating algorithms that will generate prompts (compilations of IDE code and context) for a large language model Details (like data from your open files and code you’ve written before and after the cursor) that algorithms send to a large language model (LLM) as additional information about the code

Three Best Practices for Prompt Crafting

1. Set the Stage With a High-Level Goal

If you're working in a blank file or an empty codebase, Copilot has zero context about what you're building. Starting with a big-picture description primes the assistant before you dive into specifics. Think of it as a conversation with a pair programmer: state the problem you're solving together before asking for implementation details.

For example, when building a markdown editor in Next.js, a comment like this sets the stage:

/*
Create a basic markdown editor in Next.js with the following features:
- Use react hooks
- Create state for markdown with default text "type markdown here"
- A text area where users can write markdown
- Show a live preview of the markdown text as I type
- Support for basic markdown syntax like headers, bold, italics
- Use React markdown npm package
- The markdown text and resulting HTML should be saved in the component's state and updated in real time
*/

Copilot then produces a simple, functional (if unstyled) markdown editor in under 30 seconds, leaving you time to focus on styling. Note that results may still be non-deterministic: even with a detailed prompt, Copilot may generate slightly different default text than you asked for.

2. Make the Ask Simple and Specific

Once you've stated the overall goal, articulate the logic and steps to achieve it. Copilot understands instructions better when they're broken down into discrete pieces. It's like writing a recipe—you list each step rather than describing the finished dish in a paragraph.

Let Copilot generate code after each step rather than asking it to produce a large block all at once. This iterative approach keeps outputs manageable and easier to verify.

We prompted GitHub Copilot to reverse a sentence by writing six prompts one at a time. This allowed GitHub Copilot to generate a suggestion for one prompt before moving onto the text. It also gave us the chance to tweak the suggested code before moving onto the next step. The six prompts we used were: First, let's make the first letter of the sentence lower case if it's not an 'I.' Next, let's split the sentence into an array of words. Then, let's take out the punctuation marks from the sentence. Now, let's remove the punctuation marks from the sentence. Let's reverse the sentence and join it back together. Finally, let's make the first letter of the sentence capital and add the punctuation marks.

3. Give Copilot an Example or Two

Copilot learns from examples, just like a human colleague. Without one, it can make incorrect assumptions. In one case, asking it to extract names from an array of data produced an incorrect use of map(). With a concrete example of the expected transformation, it returned the correct result immediately. This technique relates to zero-shot, one-shot, and few-shot learning approaches.

Three More Tips for Better Results

1. Experiment With Your Prompts

Prompt crafting is more art than science, so don't expect perfection on the first try. If the output misses the mark, iterate. Start with a vague prompt—it won't provide enough context. Add specificity, but keep in mind that knowing which details matter most takes practice. You may need several rounds to clearly define inputs, outputs, and boundaries. Sometimes refining the function intent in the comment itself is what finally gets Copilot on track.

2. Keep Relevant Tabs Open

Copilot uses a technique called "neighboring tabs" to contextualize your code: it processes other files open in your IDE, not just the one you're working on. Keep one or two related files open to help the assistant understand your codebase. It's not guaranteed that Copilot will use every open file as context, but having relevant references available increases the odds.

3. Follow Good Coding Practices

This one might sound obvious, but it matters: use descriptive variable and function names, maintain consistent coding styles, and follow the patterns established in your codebase. In one test, a descriptive function name using snake_case matching the project's conventions led Copilot to generate a spot-on suggestion. When a poorly named function introduced inconsistent style, Copilot instead generated a useless comment: "Code goes here."

Always Review Generated Code

The LLMs behind coding assistants find patterns in training data and extrapolate from them. Given their scale, they may generate sequences that don't exist in their training data at all. So treat generated code like you'd treat a colleague's contribution: assess it, analyze it, and validate it before accepting it.

A Practice Exercise

Ready to put these tips to work? A guided example walks through prompting GitHub Copilot to build a browser extension. You'll need Copilot installed and enabled in your IDE to follow along.