Skip to content

saishshinde15/Context_Engineering

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Context Engineering

The delicate art and science of filling the context window with just the right information for the next step. - Andrej Karpathy

License: MIT

πŸ“– Overview

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.

🎯 Why Context Engineering Matters

The Context Problem

LLMs don't treat every token in their context window equally. As context grows, four failure modes emerge:

  1. Context Poisoning - Hallucinations or errors that enter the context and get repeatedly referenced
  2. Context Distraction - Models focus more on accumulated history than their training
  3. Context Confusion - Superfluous content influences response quality negatively
  4. Context Clash - Conflicting information within the context degrades reasoning

The Solution

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)

πŸš€ Quick Start

Prerequisites

  • Python 3.10 or higher (< 3.14 for CrewAI projects)
  • uv package manager
  • API keys (OpenAI, Anthropic, or Google Gemini depending on implementation)

Installation

  1. Clone the repository:
git clone https://github.com/saishshinde15/Context_Engineering.git
cd Context_Engineering
  1. 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
  1. 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
  1. 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"
  1. Run the examples:
# LangGraph: Open Jupyter notebooks
jupyter notebook notebooks/

# CrewAI: Run the crew
crewai run

πŸ“š Repository Structure

Context_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

πŸ› οΈ Context Engineering Techniques

1. πŸ” RAG (Retrieval-Augmented Generation)

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.


2. 🧰 Tool Loadout

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.


3. πŸ”’ Context Quarantine

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.


4. βœ‚οΈ Context Pruning

What it is: Removing irrelevant or unneeded information from the context.

Implementations:

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:


5. πŸ“Š Context Summarization

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.


6. πŸ’Ύ Context Offloading

What it is: Storing information outside the LLM's context window in external storage.

Implementations:

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:

πŸ”§ Implementation Frameworks

LangGraph Implementation

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/

CrewAI Implementation

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/

πŸ“Š Comparison: LangGraph vs CrewAI

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

πŸŽ“ Learning Path

Beginners

  1. Start with RAG to understand basic retrieval concepts
  2. Explore Context Pruning (CrewAI) for a complete, well-documented implementation
  3. Try Tool Loadout to see semantic tool selection in action

Intermediate

  1. Study Context Quarantine for multi-agent patterns
  2. Implement Context Summarization for compression techniques
  3. Experiment with Context Offloading for persistent memory

Advanced

  1. Compare LangGraph vs CrewAI implementations side-by-side
  2. Combine multiple techniques in a single application
  3. Adapt techniques to your specific use cases

πŸ“ˆ Performance Benchmarks

Context Pruning

  • 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

Context Summarization

  • Compression: 50-70% size reduction
  • Information Retention: High (preserves key facts)
  • Best For: Verbose but relevant content

Context Offloading

  • Memory Efficiency: Unlimited external storage
  • Persistence: Cross-session memory
  • Best For: Long-running research tasks

πŸ”¬ Research & References

Core Concepts

Frameworks

Influential Posts

🀝 Contributing

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

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • 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

πŸ“ž Support & Community

πŸ—ΊοΈ Roadmap

  • 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 ⭐!

Report Bug Β· Request Feature Β· Documentation

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors