If Content is King, Then Context is God
How GitHub Copilot Uses Context
One of the key factors that make GitHub Copilot effective is its ability to understand the context of the code you are working on. It uses your code, obviously, but also the file type, the programming language, the libraries you are using, the file structure, and even the cursor position to generate relevant completions and answers. Let's break down some of these context factors:
Cursor-focused context: Copilot prioritizes the code immediately before and after your cursor to capture the local logic and inform suggestions.
Open files in your editor: If related files are open in the IDE, Copilot includes them in its analysis.
Chat history as context: In Copilot Chat, prior messages and code references feed into the model’s understanding. This allows the user to build on previous exchanges naturally and enables Copilot to maintain continuity in the conversation.
Context variables: Copilot supports specific context variables that allow you to explicitly define what context to consider when making a request. We will explore this in more detail later.
Tools outputs: When Copilot calls a tool like a terminal or a web search, it includes the output of that tool in its context and sends it to the model along with your request and any other relevant context from your environment.
Workspace context: Copilot can access the entire workspace in the Agent chat experience. Even if files are not open in the editor, Copilot can still read them, use them as context, and reference them in its responses.
The combination of different context sources helps Copilot deliver relevant results given the specific situation you're in. For example, when you ask Copilot to generate a function, "create a function that fetches user data from an online crypto API", it may follow these steps:
- Read any system prompt to understand your coding style, preferences, and more.
- Read the current open file(s).
- Analyze the code around the cursor.
- Check for any relevant code in other open files.
- Look for relevant code in the entire workspace.
- Scan the available tools to see if any can help.
- Make a search on the web if needed to find the right API.
- Use history from the chat to find the most relevant interactions.
Building with GitHub Copilot
From Autocomplete to Autonomous AgentsEnroll now to unlock all content and receive all future updates for free.
