AI Context Engineering – Ultimate Cheat Sheet
???? Core Principles Context Window Limited token budget (~200k tokens typical) Context = Input + Output combined Earlier tokens may be "forgotten" (lost attention) Most recent context has…

???? Core Principles
Context Window
- Limited token budget (~200k tokens typical)
- Context = Input + Output combined
- Earlier tokens may be “forgotten” (lost attention)
- Most recent context has highest weight
Information Density
- Dense, structured input > verbose
- Use XML/JSON for complex data
- Key info at start AND end
- Remove redundancy ruthlessly
???? Prompt Structure
Optimal Template
<role>Expert persona</role>
<task>What to do</task>
<context>Background info</context>
<constraints>Rules</constraints>
<output_format>Structure</output_format>
<examples>…</examples>
Critical Elements
- Clear objective (single, specific)
- Success criteria defined
- Format requirements explicit
- Edge cases addressed
???? Few-Shot Techniques
Example Types
- Positive: Ideal outputs
- Negative: What to avoid
- Edge cases: Boundary conditions
- Progressive: Simple → Complex
Best Practices
- 3-5 examples optimal
- Diverse, representative samples
- Annotate reasoning process
- Match target complexity
???? Chain-of-Thought
Activation Phrases
Zero-shot: “Let’s think step by step…”
“First, analyze… Then…”
“Break this down systematically”
“First, analyze… Then…”
“Break this down systematically”
Advanced Patterns
- Self-Consistency: Multiple reasoning paths
- Tree-of-Thought: Explore branches
- Meta-prompting: Analyze prompt first
⚙️ Token Optimization
Compression Strategies
- Use abbreviations consistently
- Remove articles (a, an, the)
- Prefer numerals over words
- Use symbols: → ∴ ∵ ≈
Token Count Rules
~4 chars = 1 token
100 words ≈ 75 tokens
100 words ≈ 75 tokens
- Whitespace counts
- Code is token-expensive
- Repeated patterns compress
???? Role & Persona Engineering
Effective Personas
Generic: “You are helpful”
Specific: “You are a senior DevOps engineer with 15 years experience in Kubernetes, known for writing clear, actionable documentation”
Specific: “You are a senior DevOps engineer with 15 years experience in Kubernetes, known for writing clear, actionable documentation”
- Specific expertise domain
- Years of experience
- Communication style
- Known strengths/weaknesses
Multi-Agent Patterns
- Debate: Pro vs Con roles
- Specialist Panel: Expert consultation
- Red Team: Adversarial review
- Socratic: Question-driven dialogue
Analyst: {analysis}
Critic: {critique}
Synthesizer: {final_output}
???? Output Control
Format Specifications
- Structured: JSON, XML, YAML
- Length: Exact word/token count
- Style: Tone, formality, POV
- Constraints: No speculation, cite sources
Validation Triggers
“Before responding, verify:
✓ All claims sourced
✓ Format matches schema
✓ Edge cases handled”
✓ All claims sourced
✓ Format matches schema
✓ Edge cases handled”
????️ Context Window Management
Prioritization Strategies
- Recency: Latest messages matter most
- Relevance: Semantic similarity search
- Importance: Tag critical information
- Summarization: Compress old context
Priority_Score = (Recency × 0.3) + (Relevance × 0.5) + (Importance × 0.2)
Memory Patterns
- Sliding Window: Keep N recent turns
- Hierarchical: Summary + recent detail
- Retrieval: Vector DB + current context
- Compression: Distill multi-turn to summary
System: {permanent_instructions}
Summary: {condensed_history}
Recent: {last_3_turns}
Current: {user_message}
???? RAG (Retrieval)
Context Injection
- Embed retrieved docs with metadata
- Source attribution required
- Chunk size: 200-500 tokens optimal
- Include relevance scores
Quality Signals
<source confidence=”0.89″>
<metadata>Updated: 2024-01</metadata>
{retrieved_content}
</source>
<metadata>Updated: 2024-01</metadata>
{retrieved_content}
</source>
???? Advanced Methods
Self-Reflection
1. Generate initial response
2. Critique: “What’s wrong?”
3. Refine based on critique
4. Repeat if needed
Constitutional AI
- Embed principles in system
- Self-critique against rules
- Revision loops
???? Semantic Control
Embedding Strategies
- Similar examples cluster together
- Use for dynamic few-shot selection
- Semantic search > keyword match
- Rerank by similarity score
Distance Metrics
Cosine Similarity Euclidean Dot Product⚠️ Common Pitfalls & Solutions
❌ Vague Instructions
Bad: “Write something good”
Good: “Write a 500-word technical blog post for developers explaining OAuth 2.0 flow with code examples in Python”
Good: “Write a 500-word technical blog post for developers explaining OAuth 2.0 flow with code examples in Python”
❌ Conflicting Directives
Bad: “Be brief but comprehensive”
Good: “Provide a 200-word summary covering the 3 most important points”
Good: “Provide a 200-word summary covering the 3 most important points”
❌ Assumed Knowledge
Bad: “Fix the bug”
Good: “The login form submits but returns 401. Here’s the code: {…}”
Good: “The login form submits but returns 401. Here’s the code: {…}”
❌ No Format Spec
- Always define output structure
- Use schema/template
- Example of desired format
❌ Ignoring Tokens
- Monitor context usage
- Compress when possible
- Truncate intelligently
❌ One-Shot Hoping
- Iterate prompts
- Test variations
- A/B test approaches
???? Testing & Evaluation
Evaluation Metrics
- Accuracy: Factual correctness
- Relevance: On-topic responses
- Coherence: Logical flow
- Consistency: Same input → same output
- Completeness: All requirements met
Testing Strategies
- Build test suite of edge cases
- Use golden datasets
- Temperature testing (0.0 → 1.0)
- Automated regression tests
- Human-in-loop validation
⚡ Quick Wins
Instant Improvements
- ✓ Add “Let’s think step-by-step”
- ✓ Provide 2-3 examples
- ✓ Define output format
- ✓ Use XML tags for structure
- ✓ Specify desired length
- ✓ Include validation criteria
???? Model-Specific Strategies
Claude-Specific
- Prefers XML structure
- Excels at long context
- Strong with technical tasks
- Use <thinking> tags for reasoning
GPT-Specific
- Follows system messages strongly
- JSON mode available
- Function calling native
- Sensitive to instruction order
????️ Parameters
Temperature Guide
- 0.0-0.3: Deterministic, factual
- 0.4-0.7: Balanced creativity
- 0.8-1.0: Highly creative
Other Parameters
- top_p: Nucleus sampling (0.9 typical)
- max_tokens: Hard output limit
- frequency_penalty: Reduce repetition
???? Security & Safety
Prompt Injection Defense
- Sandwich user input with instructions
- Use delimiters clearly
- Validate output format strictly
- Never trust user-provided “system” messages
Content Filtering
<user_input>
{sanitized_input}
</user_input>
Respond only to legitimate requests
