← Back to modules

Prompt Engineering & Context Design — Advanced

Context compression, DSPy optimization, long-context vs RAG, needle-in-a-haystack, ReAct and tree-of-thoughts, constrained decoding, sampling, indirect injection, jailbreaks, and prompt caching.

Hard30 questions

Sample questions

1

When conversation history exceeds the budget, summarization-based compression keeps continuity but has what inherent cost?

  • It loses exact phrasing, tone, and nuance, and adds latency from a separate summarization call
  • It permanently raises the model's context window, so later turns silently exceed the provider's token limit
  • It doubles the output token cost, because every summary must be regenerated on each subsequent turn
  • It corrupts the tokenizer state, so tokens after the summary are encoded with a different vocabulary

Why

Summarizing old turns replaces verbose history with a compact recap, which frees budget while preserving the gist. The costs are real. A summary keeps decisions, facts, and state, but it drops exact phrasing, tone, and fine conversational nuance. It also requires a separate model call, which adds latency mid-conversation. The raises-the-window option is wrong because summarization does not change the architectural limit; it fits more meaning under the same limit. The doubles-output-cost option is wrong because you can cache or reuse a running summary rather than regenerate everything each turn. The corrupts-the-tokenizer option is fabricated; summarization is ordinary text and does not alter tokenization. To limit the downside, a fast, cheap model is often used for the summary, and only the oldest turns are compressed while recent turns stay verbatim. The lost detail is why summarization can hurt tasks that depend on precise earlier wording. It is a lossy but often necessary compression.

2

To fit content far larger than the window, recursive (hierarchical) summarization does what, and what is its main risk?

  • Increases the context window in fixed steps until the entire document fits without any loss
  • Summarizes batches, then summarizes the summaries, with cumulative information loss at each stage
  • Splits the document across several models running in parallel, each returning the full text unchanged
  • Encodes the document as a single embedding vector that the model decodes back losslessly on demand

Why

Recursive summarization processes a very large body of text in batches, summarizes each batch, and then summarizes those summaries, repeating until the distilled content fits the window. Each stage can compress by several times, so a huge input can be reduced to a small digest. The main risk is that information loss compounds across stages, so details present in the raw text may not survive to the final digest. The increases-the-window option is wrong because the window is fixed; recursion works around it rather than expanding it. The parallel-models-return-full-text option is wrong because the point is compression, not returning the text unchanged. The single-embedding option is fabricated; you cannot losslessly decode arbitrary long text from one embedding. This technique is powerful for getting a global overview of a large corpus. But because loss is cumulative, it is poorly suited to tasks that need exact facts buried deep in the source. Pairing it with retrieval for precise lookups mitigates that weakness.

3

DSPy treats prompts as programs with optimizable parameters. What does this actually change about how you build a prompt?

  • You hand-write the final prompt string, and DSPy only measures its accuracy without altering anything
  • You fine-tune the model's weights directly, since DSPy replaces prompting with gradient-based training
  • You lock the temperature to zero, which DSPy claims is sufficient to remove the need for any prompt tuning
  • You define a task signature and examples, and an optimizer searches for instructions and few-shot demonstrations

Why

DSPy reframes prompt construction as programming with optimizable pieces. You declare a task signature that specifies input and output types and supply training examples, then an optimizer searches over instruction phrasings and few-shot demonstrations to maximize a metric. This replaces hand-tuning with a systematic search grounded in real model behavior. The hand-write-only option is wrong because DSPy actively generates and selects prompt components, not just measures them. The fine-tune-weights option is wrong; DSPy optimizes the prompt program, not the model parameters, so it works even with closed models. The lock-temperature option is fabricated; temperature is not the mechanism, and it does not remove the need for tuning. One common optimizer bootstraps few-shot examples by running the task on training data and keeping the cases the model gets right as demonstrations. Reported gains over hand-written prompts can be several to many points on classification and extraction. The value comes from testing phrasings and examples against actual outputs rather than human intuition.

4

A team debates stuffing an entire document set into a long context versus retrieving the top chunks (RAG). What does the evidence suggest?

  • Long context always wins, so retrieval is obsolete once a model supports a large enough window
  • Long context tends to win on cross-document reasoning, while retrieval tends to win on single-fact lookup
  • Retrieval always wins, because a smaller prompt is cheaper and cheaper always means more accurate
  • The two are interchangeable, since accuracy is identical whenever the total token counts happen to match

Why

Comparisons of full-document long context against top-k retrieval show a nuanced picture rather than a single winner. Long context tends to do better on tasks that require reasoning across many documents, because the model can see everything at once. Retrieval tends to do better on precise single-fact lookups, because it concentrates attention on the most relevant text and avoids diluting it. The long-context-always-wins option ignores retrieval's edge on focused lookups and its lower cost. The retrieval-always-wins option wrongly equates cheaper with more accurate, which does not hold on cross-document tasks. The interchangeable option is wrong because matching token counts does not equalize accuracy; how the tokens are selected and placed matters. A hybrid that retrieves, re-ranks, then stuffs the top results into a long context often beats either pure approach. The right choice depends on whether the task is lookup-heavy or reasoning-heavy. This is a genuine architecture decision, not a solved default.

5

Your model retrieves a fact perfectly at 8K tokens but misses it at 100K tokens when it sits at 50% depth. Which test characterizes this, and what does it reveal?

  • A tokenizer fuzz test, revealing that long inputs overflow the vocabulary and corrupt the buried fact
  • A throughput benchmark, revealing that the GPU cannot process 100K tokens within the request timeout window
  • A jailbreak probe, revealing that the buried fact was filtered out by a safety layer at long context
  • The needle-in-a-haystack test, revealing lost-in-the-middle degradation that worsens at long context and mid-depth

Why

The needle-in-a-haystack test inserts a specific fact at a controlled depth within a large body of unrelated text and asks the model to retrieve it, sweeping both total length and needle position. The resulting heatmap shows where recall stays strong and where it fails. The described pattern, perfect at short context but failing at long context and middle depth, is the classic lost-in-the-middle effect scaled up, where mid-depth retrieval degrades as total length grows. The tokenizer-fuzz option is wrong; long inputs do not overflow the vocabulary, and the failure is about attention over position, not encoding. The throughput option is wrong because this is a retrieval-accuracy failure, not a timeout. The jailbreak option is fabricated; no safety layer is filtering an ordinary fact. The practical response is to place critical information near the start or end, keep contexts shorter when possible, and re-rank retrieved material. It is why simply having a huge window does not guarantee reliable recall everywhere in it.

Free account

Take the full module

These are the first few of 30 questions. A free account opens the rest as a scored drill.

  • Every question in this module
  • Instant feedback and supporting reading
  • Your score and progress, saved

Free · your email is used for progress only.