Apply Prompt Engineering & Context Crafting
This skill area accounts for 10–15% of the exam. It covers how to write effective prompts, understand context determination, apply zero-shot and few-shot techniques, and engineer prompts for optimal Copilot performance.
Craft Effective Prompts
Prompt Structure & Context
The quality of Copilot's suggestions depends heavily on the quality of the prompt. Refer to the official prompt engineering guide.
The 5 Components of a Good Prompt (ICSEC)
| Component | What It Means | Example |
|---|---|---|
| Intent | What you want Copilot to do | "Write a function that..." |
| Context | Language, framework, environment, purpose | "...in Python using FastAPI..." |
| Specificity | Exact inputs, outputs, edge cases | "...that takes a list of integers and returns..." |
| Examples | Sample input/output pairs (few-shot) | "e.g., [1,2,3] → 6" |
| Constraints | Limitations on the solution | "...without using built-in sum()" |
Simple (one task at a time) + Specific (precise about what you need) + Short (concise but complete). Break complex requests into smaller, focused prompts — Copilot handles chained context via conversation history.
How Context Is Determined
This is directly tested. Know the sources and their priority.
Context Sources for Inline Suggestions (Priority Order)
- Selected/highlighted code — treated as primary context, highest weight
- Current file (surrounding cursor) — code above and below the cursor
- Open tabs in the IDE — other files currently open; opening relevant files improves suggestions
- Repository structure — file paths, directory layout, inferred framework/language
- Instructions files —
.github/copilot-instructions.mdand path-specific instructions
Context Sources for Chat
- Selected code or explicitly referenced files (
#file) - Chat history of the current session
- Workspace context when
@workspaceis invoked - Web search results when
#webis used - External tool data via MCP connections
Open tabs in the IDE actively influence inline suggestion quality. This is a specific, testable fact. Closing irrelevant files reduces noise; opening files with similar patterns improves suggestion relevance.
Zero-Shot & Few-Shot Prompting
Zero-Shot Prompting
Asking Copilot to complete a task without providing any examples. The model relies entirely on its training knowledge.
// Zero-shot: state the intent clearly
// Write a function that validates an email address using a regex
- Works well for common, widely-understood tasks (sorting, parsing, CRUD operations)
- May produce generic results for domain-specific or unusual requirements
- Good starting point — iterate if the first suggestion doesn't fit
Comments-to-Code (a Zero-Shot Pattern)
Microsoft Learn's official example of "comments to code": describe the function in a comment, then let Copilot complete it on the next line.
# Function to reverse a string
def reverse_string(s):
# Copilot suggests the function body here
# After accepting the suggestion:
def reverse_string(s):
return s[::-1]
This is ideal for short, well-defined functions — drafting code quickly when the task is straightforward.
Few-Shot Prompting
Providing one or more examples (shots) to demonstrate the pattern you want the model to follow.
// Few-shot: show 2 examples, then trigger completion
// formatName("john", "doe") => "Doe, John"
// formatName("jane", "smith") => "Smith, Jane"
// formatName("bob", "jones") =>
- Dramatically improves consistency and accuracy for project-specific patterns
- Especially useful for: custom formatting, naming conventions, API response shapes, test patterns
- The model infers the transformation rule from your examples and continues the pattern
- More examples generally = more consistent output (but uses more context window tokens)
If the exam describes a scenario where Copilot keeps producing inconsistent output for a custom naming convention or output format → few-shot prompting is the answer. If the task is a common algorithm like binary search → zero-shot is sufficient.
Best Practices for Prompt Crafting
| Best Practice | Why It Works |
|---|---|
| Write a descriptive comment before starting to code | Comment becomes the primary intent signal for inline suggestions |
| Use meaningful function and variable names | Descriptive names provide implicit context about purpose and types |
| Add type hints / type annotations | Strongly typed signatures help Copilot infer expected input/output |
| Keep open tabs of files with similar patterns | Copilot uses open tabs as context — similar code guides better suggestions |
| Break complex tasks into smaller prompts | One task at a time → more focused, higher quality output |
| Iterate: follow up on initial suggestions | The chat history is context — refine progressively in the same session |
| Use slash commands for specific actions | /explain, /fix, /test, /doc are faster than free-form prompts for common tasks |
| Include constraints explicitly | Specify frameworks, libraries, performance requirements, or patterns to avoid |
| Provide examples (few-shot) | For custom patterns, examples are far more effective than descriptions alone |
| Start a new chat when conversation is off-track | Old history consumes tokens and introduces conflicting context |
Engineer Prompts for Performance
Prompt Engineering Principles
Core Principles
| Principle | Description | Anti-Pattern |
|---|---|---|
| Single responsibility | One task per prompt | "Generate a REST API, add auth, write tests, and deploy it" in one prompt |
| Explicit context | State language, framework, constraints upfront | "Write a login function" (no language, no framework context) |
| Be specific about output format | State expected return type, variable names, error handling style | Accepting the first suggestion without verifying it matches your conventions |
| Leverage examples (few-shot) | Show input/output pairs for custom patterns | Trying to describe a pattern with words alone when examples would be clearer |
| Iterate conversationally | Build on the previous response in the same chat session | Discarding everything and starting a new prompt after each suboptimal response |
| Leverage code structure | Use comments, docstrings, type stubs as implicit prompts | Asking for code in chat when a well-named function stub would get better inline suggestions |
Prompt Process Flow & Chat History
How Chat History Influences the Model
- Copilot Chat maintains a conversation history within a session
- Each new message is sent to the model along with the prior messages as context
- History enables iterative refinement: "now make it async" → Copilot knows what "it" is
- History consumes context window tokens — a very long session may start losing early context
- If a conversation goes off-track or accumulates irrelevant context, start a new chat session
Prompt Files for Reuse
- Create reusable prompt templates stored in the repository
- Share consistent prompts across the entire team
- Reduces variability and ensures Copilot produces output in your preferred style
- Combine with
.github/copilot-instructions.mdfor maximum consistency - Reference: Custom instructions documentation
Study Resources
- Prompt Engineering for Copilot Chat (GitHub Docs) Official guide — structure, context, few-shot, best practices. Read this before the exam.
- GitHub Copilot Fundamentals Part 2 (Microsoft Learn) Learning path with a full module on prompt engineering and context crafting.
- Custom Instructions for Copilot (GitHub Docs) How instructions files extend prompt engineering at the repository level.
- GH Certified — Practice Questions Community practice tests with prompt engineering scenarios.