Module 09 · Lesson 02
Context Engineering
Reading time: 25 minutes Track: Advanced Claude Setup · Universal Prerequisites: Module 09 · Lesson 01
What context engineering actually is
Prompt engineering is about what you say. Context engineering is about how you arrange what you say.
The same information, structured differently, produces measurably different results. Claude's attention and retrieval are not uniform across a context window. The order, structure, and labeling of content affects how reliably Claude finds and uses it.
This lesson covers the structural primitives that make Claude prompts perform consistently at scale.
01 · XML tags: the structural primitive
Claude was trained on content that uses XML-style tags to denote document structure. This means XML tags aren't decorative — they're semantic signals that Claude uses to parse the intent and structure of a prompt.
The core set of tags:
<instructions>
What Claude should do
</instructions>
<context>
Background information Claude needs
</context>
<examples>
<example>
<input>...</input>
<output>...</output>
</example>
</examples>
<input>
The actual thing to process
</input>
<documents>
<document index="1">
<source>filename.pdf</source>
<document_content>
Full document text here
</document_content>
</document>
</documents>
Rules for effective XML tagging:
-
Use consistent, descriptive names.
<protocol_text>is better than<doc>. Future you and your teammates will thank you. -
Nest when content has natural hierarchy. Multiple documents go inside
<documents>, each wrapped in<document index="n">. Multiple examples go inside<examples>, each in<example>. -
Don't use XML tags inside code blocks. Code blocks already have delimiters. XML inside a code block will confuse both humans and Claude.
-
Tag separates content from instructions. Without tags, Claude sometimes treats long pasted documents as instruction text. Tags prevent this.
Biotech application: For protocol review tasks, use
<protocol_text>,<query>, and<output_format>. For safety narrative drafts, use<source_documents>,<ae_data>, and<narrative_requirements>. Name tags after what they contain, not generic labels.
02 · Document placement: queries go last
This is counterintuitive but empirically validated: long documents belong at the TOP of a prompt, and your question or instructions belong at the BOTTOM.
Anthropic's internal testing shows that queries placed at the end of long-context prompts improve response quality by up to 30% compared to queries placed at the beginning. The mechanism: Claude processes the document fully before encountering the question, which produces better grounding in the source material.
Less effective:
Identify all inclusion criteria violations in this protocol.
<protocol_text>
[50,000-word protocol document]
</protocol_text>
More effective:
<protocol_text>
[50,000-word protocol document]
</protocol_text>
Identify all inclusion criteria violations in this protocol. For each violation, quote the relevant protocol section verbatim, then explain why it constitutes a violation.
For multiple documents:
<documents>
<document index="1">
<source>protocol_v3.2.pdf</source>
<document_content>
[Protocol text]
</document_content>
</document>
<document index="2">
<source>amendment_2.pdf</source>
<document_content>
[Amendment text]
</document_content>
</document>
</documents>
Compare the original protocol and the amendment. List every change to eligibility criteria, primary endpoints, and visit schedule. For each change, note which document section it appears in.
03 · Quote-extraction grounding
For long-document tasks in biotech — where you need accurate, citable outputs — add an explicit quote-extraction step before asking for analysis.
<source_documents>
<document index="1">
<source>safety_narrative_draft.txt</source>
<document_content>
[Document text]
</document_content>
</document>
</source_documents>
First, find and quote every passage in the safety narrative that discusses causality assessment. Place these in <quotes> tags. Then, based only on those quotes, identify any inconsistencies in how causality was assessed across the AEs described. Place your analysis in <analysis> tags.
This two-step pattern — quote first, analyze second — reduces hallucination on document tasks because Claude is reasoning about text it has already surfaced rather than recalling from memory.
Critical for biotech: Quote-extraction grounding is especially important for regulatory documents, safety narratives, and any output that will be cited by reviewers or auditors. "According to the model" is not a defensible citation. "This is grounded in the source text" is.
04 · The system prompt contract
A system prompt is not a place to describe who Claude is. It is a place to define a contract: here is what I need, here is how you succeed, here is what you're not allowed to do, here is what to do when you're uncertain.
The five-part contract structure:
Role: [One sentence defining the function, not the personality]
Success criteria:
- [What a correct output looks like]
- [What quality standard applies]
Constraints:
- [What Claude must never do]
- [Data handling rules]
- [Output format requirements]
Uncertainty handling:
[What Claude should do when it doesn't know — express uncertainty, ask, flag, etc.]
Output format:
[Exact structure, labels, section names, length target]
Example for a regulatory drafting application:
Role: You are a regulatory writing assistant supporting IND submission drafting for a clinical-stage biotech.
Success criteria:
- Draft text meets ICH E3 and FDA guidance for the document type specified
- All factual claims are traceable to source documents provided in context
- Language is appropriate for FDA reviewer audience: precise, unambiguous, no marketing language
Constraints:
- Never include patient identifiers or protected health information
- Never fabricate or infer data not present in the source documents
- Do not include forward-looking commercial claims
- Flag any section where source data is insufficient to complete the draft
Uncertainty handling:
When source material is incomplete or ambiguous, write [REQUIRES VERIFICATION: {specific issue}] inline rather than guessing. Surface ambiguity explicitly rather than resolving it silently.
Output format:
Use the ICH E3 section structure. Begin each section with the section number and title. Use formal prose — no bullet points in draft text. Target length per section: [specified in user message].
Why this matters: Generic system prompts ("You are a helpful assistant") produce generic outputs. Contract-style system prompts produce consistent, predictable, auditable outputs — which is what you need for regulated document work.
05 · Controlling output format
Claude 4.x models are good at following explicit format instructions. Three reliable techniques:
1. Tell Claude what TO do, not what NOT to do:
- Instead of: "Don't use bullet points"
- Use: "Write your response in complete prose paragraphs"
2. Use XML format wrappers:
- Instead of: "Give me the conclusion first"
- Use: "Place your conclusion in
<conclusion>tags at the start of your response, then your analysis in<analysis>tags"
3. Eliminate preamble:
Respond directly without preamble. Do not begin with phrases like "Based on the documents provided," "Certainly," or "Here is my analysis." Begin immediately with the first substantive sentence.
Structured outputs for JSON/schema-constrained responses: For extraction tasks where you need strict JSON conformance, use the Structured Outputs feature (pass a JSON schema) rather than asking Claude to produce JSON in the prompt. Claude 4.x models follow schemas reliably without needing the old prefill trick (which is removed in 4.6+).
06 · Anti-patterns to eliminate
These are the most common context engineering mistakes in biotech AI deployments:
Anti-pattern 1: Vague scope. Telling Claude "apply this to the document" without specifying which sections, all sections, or just the section that is flagged. Claude 4.x is literal — if scope is ambiguous, output is unpredictable. Fix: "Apply this formatting rule to every section heading in the document, not just the first one."
Anti-pattern 2: Dynamic content before the cache breakpoint. If you're using prompt caching (Lesson 03), placing a timestamp, user ID, or session ID before your cached system prompt invalidates the cache on every request. Fix: Put static content first. Dynamic content goes after the cache breakpoint.
Anti-pattern 3: Prompting around a model limitation instead of adjusting effort.
Seeing shallow analysis and adding "Think carefully. Be thorough. Consider all angles." These prompts help on older models. On Opus 4.8, the correct lever is the effort parameter.
Fix: Raise effort to high or xhigh.
Anti-pattern 4: Asking Claude to avoid something vague. "Don't hallucinate" is not an actionable instruction — it's a wish. "If you cannot find this information in the provided documents, write [NOT FOUND IN SOURCE] rather than inferring" is actionable. Fix: State the desired behavior when the thing you want to avoid would occur, not a prohibition against the outcome.
07 · Knowledge check
Q1. You're processing a 400-page clinical study report. Where should the document appear in your prompt relative to your analysis question?
a) Before the question — document first, then the query at the end b) After the question — so Claude knows what to look for before reading c) Split across multiple messages to stay within context limits d) In a separate API call, with only a summary passed in the main prompt
Q2. Your system prompt currently reads: "Do not use bullet points in your responses." Users are still getting bullet-pointed outputs. What is the better instruction?
a) "NEVER use bullet points under any circumstances" b) "Write all responses in complete prose paragraphs" c) "Avoid bullet points where possible" d) "Use headers instead of bullet points"
Q3. For a regulated document drafting application, which system prompt element ensures Claude surfaces data gaps rather than filling them with inference?
a) "Be accurate and precise in your responses" b) "Only use information from the source documents" c) An explicit uncertainty handling rule: if source material is insufficient, write [REQUIRES VERIFICATION: {issue}] inline d) Setting temperature to 0 for deterministic output
Answers: Q1: a · Q2: b · Q3: c
End of Lesson 02.