
Build a context-pruning script for your agent
You'll have a working script that automatically decides which information your AI agent should keep in active memory versus archive or retrieve later.
What you'll make
Context Pruning Script
- analyze_retrieval_patterns() — usage pattern detection: what to include
- prune_by_pattern() — the core pruning logic: what to include
- archive_unused() — moving cold context to retrieval: what to include
- main() — orchestration and logging: what to include
Paste your working Python script with at least one real pruning rule based on your agent's actual usage data
The 4 parts
Each part ends with a check you can see.
Map Your Context Accumulation
Analyze how your agent actually accumulates and uses context over multiple sessions
Done when
A documented analysis of your agent's context patterns with at least 5 specific examples of information that stayed active vs got archived vs was never retrieved again
Build the Pattern Detection Function
Create analyze_retrieval_patterns() that identifies which context gets reused vs ignored in your agent
Done when
A working analyze_retrieval_patterns() function that processes your agent logs and outputs usage categories for each piece of context
Implement Smart Pruning Rules
Build prune_by_pattern() and archive_unused() functions that act on usage patterns from your real agent data
Done when
Working prune_by_pattern() and archive_unused() functions with at least 3 specific pruning rules derived from your usage analysis
Test and Tune the Complete Script
Wire everything together in main() and run the complete context-pruning script on real agent sessions
Done when
A complete working script that successfully manages context in at least one real agent session, with documentation of what got pruned and why
Capstone: you put together your Context Pruning Script.
Where this helps
An elective: a single lesson that sits outside rtwo's job certificates and doesn't count toward one.
About this lesson
What you'll be able to do
- Run a working context-pruning script that manages your agent's working memory based on real retrieval patterns
- Identify which information your agent should keep active vs archive vs retrieve-on-demand
- Have documented rules for context compression that match your agent's actual usage patterns
- Know when to trust automated pruning vs when to override with manual rules
Who it's for
- Anyone building or working with AI agents that accumulate context over multiple interactions
- People who've hit context limits and want systematic memory management
- Developers comfortable with Python scripts and vector database concepts
What it won't cover
- Setting up vector databases from scratch — assume you have one running
- Advanced embedding model training or fine-tuning
- Production deployment with failover and monitoring
- Multi-agent coordination or shared memory pools
Skills you'll practise
- Context window management
- Retrieval pattern analysis
- Memory hierarchy design
- Automated pruning rule creation