Prompt and Context Engineering
Grounding and Source Citation
Exam objective: Constructing a request that grounds answers in the supplied documents, cites their source, and returns 'not found' when the documents do not contain the answer. This includes deciding, based on corpus size, cost, and freshness, whether to supply the full document set in context or retrieve only the relevant passages.
What the exam tests
Whether you can build a request that answers only from supplied documents, shows where each claim came from, and says "not found" when the documents lack the answer. It also tests the architecture choice behind it: put the whole corpus in context, or retrieve only relevant passages, based on corpus size, cost and freshness.
Key rules
Grounding in the prompt
- Tell Claude to use only the provided documents, not general knowledge, and give it explicit permission to say the answer is not there.
- With long inputs (20k+ tokens), put documents at the top and the query after them. Wrap each in
<document>tags with source metadata. - Ask for relevant word-for-word quotes first, then answer from those quotes.
- These reduce hallucinations; they do not eliminate them. Validate high-stakes answers.
Citations
- Set
citations: {"enabled": true}on every document in the request (all or none). - The API returns
cited_textplus a location. It does not count toward output tokens, and the pointers are guaranteed to be valid. - Source type decides granularity and location:
| Source | Citable unit | Location |
|---|---|---|
| Plain text | sentence | character indices (0-indexed) |
| sentence | page numbers (1-indexed) | |
| Custom content | your blocks | block indices (0-indexed) |
search_result | your blocks | source, title, block indices |
titleandcontextreach the model but are never cited.- RAG chunks: use
search_resultblocks (in a tool result or a user message). They requiresource,titleandcontent, and citations are off unless enabled.
{"role": "user", "content": [
{"type": "document",
"source": {"type": "text", "media_type": "text/plain", "data": "..."},
"title": "Leave policy", "citations": {"enabled": true},
"cache_control": {"type": "ephemeral"}},
{"type": "text", "text": "Answer only from the policy. If it does not cover the question, say it was not found. How many days of study leave do I get?"}
]}
Full context or retrieval
- Small and stable corpus: include all of it and cache it. Anthropic's Contextual Retrieval article suggests this below roughly 200,000 tokens.
- Larger than the window, or changing often: retrieve at request time.
- Fitting is not the only limit: accuracy and recall degrade as context grows, and cached tokens still occupy the window.
- Better retrieval: combine embeddings with BM25 (exact terms), prepend chunk-specific context before indexing, then rerank.
- Anthropic has no embedding model of its own; Voyage AI is one provider.
- The Files API avoids re-uploading, but referenced content is still billed as input tokens. Files cannot be modified, and file IDs must never come from untrusted users.
Traps
- Citations plus
output_config.formatreturns a 400. For cited fields, requestfield: valuelines and parse them; check each has a citation. - A "return only JSON" prompt succeeds but may produce few or no citations.
- Scanned PDFs without extractable text can be read but not cited. Images are never cited.
- In a
tool_result, if one block is asearch_result, all must be. - An empty search should return a text block such as "No results found.", not raise or pad with irrelevant hits.
- Asking for quotes in the prompt is not the same as citations: generated quotes can be wrong.
Decide quickly
| Situation | Choice | Why |
|---|---|---|
| Stable manual that fits easily, many queries | Full document + prompt caching | Simple, nothing gets missed, cheap reuse |
| Millions of tokens or hourly updates | Search tool returning search_result blocks | Current, relevant passages with attribution |
| Transcript or bullet list needing exact units | Custom content document | No automatic chunking |
| Exact IDs or error codes missed | Add BM25 to embeddings | Lexical match |
| Chunks ambiguous out of context | Contextual Retrieval | Restores lost context |
| Cited data needed as fields | Line-oriented text, parsed in code | Structured outputs are incompatible |
| Answer absent from documents | Explicit "not found" instruction | Removes pressure to invent |