Module 4 — Prompt Engineering and Context Management

Exam weight: 10–15% Study time: 45–60 minutes Lessons: Effective prompts · Zero-shot & few-shot · Principles & flow

Exam tactic. Prompt engineering is hard to teach without practice. The exam usually frames it as recognition: "Which of the following is a good example of few-shot prompting?" — Use Copilot actively while studying.

Effective prompt structure and context

The four parts of an effective prompt

How Copilot collects context

Automatic context: active file, code around the cursor, open files (limited), imports, and adjacent comments — especially the comment directly above the cursor.

Manual context (# references): #file:src/api/UserController.java, #selection, #codebase.

Context optimization

Add context:

Restrict context (privacy):

Anatomy in one example

ROLE + TASK + CONTEXT + FORMAT = effective prompt

Weak:   "Write a test"
Better: "Write JUnit 5 unit tests for UserService.createUser().
         Test successful creation, duplicate email, and missing required field.
         Use Mockito to mock UserRepository."
Udemy Course Available

Want to practice prompt engineering with live Copilot demos?

The GitHub Copilot GH-300 Certification Prep course walks through zero-shot, few-shot, and chain-of-thought prompting in real code scenarios — with a chapter quiz.

Zero-shot, few-shot, and advanced prompting

Zero-shot prompting

Instructions without examples. Use it for simple, well-described tasks.

// Zero-shot: instruction only, no examples
// Generate an email validation method using RFC 5322.
// Return true if valid, otherwise false.

Few-shot prompting

Instructions with examples that guide style and format. Use when style consistency matters.

// Few-shot: examples guide the style
// Examples of our existing validators:
// validateEmail(email)  - RFC 5322, throws ValidationException
// validatePhone(phone)  - international format, throws ValidationException
//
// Now generate in the same style:
// validatePostalCode(postalCode) - Finnish postcode (5 digits)

Chain-of-thought and role prompting

Chain-of-thought: ask Copilot to think step by step.

// Analyze this algorithm step by step:
// 1. Explain what it does
// 2. Identify performance issues
// 3. Suggest improvements

Role prompting: assign a domain role.

// Act as a security auditor. Review this code
// and identify every OWASP Top 10 vulnerability. Be critical.

Technique selection

TechniqueWhen to useTypical use case
Zero-shotSimple, clear tasksFunction generation, explanation
Few-shotStyle or format mattersConsistent code patterns
Chain-of-thoughtComplex problemsAlgorithm analysis, debugging
Role promptingNeed expert framingSecurity review, code review

Principles and process flow

Principles

Prompt processing flow

User input
    ↓
Context assembly (open files, cursor, history)
    ↓
Prompt prioritization (most relevant context first)
    ↓
Tokenization (fills the context window)
    ↓
LLM inference
    ↓
Response

When to clear chat history

In VS Code: open a new chat or run "Clear Chat".

Exam-ready checklist (M04)

Official source documents

Go deeper

Done with M04? Continue with structured video lessons on Udemy.

The GitHub Copilot GH-300 Certification Prep course covers prompt engineering and all five other modules with video walkthroughs and chapter quizzes.