Prompting, Context Engineering & Model Adaptation
Engineer prompts, structured outputs, and context budgets; then climb the adaptation ladder — RAG, LoRA/QLoRA, preference tuning, distillation — with honest decision criteria.
Learning objectives
By the end of this chapter, you should be able to:
- Apply the adaptation ladder—prompting → RAG → PEFT → full fine-tune → distillation—with honest criteria: knowledge vs. behavior, freshness, latency, cost, data volume.
- Design system prompts, few-shot selection, and structured-output contracts as versioned engineering artifacts.
- Explain when chain-of-thought helps, when reasoning models make it redundant, and the token-cost implications.
- Engineer context deliberately: budgets, prefix stability, prompt-cache economics on Anthropic/OpenAI/Bedrock/Vertex, and mitigations for lost-in-the-middle and context rot.
- Reason about LoRA/QLoRA mechanics—rank, alpha, quantized bases—and multi-adapter serving.
- Compare RLHF, DPO, and RLAIF, and defend eval-before/after discipline against catastrophic forgetting.
- Map every rung to AWS (Bedrock customization, SageMaker) and GCP (Vertex AI tuning, context caching), including cost-floor gotchas.
1. The adaptation ladder: decide before you tune
The most common senior-level failure here is reaching for fine-tuning to solve a knowledge problem. Fine-tuning reliably changes behavior—format, tone, task framing, tool-selection habits. It is a poor, unauditable way to inject facts, and it cannot keep them fresh: retrieval delivers knowledge with provenance and an update path measured in minutes, while a tuning run's knowledge is frozen and cannot cite a source. Start every adaptation conversation by classifying the gap.
flowchart TD
A["Capability gap identified"] --> B{"Knowledge gap or behavior gap?"}
B -->|"knowledge that changes"| C["RAG and retrieval grounding - see Chapter 04"]
B -->|"behavior, format, tone, task style"| D{"Does prompting plus few-shot pass the eval bar?"}
D -->|"yes"| E["Ship prompt + context engineering"]
D -->|"no, and 100s to 1000s of labeled examples exist"| F["PEFT such as LoRA or QLoRA"]
F --> G{"Quality still short after PEFT sweep?"}
G -->|"yes, with large data and budget"| H["Full fine-tune of a smaller open model"]
B -->|"cost or latency gap at acceptable quality"| I["Distill to a smaller student model"]
H --> I
Prompting + context
Zero training cost, instant iteration, fully reversible. Ceiling: instruction-following limits, long-instruction token cost, drift across model upgrades.
RAG
Fresh, auditable knowledge with citations and access control; does not fix style, refusals, or format. Depth is Chapter 04—here it is the rung to rule out before tuning.
PEFT (LoRA/QLoRA)
Behavior shaping from hundreds of examples; megabyte adapters; multi-tenant serving. Ceiling: limited deep-domain capacity; base upgrades orphan adapters.
Full fine-tune
Maximum plasticity for deep domain shift on smaller open models with tens of thousands of examples. Costs: GPU budget, forgetting risk, a full model copy per variant.
Distillation
A cost/latency rung, not a quality rung: a teacher labels data, a small student learns the narrow task. Use after quality is proven.
Two criteria interviewers listen for. Data reality: prompting needs zero examples, few-shot 3–10, LoRA is credible from a few hundred vetted pairs, full fine-tuning wants an order of magnitude more. With 40 examples, the answer is prompting plus an eval set, not a training job. Reversal cost: a prompt rollback is a config change; a tuned-model rollback is a deployment with provisioned-capacity implications (Section 8). The ladder is an option-value argument—buy the cheap, reversible option first and let the eval harness (Chapter 06) say when to climb.
2. Prompting as engineering, not incantation
A production system prompt is an API contract: role, capabilities, refusal policy, output format, tool-use policy, and precedence when instructions conflict. Treat it like code—version-controlled, diff-reviewed, regression-tested on every change and model upgrade. The instruction hierarchy matters because user input and retrieved documents are untrusted: system policy outranks user requests, and retrieved content is data, never instructions (injection defense continues in Chapters 05 and 11).
- Structure beats prose — labeled blocks for role, policies, tools, examples, output spec; models follow them more reliably and reviewers can diff them.
- Positive instructions — "respond only with JSON matching the schema" beats piles of "do not" clauses; specify desired behavior plus one fallback (how to abstain).
- Few-shot is bias injection — exemplars anchor format, label distribution, and length; ordering effects are real, so shuffle-test during eval.
- Static vs. dynamic exemplars — per-query nearest-neighbor exemplars lift heterogeneous tasks but destroy cache prefix stability (Section 5); measure whether the lift beats the cache loss.
- Upgrade drift — a prompt is an implicit dependency on a checkpoint; pin versions and gate upgrades on eval regressions, not changelogs.
Chain-of-thought, and when reasoning models retire it
Chain-of-thought prompting (Wei et al., 2022) elicits intermediate reasoning that improves multi-step tasks on models not trained to reason by default. In 2026 the landscape is split: reasoning-first models (OpenAI o-series, Claude extended thinking, Gemini thinking) already run an internal reasoning phase; prepending "think step by step" is at best redundant, at worst interference—and reasoning tokens bill as output even when hidden. The senior position: CoT remains a lever for small or non-reasoning models and for auditable rationales; with reasoning models the lever is the thinking-budget parameter, tuned like any latency/cost knob. Never treat emitted rationales as faithful traces of the computation—they are artifacts, not ground truth.
| Situation | Reasoning approach | Why |
|---|---|---|
| Small/distilled model, multi-step task | Explicit CoT or few-shot rationales | Model won't reason unprompted; rationale tokens buy accuracy. |
| Reasoning model, hard task | Set thinking budget; no CoT boilerplate | Reasoning is trained; budget is the control surface. |
| Reasoning model, trivial task at scale | Minimal budget or non-reasoning tier | Reasoning tokens dominate cost with no gain. |
| Regulated flow needing audit trail | Structured rationale field in the schema | You need a stored artifact, not musing. |
3. Structured output and tool-schema design
Most enterprise LLM calls are machine-to-machine. Three enforcement tiers, in increasing strength: (1) formatting instructions plus a validator and retry-with-repair; (2) provider "JSON mode," guaranteeing syntactic JSON but not your schema; (3) constrained decoding against a declared schema—OpenAI structured outputs (docs), Gemini responseSchema on Vertex, strict tool schemas on Anthropic and Bedrock—where the sampler masks tokens that would violate the grammar.
Constrained decoding removes parse failures, not semantic failures: the model can emit a schema-perfect object with wrong values, and over-tight schemas can degrade content quality by forcing commitments before evidence. Keep the schema as loose as the consumer allows, make uncertainty representable (nullable fields, an explicit abstain reason), and keep application-level validation as the final authority.
Tool schemas are prompts with types
Function-calling quality is dominated by schema design, because the model chooses tools by reading names and descriptions. Rules that hold across Anthropic tool use (docs), Bedrock Converse, and Vertex function calling:
- Few, sharp tools — selection error grows with catalog size and overlap; merge near-duplicates and say when to use each tool and when not to.
- Enums over free strings — every free-text parameter is a hallucination surface.
- Flat over nested — deep optional nesting multiplies invalid-combination states.
- Declare side effects — read-only vs. mutating drives retries and confirmation gates (Chapter 05).
- Schemas cost tokens every call — a 20-tool catalog can be thousands of prompt tokens; exactly the stable prefix caching amortizes.
4. Context engineering: budgets, rot, compaction, memory
A 200K–2M token window is capacity, not a strategy. Context engineering decides what earns a place in the window, in what order, and what happens when the conversation outlives it. Two empirical failure modes drive the discipline. Lost-in-the-middle: models retrieve best from the beginning and end of long contexts, with a U-shaped accuracy curve over position (Liu et al., 2023). Context rot: performance degrades as the window fills—well below the advertised limit—because distractors dilute attention; needle-in-a-haystack benchmarks are saturated and don't predict this, so test with realistic multi-fact tasks at your operating lengths.
The budget above (illustrative, not a standard) encodes two rules: stable content first, so the prefix is byte-identical across calls and cacheable; volatile content last, for cache mechanics and because the end of context is a high-attention position for the current question. Trigger compaction at an explicit threshold—say 60–70% of the window (a heuristic)—because quality decays before capacity runs out and you must reserve output headroom.
flowchart TD
H["Conversation history grows"] --> C{"Context usage above budget threshold?"}
C -->|"no"| ASM["Assemble prompt - stable prefix first, volatile tail last"]
C -->|"yes"| SUM["Compact older turns into a structured summary block"]
SUM --> MEM["Persist durable facts and decisions to external memory"]
MEM --> ASM
ASM --> CALL["Model call"]
CALL --> H
Compaction is lossy summarization under a contract: preserve decisions, constraints, open questions, and identifiers; drop pleasantries and superseded drafts; log what was dropped—silent compaction is a debugging nightmare. Across sessions, memory splits into a scratchpad (working notes), episodic memory (past sessions, retrieved like RAG), and semantic memory (distilled stable facts, editable and inspectable). Memory retrieval inherits every relevance and access-control problem from Chapter 04—cross-tenant memory leakage is a security incident, and stale memory is a quality bug only eval traces catch.
5. Prompt caching: mechanics and cache-hit economics
Prompt caching stores the computed KV state (Chapter 02) of a prompt prefix so requests sharing that exact prefix skip most prefill compute. It is the highest-leverage cost/latency optimization in prompt-heavy systems—agents with big tool catalogs, long system prompts, fixed-document Q&A. Provider mechanics differ, and the differences are interview material.
sequenceDiagram
participant C as "Client"
participant R as "API frontend"
participant K as "Prefix cache"
participant G as "GPU prefill"
C->>R: Request 1 with cache breakpoint after tools
R->>K: Look up prefix hash
K-->>R: Miss
R->>G: Full prefill of entire prompt
G-->>K: Store KV state for prefix
G-->>C: Response billed at write rate for prefix
C->>R: Request 2 with identical prefix plus new question
R->>K: Look up prefix hash
K-->>R: Hit within TTL
R->>G: Prefill only the new suffix tokens
G-->>C: Faster response with discounted prefix tokens
| Platform | Control model | Economics (as of checked date — verify pricing pages) |
|---|---|---|
| Anthropic API | Explicit cache_control breakpoints; ~1024-token minimum prefix; TTL refreshes on hit (docs) | Writes ~1.25× base input (5-min TTL; 1-hour tier costs more); reads ~0.1× base input |
| OpenAI API | Automatic for prompts ≥1024 tokens; prefix stability is your only lever (docs) | No write premium; cached input discounted 50–75% by model |
| Amazon Bedrock | Cache checkpoints on Claude and Nova families; works with Converse (docs) | AWS cites up to ~90% cost and ~85% latency reduction on cached tokens |
| Vertex AI (Gemini) | Implicit caching by default plus explicit CachedContent for fixed corpora (docs) | Cached tokens ~75% discount; explicit caches add per-token-hour storage |
Do the break-even aloud in an interview. With Anthropic-style example rates (1.25× write, 0.1× read), N reuses cost 1.25 + 0.1·(N−1) versus N uncached—caching wins from the second use within the TTL, if the prefix repeats byte-for-byte. That conditional is where systems fail:
- Cache busters — a timestamp, request ID, or user name interpolated early invalidates every prefix; put dynamic content after the last breakpoint.
- Dynamic few-shot — per-query exemplars change the prefix every call; pin per segment or accept the loss knowingly.
- TTL vs. traffic cadence — a 5-minute TTL amortizes in a busy agent session and never hits for hourly visitors; match TTL tier to arrival patterns.
- Hit rate is an SLO — providers return cached-token counts; a deploy that reorders prompt sections can silently zero the hit rate and double the bill. Alert on it.
6. LoRA, QLoRA, and adapter serving
LoRA (Hu et al., 2021) freezes base weights and learns a low-rank update: tuned behavior is W + (α/r)·B·A, with A and B thin matrices of rank r. The bet—empirically sound for behavior shaping—is that task adaptation lives in a low-dimensional subspace. Trainable parameters drop to ~0.1–1%; the artifact is megabytes. Rank r (commonly 8–64) sets capacity; α scales the update, and practitioners tune the α/r ratio rather than either alone. More rank helps until the task's intrinsic dimension is covered, then buys overfitting risk; which modules you adapt (attention projections vs. all linear layers) often moves results more than r.
QLoRA (Dettmers et al., 2023) makes the economics accessible: quantize the frozen base to 4-bit NF4, backpropagate into full-precision adapters, use paged optimizers—the paper fine-tuned a 65B model on a single 48GB GPU. The subtlety to name: you trained against a quantized base, so evaluate in the exact serving configuration you'll deploy; train/serve quantization mismatch is a real regression source (quantized serving is Chapter 02).
| Dimension | Full fine-tune | LoRA | QLoRA |
|---|---|---|---|
| Trainable params | 100% | ~0.1–1% | ~0.1–1% (4-bit frozen base) |
| GPU memory | Highest | Moderate | Lowest — single-GPU for mid-size models |
| Artifact | Full copy per variant | MBs per adapter | MBs per adapter |
| Forgetting risk | Highest | Lower (base frozen) | Lower (base frozen) |
| Serving | Dedicated deployment | Multi-adapter over shared base | Multi-adapter over shared base |
| Best for | Deep domain shift, open models | Behavior/format/tone | Same, tight GPU budgets |
flowchart LR
RQ["Requests tagged with adapter id"] --> SCH["Continuous-batch scheduler"]
SCH --> BASE["Shared frozen base weights on GPU"]
subgraph SG1["Adapter pool in GPU and host memory"]
A1["LoRA - support triage"]
A2["LoRA - SQL generation"]
A3["LoRA - claims summaries"]
end
SCH -->|"attach per request"| A1
SCH -->|"attach per request"| A2
SCH -->|"attach per request"| A3
BASE --> OUT["Batched decode across tenants"]
A1 --> OUT
A2 --> OUT
A3 --> OUT
Multi-LoRA serving usually decides the PEFT-vs-full-FT debate on multi-tenant platforms: systems in the S-LoRA line (Sheng et al., 2023) and engines like vLLM batch requests for different adapters through one shared base, paging adapters between host and GPU memory. Fifty tenant variants become fifty small files on one fleet, not fifty deployments—at the cost of small per-token overhead, cold-swap latency for rare adapters, and a registry mapping tenant → adapter version → base version. That mapping is the sleeper issue: adapters couple to the exact base checkpoint, so a base upgrade is a coordinated retrain-and-reeval event across every adapter.
7. Preference tuning, distillation, and the forgetting problem
SFT teaches what a good answer looks like when a gold answer exists. Preference tuning teaches choosing between plausible answers—helpfulness, tone, safety—where quality is comparative. RLHF as productionized by InstructGPT (Ouyang et al., 2022) trains a reward model on preference pairs, then optimizes the policy with PPO under a KL penalty to the reference. It works and is operationally heavy: a second model to train, RL instability, and reward hacking—the policy exploiting reward-model blind spots such as verbosity and sycophancy.
flowchart LR
S["SFT checkpoint"] --> G["Sample candidate responses"]
G --> L["Human or AI preference labels"]
L --> PP["Preference pairs - chosen vs rejected"]
PP -->|"RLHF path"| RM["Train reward model"]
RM --> PO["PPO updates with KL penalty to reference"]
PP -->|"DPO path"| DL["Direct preference loss - no reward model, no RL loop"]
PO --> AM["Aligned model"]
DL --> AM
AM --> EV["Win-rate evals plus general-capability regression suite"]
DPO (Rafailov et al., 2023) collapses the pipeline: a closed-form objective optimizes directly on preference pairs—no reward model, no RL loop, far simpler to reproduce. The trade: no reusable reward model for filtering or online scoring, and quality bounded by the static pair set rather than on-policy sampling. RLAIF—AI feedback replacing human labels, as in Constitutional AI (Bai et al., 2022)—scales label volume while inheriting labeler bias. Interview depth is which knob solves which problem: SFT for task format, preference methods for comparative qualities, DPO as the pragmatic first choice because its failures are visible in data rather than RL dynamics.
Distillation: buying back cost and latency
Distillation transfers a narrow capability from a large teacher to a small student—training on teacher outputs and rationales (Hsieh et al., 2023), or on logits where weights allow. It sits last on the ladder because it presumes you know what good looks like: the teacher sets the ceiling, and the student inherits teacher errors invisibly unless evals cover the tails. Managed offerings (Bedrock Model Distillation; teacher generation plus Vertex tuning) compress the workflow, but the eval obligation stays yours—and distilling proprietary outputs into a competitor model is a licensing question.
Catastrophic forgetting and eval discipline
Every gradient that makes the model better at your task makes it different at everything else. Forgetting shows up as degraded instruction following, lost multilingual ability, or safety drift after a narrow tune—invisible if you only measure the target task. The non-negotiable discipline:
- Baseline firstFreeze a task eval and a general regression suite (instruction following, safety refusals, benchmark slice); score the base model.
- Train with mitigationsPrefer PEFT; mix general instruction data into the tuning set; early-stop on validation.
- Score both suites afterAccept only if task lift clears the bar AND regressions stay within budget; record both in the model card.
- Canary in servingSmall traffic slice with task metrics and safety monitors (Chapter 11) before ramp.
Synthetic data feeds every rung and needs its own gates: generate with a strong teacher from seeds and real failures; filter with programmatic verifiers then a calibrated LLM judge (Chapter 06); deduplicate; decontaminate against eval sets. A small vetted-human set beats a large unfiltered synthetic dump; the ratio you can defend with an ablation is the senior answer.
8. Cloud mapping: tuning and caching on AWS and GCP
Platform and forward-deployed interviews expect the ladder landed on real services—and the cost-model fine print that changes the recommendation. Full stack tours are Chapters 08 and 09; this is the adaptation-specific mapping.
AWS
- Bedrock custom modelsmanaged fine-tuning and continued pre-training
- Bedrock Model Distillationmanaged teacher→student distillation
- Bedrock Custom Model Importserve weights tuned elsewhere
- SageMaker training jobs / HyperPodfull-control LoRA/QLoRA/full FT on open weights
- Bedrock prompt cachingcache checkpoints on Claude and Nova families
Google Cloud
- Vertex AI supervised tuningmanaged LoRA-based tuning for Gemini
- Vertex AI custom trainingGPU/TPU jobs for open-weight PEFT and full FT
- Model Gardenopen-weight models with tuning and deployment recipes
- Vertex context cachingimplicit caching plus explicit CachedContent API
- Gemini responseSchemastructured-output enforcement at the API level
The portable logic: managed tuning when you want provider models and minimal MLOps; roll-your-own on SageMaker/Vertex custom training for open weights, custom losses (DPO is often DIY territory), multi-LoRA economics, or portability. Either way, eval-before/after is yours—no managed service owns your regression suite.
Interview playbook
For any "should we fine-tune?" question, walk the LADDER:
- L — Locate the gap: knowledge vs. behavior vs. cost/latency; tuning is for behavior, retrieval for knowledge.
- A — Attempt the cheapest rung: prompt + context engineering against a frozen eval with a stated pass bar.
- D — Data audit: vetted example count, labeling ownership, licensing, synthetic-augmentation defensibility.
- D — Decide with exit criteria: "LoRA if the baseline stalls below X with ≥500 examples; full FT only for deep domain shift on open weights."
- E — Evaluate before and after: task eval plus general regression to catch forgetting; canary rollout.
- R — Runtime and cost model: adapter vs. full-copy serving, provisioned floors, cache hit-rate impact, base-upgrade coupling.
Senior signals
- Quantified caching arguments: write premium vs. read discount, TTL vs. cadence, hit rate as a monitored SLO.
- Prompts and schemas as versioned, regression-tested artifacts pinned to model versions.
- Reasoning models move CoT from prompt text to a thinking-budget parameter—with the output-cost implication.
- Naming adapter/base version coupling and provisioned-capacity floors before recommending tuning.
Common traps
- Fine-tuning to "teach the model our docs"—a knowledge/freshness problem retrieval solves with provenance.
- Claiming constrained decoding guarantees correct output—it guarantees shape, not semantics.
- A timestamp at the top of the system prompt, and a mysteriously doubled cache bill.
- Reporting only target-task metrics after tuning, with no regression suite.
- "DPO is better than RLHF" as an absolute rather than a complexity/control trade-off.
Question bank
Questions actually asked for prompting, context engineering, and model adaptation at senior level.
Q1A product team wants to fine-tune a model on the company wiki so it "knows our products." Respond.
Strong answer outline
- Classify the gap: knowledge that changes—tuning bakes stale facts with no citations or access control.
- Propose RAG for knowledge with provenance and freshness; reserve tuning for behavior gaps.
- Offer the ladder with exit criteria and the eval set that would justify climbing.
Follow-up probes
- When would tuning on the wiki ever be right?
- How would you prove RAG is sufficient?
Pass if knowledge-vs-behavior is the spine and an eval bar is named; fail on a generic "RAG is cheaper."
Q2Design the prompt-caching strategy for an agent with a 20-tool catalog and a long system prompt.
Strong answer outline
- Order by stability: system prompt → tool schemas → static exemplars → memory → volatile history/query; breakpoints after stable blocks.
- Eliminate cache busters (timestamps, request IDs) from the prefix; pin exemplars or justify the trade.
- Match TTL to session cadence; monitor cached-token counts as an SLO; estimate savings with write/read arithmetic.
Follow-up probes
- What changes on OpenAI's automatic caching vs. Anthropic's explicit breakpoints?
- A deploy halves your hit rate—first thing you inspect?
Pass if ordering, busters, TTL, and monitoring all appear; fail if the answer is "turn on caching."
Q3Do the math: when does prompt caching pay for itself?
Strong answer outline
- Example rates: write ~1.25× base input, read ~0.1× (Anthropic-style; OpenAI has no write premium).
- N uses cost 1.25 + 0.1(N−1) cached vs. N uncached → any real reuse within TTL wins.
- The dominant variable is hit rate—prefix churn and TTL misses destroy the economics; caching cuts TTFT, not decode.
Follow-up probes
- How does a per-token-hour storage fee (Vertex explicit caches) change the model?
- What hit rate would you demand before relying on caching in capacity planning?
Pass if arithmetic is performed and hit rate identified as dominant; fail on vague "caching saves money."
Q4What are lost-in-the-middle and context rot, and how do you design around them?
Strong answer outline
- Lost-in-the-middle: U-shaped accuracy over position (Liu et al.); context rot: quality decays as the window fills, below the advertised limit.
- Mitigations: retrieve/select rather than stuff, critical evidence and the question near the end, compact at a utilization threshold.
- Test with realistic multi-fact tasks at operating lengths—needle-in-a-haystack is saturated.
Follow-up probes
- Does a bigger window remove the need for RAG?
- How would you detect rot in production traces?
Pass if both phenomena are distinguished with positional and budgetary mitigations; fail on "use a bigger model."
Q5Explain LoRA and QLoRA: what do rank and alpha control, and what risk does QLoRA add?
Strong answer outline
- Freeze W, learn ΔW = (α/r)·B·A; ~0.1–1% trainable params; MB artifacts. r sets capacity (8–64 typical); tune the α/r ratio; target-module choice often matters more than r.
- QLoRA: 4-bit NF4 frozen base, full-precision adapters, paged optimizers—single-GPU fine-tuning for mid-size models.
- Risk: train/serve quantization mismatch—evaluate in the exact deployment configuration.
Follow-up probes
- Why might doubling r not improve results?
- You'll serve the base in 8-bit—does the QLoRA adapter transfer cleanly?
Pass if low-rank intuition, serving consequences, and the mismatch risk all appear; fail if it's only "efficient fine-tuning."
Q6Design serving for 50 tenant-specific model variants.
Strong answer outline
- Multi-LoRA over one shared frozen base (S-LoRA/vLLM pattern): 50 adapters as small files, one fleet, batching across adapters.
- Operational needs: adapter registry (tenant→adapter→base version), cold-swap latency budget, per-tenant eval gates.
- Name the coupling: a base upgrade forces coordinated retrain/reeval of all adapters; compare cost with 50 deployments.
Follow-up probes
- What breaks if two tenants need different base models?
- How do you canary one tenant's new adapter?
Pass if shared-base economics and version coupling both appear; fail if the answer is 50 endpoints.
Q7RLHF vs. DPO: differences, and which would you run first?
Strong answer outline
- RLHF: reward model + PPO with KL leash—powerful, reusable reward model, operationally heavy, reward-hacking risk.
- DPO: closed-form loss on pairs—no RM, no RL loop, reproducible; bounded by static pair quality, no reusable scorer.
- Default DPO first for applied teams; mention RLAIF for scaling labels with inherited bias.
Follow-up probes
- What is reward hacking and how do you detect it?
- When is the reusable reward model worth RLHF's complexity?
Pass if the trade-off is complexity/control, not "DPO is newer"; fail on absolutes.
Q8How do you detect and mitigate catastrophic forgetting after a fine-tune?
Strong answer outline
- Detection: frozen general regression suite (instruction following, safety refusals, benchmark slice) scored before and after, next to the task eval.
- Mitigation: prefer PEFT, mix general instruction data, early-stop on validation.
- Governance: accept within an agreed regression budget; canary with safety monitors.
Follow-up probes
- Task accuracy up 6 points, refusal rate down 8—ship it?
- Why does LoRA reduce but not eliminate forgetting?
Pass if the before/after dual-suite discipline is explicit; fail if forgetting is defined but not operationalized.
Q9Compare JSON mode, constrained decoding, and tool-calling for structured extraction.
Strong answer outline
- JSON mode: syntactic JSON only. Constrained decoding (structured outputs, responseSchema): grammar-level schema enforcement. Tool-calling: schema enforcement plus multi-action framing.
- Constrained decoding removes parse failures, not semantic errors; over-tight schemas can hurt content quality.
- Application validator as final authority, one repair retry, dead-letter path.
Follow-up probes
- How do you represent "not found" without inviting hallucinated values?
- What breaks with an unbounded free-string field?
Pass if shape-vs-semantics is crisp; fail if constrained decoding is called a correctness guarantee.
Q10Is chain-of-thought prompting still relevant with reasoning models?
Strong answer outline
- Yes for small/non-reasoning models and audit-trail rationales; largely no as boilerplate for reasoning models, where the control is the thinking budget.
- Reasoning tokens bill as output—budget them per task tier like a latency/cost knob.
- Emitted rationales aren't faithful traces; use as artifacts, verify with evals.
Follow-up probes
- How would you set thinking budgets across a mixed-difficulty workload?
- When would you route to a non-reasoning tier entirely?
Pass if the answer is model-conditional with a cost dimension; fail on a blanket yes or no.
Q11Compare the tuning paths and cost gotchas on Bedrock vs. Vertex AI.
Strong answer outline
- AWS: Bedrock customization (fine-tune/distill) with the provisioned-throughput serving floor historically attached to custom models—do utilization math; SageMaker for open-weight control.
- GCP: Vertex supervised tuning is adapter-based; tuned Gemini serves at base per-token pricing per current docs—different break-even.
- Both: eval-before/after is customer-owned; verify current docs since serving options evolve.
Follow-up probes
- At 100K requests/day vs. 1K/day, does the recommendation change?
- When does Custom Model Import beat tuning inside Bedrock?
Pass if the provisioned-floor vs. per-token difference is named; fail on a feature-list comparison.
Q12Design a synthetic data pipeline for fine-tuning, with quality controls.
Strong answer outline
- Seed from real failures and vetted human examples; generate variations with a strong teacher under coverage targets.
- Filter: programmatic verifiers first (schema, execution), calibrated LLM judge second; deduplicate; decontaminate against eval sets.
- Validate with an ablation (synthetic vs. mixed vs. human-only); check teacher-output licensing.
Follow-up probes
- How do you prevent the student learning the teacher's systematic errors?
- What human-to-synthetic ratio would you defend, and how?
Pass if filtering, dedup, and decontamination all appear; fail if volume is the only lever.
Proof artifact: an adaptation-ladder bake-off
Build a public, reproducible comparison of three rungs on one task—structured extraction from messy support tickets (public or synthetic data). All results are portfolio measurements, not claims about Purnendu's production history.
Steps
- Freeze the task: extraction JSON schema, 300–500 labeled examples split train/validation/eval, plus a general regression suite (instruction following + safety refusals).
- Variant A — engineered prompt: versioned system prompt, static few-shot, constrained decoding, validator with one repair retry; iterate to plateau, logging every version's score.
- Variant B — dynamic few-shot: nearest-neighbor exemplars; measure the accuracy delta AND the cache hit-rate/cost delta vs. A.
- Variant C — QLoRA tune of an 8B-class open model on the train split via a SageMaker training job or Vertex custom job; serve on a vLLM container with LoRA support.
- Benchmark identically: per-field accuracy, schema-violation rate, p50/p95 latency, cost per 1,000 requests including cache effects, regression deltas for C.
- Write a one-page memo recommending a rung at 1K, 50K, and 1M requests/day with break-even arithmetic shown.
Metrics
Per-field precision/recall, exact match, schema-violation and repair-retry rates, cached vs. uncached token counts and TTFT, cost per 1,000 requests per variant, tuning-cost amortization curve, before/after regression scores for the tuned model.
Deliberate failures
- Insert a timestamp at the top of the system prompt; show the cache hit-rate collapse and cost delta.
- Over-tighten the schema and measure content-quality degradation vs. the loose schema.
- Overtrain the QLoRA run (no early stopping) and show the regression suite catching instruction-following decay while task accuracy still looks fine.
- Evaluate the adapter against a differently quantized serving base and document the silent quality drop.
What to present
The ladder diagram with measured numbers per rung, the cost-vs-volume break-even chart, the cache-buster incident graph, and the forgetting demonstration. The arc—"prompting won at low volume, tuning won at 1M/day, here is the crossover"—is exactly the judgment senior interviews probe.
Chapter review
Adaptation is an economics-and-evidence problem. Classify the gap, buy the cheapest reversible option first, and climb only when a frozen eval says the rung below failed. Engineer prompts and schemas as versioned artifacts; order context for stability and cache economics; treat LoRA as the default tuning tool; and never accept a tuned model without before/after scores on both the task and a regression suite.
Glossary
- Adaptation ladder
- Escalation—prompting, RAG, PEFT, full fine-tune, distillation—governed by gap type, data volume, and reversal cost.
- Context rot
- Task-quality degradation as the window fills, well below the advertised token limit.
- Lost in the middle
- U-shaped retrieval accuracy over position in long contexts; beginnings and ends are privileged.
- Prompt caching
- Reuse of a prefix's computed KV state across requests, discounting cached tokens and cutting TTFT.
- LoRA
- Frozen base weights plus a trained low-rank update (α/r)·B·A; megabyte-scale artifacts.
- QLoRA
- LoRA over a 4-bit-quantized frozen base with paged optimizers; single-GPU fine-tuning of mid-size models.
- Multi-LoRA serving
- Batching requests for many adapters through one shared base model on the same GPUs.
- DPO
- Direct preference optimization: closed-form loss on preference pairs, no reward model or RL loop.
- Catastrophic forgetting
- Degradation of general capabilities from narrow fine-tuning; detected only by regression suites.
- Constrained decoding
- Sampling restricted by a schema/grammar—shape guarantee, not semantic correctness.
Mastery checklist
- I can classify a requirement as knowledge, behavior, or cost/latency and pick the rung with exit criteria.
- I can design system prompts and tool schemas as versioned, regression-tested artifacts.
- I can say when CoT helps, when a thinking budget replaces it, and the billing implication.
- I can order a context window for cache stability and positional attention, with a compaction threshold.
- I can do prompt-cache break-even arithmetic and name the top three cache busters.
- I can explain rank, alpha, and target modules in LoRA and the QLoRA train/serve quantization risk.
- I can design multi-LoRA serving for many tenants and name the base-version coupling.
- I can compare RLHF, DPO, and RLAIF as complexity/control trade-offs.
- I can run eval-before/after and defend a regression budget.
- I can map every rung to Bedrock/SageMaker and Vertex AI, including provisioned-throughput vs. per-token serving.
Primary sources
Links checked . Pricing and model support change frequently; verify current pricing pages before quoting numbers.
- Anthropic — prompt caching: breakpoints, TTLs, and pricing multipliers
- Anthropic — tool use and schema design
- OpenAI — automatic prompt caching
- OpenAI — structured outputs and strict schemas
- Amazon Bedrock — prompt caching
- Amazon Bedrock — model customization
- Amazon Bedrock — model distillation
- Amazon SageMaker — training jobs
- Vertex AI — context caching overview
- Vertex AI — Gemini model tuning overview
- Wei et al. — Chain-of-Thought Prompting Elicits Reasoning in LLMs
- Liu et al. — Lost in the Middle: How Language Models Use Long Contexts
- Hu et al. — LoRA: Low-Rank Adaptation of Large Language Models
- Dettmers et al. — QLoRA: Efficient Finetuning of Quantized LLMs
- Sheng et al. — S-LoRA: Serving Thousands of Concurrent LoRA Adapters
- Ouyang et al. — Training language models to follow instructions (InstructGPT/RLHF)
- Rafailov et al. — Direct Preference Optimization
- Bai et al. — Constitutional AI: Harmlessness from AI Feedback
- Hsieh et al. — Distilling Step-by-Step