Chapter 7: Contextual prompting
The chapter introduces contextual prompting as using information inside a prompt to guide a model’s behavior for a specific request.
After this chapter you will be able to
- Contextual prompting concept. Explain that contextual prompting uses information inside the prompt to guide the model’s behavior for a specific request.
- Request-scoped prompt adaptation. Explain how in-context learning shapes a model’s response using the current prompt without retraining it.
- Instructions versus example patterns. Distinguish zero-shot prompting from few-shot prompting in terms of what the prompt supplies to the model.
- Zero-shot prompting as default. Explain that zero-shot prompting gives the model task instructions without examples and relies on its pretraining. Describe why it is a useful starting point for familiar tasks.
- Domain fit and example decisions. Explain how alignment with the model’s training data affects zero-shot reliability, and describe when to add examples after evaluating the result.
- Few-shot prompting fit. Explain when few-shot prompting is useful and which kinds of tasks tend to benefit most.
- Small example set. Describe how to start with one or two examples and what to do if the task needs broader coverage.
- Example-guided handoff structure. Explain how the reference examples change the handoff’s structure compared with the zero-shot response, without adding facts about the target failure.
- Few-shot example format consistency. Explain why few-shot reference examples should use the same structure, including the shape of values within a shared field. Describe how the code-review example contrasts mixed classification formats with consistent single-line labels.
- Diverse boundary-focused examples. Explain why few-shot examples should show distinct input patterns, include a high-risk boundary case, and resemble the quality of real inputs. Trace how replacing a second easy public endpoint with a restricted admin endpoint changes the example’s classification.
- Advanced few-shot variations. Distinguish contrastive few-shot prompting, few-shot chain of thought, and few-shot retrieval-augmented generation by the kind of example context each adds.
- Zero-shot and few-shot comparison. Describe how to establish a zero-shot baseline for an engineering classification task and upgrade it with one or two few-shot examples while keeping the task inputs the same.
- Representative and boundary examples. Describe how to revise a three-class classification prompt from two narrow examples to a representative example and a boundary example, then check label coverage and ambiguous inputs.
What it builds on
- What you design: System and user prompts (chapter 6) · The distinction between system and user prompts.
- Output format (chapter 2) · The importance of specifying an output format.
Section by section
Introduction
The chapter introduces contextual prompting as using information inside a prompt to guide a model’s behavior for a specific request.
- Contextual prompting concept: Explain that contextual prompting uses information inside the prompt to guide the model’s behavior for a specific request.
Why it matters: This gives you a starting point for understanding the prompting approaches developed later in the chapter.
Key terms: contextual prompting
7.1 In-context learning
This section explains how a model adapts to instructions or examples within a single prompt, and why that is different from fine-tuning.
- Request-scoped prompt adaptation: Explain how in-context learning shapes a model’s response using the current prompt without retraining it.
Why it matters: It explains why changing a prompt can change an answer without changing the model. - Instructions versus example patterns: Distinguish zero-shot prompting from few-shot prompting in terms of what the prompt supplies to the model.
Why it matters: The distinction identifies what changes in the prompt when examples are added. - Prompting before fine-tuning: Justify trying strong few-shot prompts before escalating to fine-tuning.
Why it matters: It gives engineers a practical reason to test prompt designs before taking on a heavier model-change process.
Key terms: in-context learning, next-token prediction, current context window, training distribution, zero-shot prompting, few-shot prompting, input–output examples, local pattern, fine-tuning, prompt-level adaptation
7.2 Zero-shot prompting
This section defines zero-shot prompting, explains why it is a useful default, and shows when inconsistent results warrant adding examples.
- Zero-shot prompting as default: Explain that zero-shot prompting gives the model task instructions without examples and relies on its pretraining. Describe why it is a useful starting point for familiar tasks.
Why it matters: Starting without examples can save tokens while still working well for familiar tasks. - Domain fit and example decisions: Explain how alignment with the model’s training data affects zero-shot reliability, and describe when to add examples after evaluating the result.
Why it matters: Checking reliability before adding examples avoids spending tokens on examples a task does not need.
Key terms: zero-shot prompting, task instructions, pretraining data, token cost, training distribution, proprietary categories, organization-specific conventions, few-shot prompting, domain fit
7.3 Few-shot prompting
This section explains when to add input–output examples to a prompt and how to use a small number of them effectively.
- Few-shot prompting fit: Explain when few-shot prompting is useful and which kinds of tasks tend to benefit most.
Why it matters: Examples are most useful when the prompt needs a more consistent way to handle a specific task. - Classification definitions and examples: Distinguish the role of category definitions from the role of examples in a classification prompt.
Why it matters: Separating label meanings from examples makes each part of a classification prompt serve a clear purpose. - Small example set: Describe how to start with one or two examples and what to do if the task needs broader coverage.
Why it matters: A small, purposeful example set provides guidance without turning the prompt into a long static list.
Key terms: few-shot prompting, input–output examples, zero-shot prompting, output consistency, classification tasks, category definitions, system prompt, few-shot examples, user prompt, one or two examples
7.3.1 Practical example 1: CI pipeline failure handoff generation
A CI failure example compares a useful but prose-shaped zero-shot handoff with a few-shot handoff that follows the team’s four-field triage pattern.
- Example-guided handoff structure: Explain how the reference examples change the handoff’s structure compared with the zero-shot response, without adding facts about the target failure.
Why it matters: Examples can make a handoff easier to use even when the prompt already asks for the right information. - Rollout-first failure triage: Trace how the few-shot handoff prioritizes the failed readiness check when summarizing the rollout and subsequent smoke-test failures.
Why it matters: A concise handoff is more actionable when it points engineers to the failing stage rather than treating every reported failure equally.
Key terms: zero-shot, few-shot, Reference Examples, four-field handoff pattern, Deployment rollout, readiness probe, smoke tests, Next step
7.3.2 Format consistency
This section shows how consistent value formats across few-shot examples make the model’s output more predictable and easier to parse.
- Few-shot example format consistency: Explain why few-shot reference examples should use the same structure, including the shape of values within a shared field. Describe how the code-review example contrasts mixed classification formats with consistent single-line labels.
Why it matters: Consistent examples help an automated system receive a classification it can reliably parse.
Key terms: few-shot prompt, reference examples, Classification: field, value shapes
7.3.3 Example selection
This section explains how to choose and evaluate a small set of few-shot examples so they cover important patterns without becoming narrow or costly.
- Diverse boundary-focused examples: Explain why few-shot examples should show distinct input patterns, include a high-risk boundary case, and resemble the quality of real inputs. Trace how replacing a second easy public endpoint with a restricted admin endpoint changes the example’s classification.
Why it matters: A small example set is most useful when it reflects the cases the model might otherwise mishandle. - Limited label coverage: Describe the tradeoff when two examples must support a three-class task: choose a common case and the label most likely to be missed, then test the omitted labels separately.
Why it matters: Choosing which labels to show also means deciding what needs separate testing. - Example count and ordering: Explain the section’s suggested optimization approach: start with one or two concise examples, try a different pair when evaluation reveals gaps, and swap their order across evaluations.
Why it matters: Evaluating a small set in different combinations can reveal gaps without automatically making the prompt longer.
Key terms: content diversity, edge case coverage, boundary case, realistic input quality, label coverage tradeoffs, common case, omitted labels, example count, example ordering, order bias
7.3.4 Advanced variations
This subsection briefly identifies three advanced ways to extend few-shot prompting with contrasting examples, reasoning steps, or retrieved examples.
- Advanced few-shot variations: Distinguish contrastive few-shot prompting, few-shot chain of thought, and few-shot retrieval-augmented generation by the kind of example context each adds.
Why it matters: These distinctions help you recognize what each variation adds without needing to know how to implement it.
Key terms: contrastive few-shot prompting, few-shot chain of thought, few-shot retrieval-augmented generation
7.3.5 Hands-on practice
This hands-on section asks readers to compare zero-shot and few-shot prompts for engineering tasks, testing how formatting, example selection, and token cost affect the design.
- Zero-shot and few-shot comparison: Describe how to establish a zero-shot baseline for an engineering classification task and upgrade it with one or two few-shot examples while keeping the task inputs the same.
Why it matters: A shared baseline helps you see what the examples change. - Few-shot formatting stress test: Describe a test that compares inconsistent formatting with strict repeated formatting in few-shot prompts for the same classification task, and state what to inspect in the outputs.
Why it matters: Consistent examples can help establish the output structure you want. - Representative and boundary examples: Describe how to revise a three-class classification prompt from two narrow examples to a representative example and a boundary example, then check label coverage and ambiguous inputs.
Why it matters: Two examples may convey a useful pattern while still leaving labels or edge cases untested. - Production token cost comparison: Describe how to compare zero-shot and few-shot designs for a production-like engineering task by estimating input and output tokens and deciding which design is practical.
Why it matters: An example-based prompt must be practical to run, not just useful in a test.
Key terms: zero-shot baseline, few-shot examples, task inputs, format consistency, strict repeated formatting, output structure, representative example, boundary example, label coverage, edge case coverage
7.4 Summary
This summary contrasts prompt-time adaptation with fine-tuning and recaps when and how to use few-shot prompting.
Where this is used later
- (chapter intro) (chapter 9) · Few-shot reference examples that CoT extends.
Concepts explained in this chapter
- few-shot prompt contextFew-shot prompting is a prompting technique that provides a small number of input-output examples to guide a model's response.
- zero-shot promptingZero-shot prompting is a prompting technique that asks a model to perform a task without providing task examples in the prompt.
- format consistencyFormat consistency is the practice of keeping corresponding examples and outputs in a uniform structural shape so that a model receives an unambiguous pattern.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
