Skip to content

Domain 4: Apply Prompt Engineering and Context Crafting (10โ€“15%) โ€‹

โ† Domain 3 ยท Next Domain โ†’

Exam Tip

Prompt engineering is about giving Copilot the right information to generate the right output. The exam tests your ability to recognize a well-structured prompt and know when to apply zero-shot vs. few-shot techniques.


Prompt Structure and Context โ€‹

A high-quality Copilot prompt has four elements:

ElementPurposeExample
GoalWhat you want Copilot to do"Write a function that..."
ContextWhy or in what situation"...for a REST API that handles user auth"
SourceWhat data/files to reference"...using the User class defined in models.py"
Format/ExpectationWhat the output should look like"...return a typed dict, include error handling"

Exam Tip

A comment above a function is one of the most powerful prompts in Copilot. Clear, descriptive comments produce better inline suggestions than vague ones.


How Context is Determined โ€‹

Copilot automatically uses:

  • Current file: Highest priority โ€” the code around the cursor
  • Open tabs: Other files currently open in the editor contribute context
  • Language/framework: Copilot adjusts suggestions based on detected language and imports
  • Comments: Inline and block comments are interpreted as instructions

To improve context:

  • Keep relevant files open in editor tabs
  • Write descriptive function/variable names
  • Add comments describing intent, not just what the code does

Zero-Shot Prompting โ€‹

Zero-shot prompting: asking the model to perform a task without any examples.

python
# Zero-shot: just describe what you want
# Parse a JSON string and return the 'email' field, or None if missing
def get_email(json_str: str) -> str | None:
    # Copilot completes here

When to use: Simple, well-defined tasks where the intent is clear from context alone.


Few-Shot Prompting โ€‹

Few-shot prompting: providing examples before the task to guide the model.

python
# Few-shot: show the pattern, then ask for more
# Input: "hello world" โ†’ Output: "Hello World"
def title_case(s: str) -> str:
    return s.title()

# Input: "remove spaces" โ†’ Output: "removespaces"
def remove_spaces(s: str) -> str:
    return s.replace(" ", "")

# Now generate: Input: "reverse me" โ†’ Output: "em esrever"
def reverse_string(s: str) -> str:
    # Copilot completes here, following the established pattern

When to use: When the pattern or style is non-obvious or when you want Copilot to follow a specific convention.


Best Practices for Prompt Crafting โ€‹

  • Be specific: "Write a function that validates an email address using regex" > "Write email validation"
  • Specify the output: Include expected return types, error handling style, and format
  • Use personas: Start with "As a senior Python developer..." to influence code quality and style
  • Reference files: Mention specific files or classes when relevant (see the models.py User class)
  • Iterate: If the first suggestion is wrong, refine your prompt and try again
  • Prompt files: Store reusable instructions in .github/copilot-instructions.md to establish workspace-wide conventions

Prompt Engineering Principles โ€‹

PrincipleMeaning
ClarityUnambiguous language produces better suggestions
SpecificityDetailed prompts outperform vague ones
ExamplesShowing the pattern (few-shot) reduces ambiguity
IterationTreat Copilot as a conversation โ€” refine based on output
Context managementKeep relevant files open; close irrelevant ones to reduce noise

Chat History Usage โ€‹

In Copilot Chat, previous messages in the session are included in the context window:

  • Copilot uses chat history to maintain conversational continuity
  • If you change topics, consider starting a new chat to avoid old context contaminating new suggestions
  • Long conversations may hit context limits โ€” earlier messages may get truncated

Domain 4 Quick Quiz

1 / 4
โ“

What is the difference between zero-shot and few-shot prompting?

(Click to reveal)
๐Ÿ’ก
Zero-shot: describe the task without examples. Few-shot: provide 1-3 examples of the pattern before asking Copilot to continue it.

โ† Domain 3 ยท Next Domain โ†’

Happy Studying! ๐Ÿš€ โ€ข Privacy-friendly analytics โ€” no cookies, no personal data
Privacy Policy โ€ข AI Disclaimer โ€ข Report an issue