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.

Disclaimer: These are personal study notes. Always verify against the official study guide.

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)

ComponentWhat It MeansExample
IntentWhat you want Copilot to do"Write a function that..."
ContextLanguage, framework, environment, purpose"...in Python using FastAPI..."
SpecificityExact inputs, outputs, edge cases"...that takes a list of integers and returns..."
ExamplesSample input/output pairs (few-shot)"e.g., [1,2,3] → 6"
ConstraintsLimitations on the solution"...without using built-in sum()"
💡 The 3S Rule for Prompts

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)

  1. Selected/highlighted code — treated as primary context, highest weight
  2. Current file (surrounding cursor) — code above and below the cursor
  3. Open tabs in the IDE — other files currently open; opening relevant files improves suggestions
  4. Repository structure — file paths, directory layout, inferred framework/language
  5. Instructions files — .github/copilot-instructions.md and path-specific instructions

Context Sources for Chat

  1. Selected code or explicitly referenced files (#file)
  2. Chat history of the current session
  3. Workspace context when @workspace is invoked
  4. Web search results when #web is used
  5. External tool data via MCP connections
⚠ Exam Tip — Open Tabs Are Context

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

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") => 
💡 Zero-Shot vs. Few-Shot — Exam Scenarios

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 PracticeWhy It Works
Write a descriptive comment before starting to codeComment becomes the primary intent signal for inline suggestions
Use meaningful function and variable namesDescriptive names provide implicit context about purpose and types
Add type hints / type annotationsStrongly typed signatures help Copilot infer expected input/output
Keep open tabs of files with similar patternsCopilot uses open tabs as context — similar code guides better suggestions
Break complex tasks into smaller promptsOne task at a time → more focused, higher quality output
Iterate: follow up on initial suggestionsThe 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 explicitlySpecify 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-trackOld history consumes tokens and introduces conflicting context

Engineer Prompts for Performance

Prompt Engineering Principles

Core Principles

PrincipleDescriptionAnti-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

Prompt Files for Reuse

Study Resources