Context Design
Learn to design effective context through information architecture, ordering strategies, structured formats, context templates, and dynamic assembly patterns.
What to Include vs Exclude
The first decision in context design is choosing what information to provide. Including too much wastes tokens and can confuse the model. Including too little produces shallow or inaccurate responses.
| Include | Exclude |
|---|---|
| Information directly relevant to the query | Tangentially related background |
| Definitions of domain-specific terms | Common knowledge the model already has |
| Constraints and requirements | Duplicate information (same fact stated multiple ways) |
| Examples of desired output | Irrelevant metadata (timestamps, IDs, boilerplate) |
| Edge cases and exceptions | Personal opinions not relevant to the task |
Ordering Matters: Primacy and Recency Effects
AI models, like humans, are influenced by the order in which information is presented. Two key effects shape how context is processed:
Recency effect: Information at the end of the context (closest to where the model starts generating) has strong influence on the immediate response. Place the specific task and any critical constraints here.
BEGINNING (Primacy Zone - High Influence) 1. System instructions and persona 2. Core rules and constraints 3. Most important reference documents MIDDLE (Lower Influence) 4. Supporting documents and data 5. Conversation history 6. Background information END (Recency Zone - High Influence) 7. The specific task or question 8. Output format requirements 9. Final reminders of critical constraints
Structured Context Formats
Structuring context with clear formatting helps models parse and reference information more accurately.
XML Tags (Claude's Preferred Format)
<context> <company_info> Name: AcmeCorp Industry: Enterprise SaaS ARR: $5M Employees: 45 </company_info> <product_docs> <doc title="API Reference" updated="2026-03"> [API documentation content] </doc> <doc title="User Guide" updated="2026-02"> [User guide content] </doc> </product_docs> <user_profile> Plan: Enterprise Role: Admin Account age: 2 years </user_profile> </context> <task> Answer the user's question using only the information in the context above. </task>
Markdown Sections
## Company Information - Name: AcmeCorp - Industry: Enterprise SaaS - ARR: $5M ## Relevant Documentation ### API Reference (Updated March 2026) [API documentation content] ### User Guide (Updated February 2026) [User guide content] ## User Profile - Plan: Enterprise - Role: Admin --- ## Task Answer the user's question using the above context.
Context Templates
Context templates are reusable patterns for assembling context. They define slots for different types of information and can be dynamically filled at runtime.
class ContextBuilder: def __init__(self): self.sections = [] def add_system_rules(self, rules: list[str]): formatted = "\n".join( f"- {r}" for r in rules ) self.sections.append( f"<rules>\n{formatted}\n</rules>" ) return self def add_documents(self, docs: list[dict]): doc_text = "" for doc in docs: doc_text += f"\n<doc title='{doc['title']}'>" doc_text += f"\n{doc['content']}" doc_text += f"\n</doc>" self.sections.append( f"<documents>{doc_text}\n</documents>" ) return self def build(self) -> str: return "\n\n".join(self.sections)
Dynamic Context Assembly
In production applications, context is assembled dynamically based on the user's query, their profile, and relevant data. This is the core of context engineering in practice.
Analyze the query
Determine what the user is asking about to select relevant context sources.
Retrieve relevant documents
Use vector search or keyword search to find matching content from your knowledge base.
Enrich with user context
Add user profile, preferences, history, and permissions.
Apply token budget
Fit everything within the context window, prioritizing by relevance.
Assemble and format
Combine all components using your context template with proper structure.
Personalization Through Context
Context enables personalized AI experiences without fine-tuning. By including user-specific information, you tailor every response:
User Profile
Include expertise level, role, preferences, and communication style to adapt responses automatically.
Interaction History
Reference past conversations and outcomes to build continuity and avoid repeating information.
Environment
Include timezone, language, platform, and other environmental factors for relevant responses.
Goals & Preferences
Track user objectives and preferences to proactively surface relevant information and suggestions.
Lilly Tech Systems