GitHub Copilot study materials
GitHub Copilot study resources

GitHub Copilot: Apply Prompt Engineering and Context Crafting

GitHub Copilot · Work through your exam, one topic at a time.

All guide sections

Apply Prompt Engineering and Context Crafting

Domain 4 - Apply Prompt Engineering and Context Crafting

This domain tests whether you can turn vague intent into a prompt that yields usable code, and whether you can control the context Copilot sees.

Prompt rules that move the needle

  • Specify the contract: language, framework version, inputs, outputs, error behavior, and what must not change.
  • Show an example: a sample input/output or an existing pattern (few-shot) beats adjectives like “clean” or “robust”.
  • Decompose: ask for a plan, then implementation, then tests. One task per prompt produces tighter diffs.
  • Iterate: correct with follow-ups (“keep the signature, use the repository Result type”) instead of restarting.
  • Name the role and audience: “review this as a security reviewer looking for injection risks” shifts what the model emphasizes.
  • Request verification artifacts: edge cases, error paths, tests, and a list of assumptions.

Context crafting

  • Open the files that matter and close the ones that do not; open tabs are implicit context.
  • Reference explicitly with #file, #selection, #editor, #terminalLastCommand, or #codebase for repo-wide questions.
  • Put standing conventions in .github/copilot-instructions.md (banned libraries, error-handling style, test framework) instead of repeating them in each prompt.
  • Use comment-driven development: write the signature, docstring, and an example call, then let completion fill the body.
  • Inline chat for local edits; chat view for exploration and multi-file reasoning.

Worked example

Weak: “write a function to parse config.” Stronger: “In #file:config/loader.py, add load_settings(path: Path) -> Settings using pydantic v2, raising ConfigError with the file name on validation failure. Follow the error pattern in #file:config/errors.py. Return only the function plus a pytest test covering a missing key.” Reasoning: the second prompt fixes the contract, supplies a style exemplar, and asks for a check - three levers that cut rework.

Traps

  • Assuming longer is better; unfocused context dilutes instruction-following.
  • Forgetting that irrelevant open tabs leak into suggestions.
  • Expecting Copilot to remember a prior chat session or internal APIs it was never shown.
  • Treating output as deterministic - the same prompt can yield different code.