The delicate art and science of filling the context window with just the right information for the next step. - Andrej Karpathy
Context Engineering is a collection of techniques for optimizing how information is provided to Large Language Models (LLMs). This repository provides practical implementations of six core context engineering techniques, helping you build more effective and efficient AI agents.
As LLMs are increasingly used in production systems, managing context windows effectively has become critical. Research from Chroma on Context Rot shows that LLM performance degrades as input length grows - often in non-uniform and surprising ways across 18+ models including GPT-4, Claude, and Gemini.
This repository implements the six context engineering techniques outlined in Drew Breunig's influential post "How to Fix Your Context", providing both LangGraph and CrewAI implementations to help you understand and apply these techniques in your own projects.
LLMs don't treat every token in their context window equally. As context grows, four failure modes emerge:
- Context Poisoning - Hallucinations or errors that enter the context and get repeatedly referenced
- Context Distraction - Models focus more on accumulated history than their training
- Context Confusion - Superfluous content influences response quality negatively
- Context Clash - Conflicting information within the context degrades reasoning
Context engineering techniques systematically address these failure modes by:
- π― Selecting the most relevant information (RAG, Tool Loadout)
- π‘οΈ Isolating contexts to prevent interference (Context Quarantine)
- βοΈ Removing or condensing unnecessary information (Context Pruning, Summarization)
- πΎ Offloading information to external storage (Context Offloading)
- Python 3.10 or higher (< 3.14 for CrewAI projects)
- uv package manager
- API keys (OpenAI, Anthropic, or Google Gemini depending on implementation)
- Clone the repository:
git clone https://github.com/saishshinde15/Context_Engineering.git
cd Context_Engineering- Choose your technique and navigate to its directory:
# For LangGraph implementations
cd how_to_fix_your_context
# For CrewAI implementations
cd context_pruning # or context_offloading- Install dependencies:
# For LangGraph
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
uv pip install -r requirements.txt
# For CrewAI
crewai install- Configure API keys:
# Create .env file with your API keys
export OPENAI_API_KEY="your-key-here"
export ANTHROPIC_API_KEY="your-key-here"
export GEMINI_API_KEY="your-key-here"- Run the examples:
# LangGraph: Open Jupyter notebooks
jupyter notebook notebooks/
# CrewAI: Run the crew
crewai runContext_Engineering/
β
βββ π how_to_fix_your_context/ # LangGraph implementations
β βββ notebooks/
β β βββ 01-rag.ipynb # RAG implementation
β β βββ 02-tool-loadout.ipynb # Tool Loadout
β β βββ 03-context-quarantine.ipynb # Context Quarantine
β β βββ 04-context-pruning.ipynb # Context Pruning
β β βββ 05-context-summarization.ipynb # Context Summarization
β β βββ 06-context-offloading.ipynb # Context Offloading
β βββ README.md
β
βββ π context_pruning/ # CrewAI Context Pruning
β βββ src/context_pruning/
β β βββ config/ # Agent & task configs
β β βββ tools/ # RAG & pruning tools
β β βββ crew.py # Crew orchestration
β β βββ main.py # Entry point
β βββ README.md # Detailed documentation
β βββ QUICKSTART.md # Quick start guide
β βββ VISUAL_GUIDE.md # Visual workflow
β βββ IMPLEMENTATION_NOTES.md # Technical details
β
βββ π context_offloading/ # CrewAI Context Offloading
β βββ src/context_offloading/
β β βββ config/ # Agent & task configs
β β βββ tools/ # Scratchpad tools
β β βββ crew.py # Crew orchestration
β β βββ main.py # Entry point
β βββ README.md # Detailed documentation
β βββ QUICKSTART.md # Quick start guide
β βββ DOCUMENTATION.md # Complete docs
β
βββ README.md # This file
What it is: Selectively adding relevant information to help the LLM generate better responses.
Implementation: how_to_fix_your_context/notebooks/01-rag.ipynb
Key Features:
- Document loading and chunking
- Vector store with semantic search
- LangGraph StateGraph with tool calling
- Uses Claude Sonnet for intelligent retrieval
Use Case: When you need to ground LLM responses in specific knowledge sources.
What it is: Selecting only relevant tool definitions to add to your context.
Implementation: how_to_fix_your_context/notebooks/02-tool-loadout.ipynb
Key Features:
- Semantic tool selection via vector store
- Dynamic tool binding based on query
- Prevents context confusion from overlapping tool descriptions
- Indexes Python math library functions
Use Case: When you have many tools and want to avoid overwhelming the context.
What it is: Isolating contexts in dedicated threads, each used separately by specialized agents.
Implementation: how_to_fix_your_context/notebooks/03-context-quarantine.ipynb
Key Features:
- Supervisor multi-agent architecture
- Specialized agents (Math Expert, Research Expert)
- Isolated context windows per agent
- Tool-based handoffs for complex tasks
Use Case: When different tasks require different expertise and context.
What it is: Removing irrelevant or unneeded information from the context.
Implementations:
- LangGraph:
how_to_fix_your_context/notebooks/04-context-pruning.ipynb - CrewAI:
context_pruning/
Key Features:
- Token Reduction: 40-60% reduction in context size
- Improved Accuracy: Focused context leads to better answers
- Cost Efficiency: Uses smaller models (GPT-4o-mini or Gemini Flash) for pruning
- Three-Agent Workflow: Retrieval β Pruning β Synthesis
Performance: Reduces token usage from 25k to 11k tokens while maintaining answer quality.
Use Case: When retrieved content contains a mix of relevant and irrelevant information.
π Extended Documentation:
- README.md - Complete guide
- QUICKSTART.md - 3-step quick start
- VISUAL_GUIDE.md - Detailed workflow visualization
- IMPLEMENTATION_NOTES.md - Technical deep dive
What it is: Condensing accumulated context into a summary while preserving essential information.
Implementation: how_to_fix_your_context/notebooks/05-context-summarization.ipynb
Key Features:
- Compresses all content (not just irrelevant parts)
- Preserves key information while eliminating verbosity
- 50-70% reduction target
- Uses GPT-4o-mini for cost efficiency
Use Case: When all retrieved content is relevant but too verbose.
What it is: Storing information outside the LLM's context window in external storage.
Implementations:
- LangGraph:
how_to_fix_your_context/notebooks/06-context-offloading.ipynb - CrewAI:
context_offloading/
Key Features:
- Session Scratchpad: Temporary storage within a conversation
- Persistent Memory: Cross-thread storage using key-value pairs
- Organized Storage: Category-based organization
- Reusability: Information survives across sessions
Use Case: Long research tasks, multi-step workflows, or when information needs to persist across conversations.
π Extended Documentation:
- README.md - Complete guide
- QUICKSTART.md - 3-step quick start
- DOCUMENTATION.md - Architecture details
Framework: Low-level orchestration with full control over flow
Characteristics:
- β StateGraph with explicit nodes and edges
- β Custom state management
- β Direct tool binding to LLMs
- β Conditional flow control
- β Fine-grained control over execution
Best For: Complex workflows requiring precise control
Location: how_to_fix_your_context/
Framework: High-level agent framework with declarative configuration
Characteristics:
- β Agent-based workflow with automatic context passing
- β YAML configuration for agents and tasks
- β Agents decide when to use tools
- β Sequential or hierarchical processes
- β Easier to read and maintain
Best For: Rapid prototyping and clear agent roles
Locations: context_pruning/, context_offloading/
| Aspect | LangGraph | CrewAI |
|---|---|---|
| Abstraction Level | Lower (more control) | Higher (more productivity) |
| Configuration | Python code | YAML + Python |
| State Management | Manual with custom classes | Automatic context passing |
| Tool Calling | Explicit binding | Agent decides |
| Flow Control | Conditional edges | Task dependencies |
| Learning Curve | Steeper | Gentler |
| Flexibility | Maximum | Opinionated |
| Use Case | Complex, custom workflows | Standard agent workflows |
- Start with RAG to understand basic retrieval concepts
- Explore Context Pruning (CrewAI) for a complete, well-documented implementation
- Try Tool Loadout to see semantic tool selection in action
- Study Context Quarantine for multi-agent patterns
- Implement Context Summarization for compression techniques
- Experiment with Context Offloading for persistent memory
- Compare LangGraph vs CrewAI implementations side-by-side
- Combine multiple techniques in a single application
- Adapt techniques to your specific use cases
- Token Reduction: 40-60% (25k β 11k tokens)
- Cost Savings: ~50% on LLM API calls
- Execution Time: 20-30 seconds
- Quality: Maintained or improved answer quality
- Compression: 50-70% size reduction
- Information Retention: High (preserves key facts)
- Best For: Verbose but relevant content
- Memory Efficiency: Unlimited external storage
- Persistence: Cross-session memory
- Best For: Long-running research tasks
- How to Fix Your Context - Drew Breunig
- How Contexts Fail - Drew Breunig
- Context Rot Research - Chroma
- Context Engineering for Agents - LangChain
- Don't Build Multi-Agents - Cognition AI
- Context Engineering for AI Agents - Manus
- Karpathy on Context Engineering - Twitter/X
Contributions are welcome! Here are some ways you can contribute:
- π Report bugs or issues
- π‘ Suggest new context engineering techniques
- π Improve documentation
- π§ Add new implementations or examples
- β Star the repository if you find it useful
This project is licensed under the MIT License - see the LICENSE file for details.
- Drew Breunig for the original context engineering framework
- LangChain/LangGraph team for the excellent orchestration tools
- CrewAI team for the powerful agent framework
- Lilian Weng for her informative blog posts used in examples
- Chroma team for their research on context rot
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- LangGraph: Discord
- CrewAI: Discord
- Add evaluation metrics for each technique
- Implement hybrid approaches (combining multiple techniques)
- Add streaming support for real-time applications
- Create benchmarking suite
- Add support for more LLM providers
- Implement caching strategies
- Add video tutorials and walkthroughs
Built with β€οΈ for the AI Agent community
If this repository helps you, please consider giving it a β!