Back
51/65
Lesson 51
8 min
Context Window Mastery
Maximize agent context efficiency with TRU, S2P, CASS, and CM
Ready to level up?
Mark complete to track your learning progress.
Maximize agent context efficiency with TRU, S2P, CASS, and CM
Mark complete to track your learning progress.
Maximize what fits in your agent's context window using TRU, S2P, CASS, and CM — the difference between an agent that forgets and one that remembers everything.
Every AI agent has a finite context window. When it fills up, the agent compacts (forgets earlier messages) or degrades. Four tools help you fit more useful information into less space.
40-70% token reduction via compression
Bundle source files into a single prompt
Retrieve specific solutions, not whole files
Distilled rules instead of raw session logs
TRU (toon_rust) converts JSON to TOON, a notation that carries the same data with far fewer quotes, braces and commas. It is for structured data (API responses, search results, config, exports), not for source code. Tables of records shrink by roughly half.
# Encode a JSON file before handing it to an agenttoon results.json# → same records as TOON, no braces/quotes, one line per row# Check the estimate firsttoon --stats results.json# → Token estimates: ~78 (JSON) → ~53 (TOON)# Pipe JSON straight from another toolgh issue list --json number,title,labels | toon --encode# Decode back to JSON when a program needs ittoon results.toon
S2P (Source to Prompt) combines multiple source files into a single, well-structured prompt with automatic token counting. Instead of agents reading files one by one, give them everything at once.
# Bundle all TypeScript files in a directorys2p src/api/ --include "*.ts"# Bundle with token count for each files2p src/ --include "*.ts" --show-tokens# Exclude test files and node_moduless2p src/ --include "*.ts" --exclude "*test*,*spec*"# Output to clipboard for pastings2p src/components/ --include "*.tsx" --clipboard
Instead of dumping entire files into context, use CASS to retrieve specific snippets from past sessions where a problem was already solved.
# Find how you solved a specific problem beforecass search "postgres connection pool exhaustion fix" --robot --limit 3# Get targeted context with just the relevant snippetcass view /path/to/session.jsonl -n 42 --json# → Returns just the solution, not the whole 10,000-line session# Expand context around a hit if you need more detailcass expand /path -n 42 -C 3 --json# Use minimal fields to save tokens in the responsecass search "auth middleware pattern" --robot --fields minimal
--fields minimal to get just the snippet and score, not the full metadata. This can cut the response size by 60%.CM distills entire sessions into single-line rules. Instead of re-reading a 500-message session, your agent gets “Always use bcrypt with cost factor 12” — one line instead of 50,000 tokens.
# Get all relevant rules for a task (tiny token footprint)cm context "implement rate limiting" --json# → Returns 3-5 rules, ~200 tokens total# Compare to reading the full session:# Rule: "Use sliding window, not fixed window, for rate limiting"# vs. re-reading a 2-hour debugging session (50,000+ tokens)# The memory protocol in action:# 1. Agent starts → cm context loads ~200 tokens of rules# 2. Agent works → references rule IDs in decisions# 3. Agent finishes → new rules extracted for future use
Here's how to combine all four tools for maximum context efficiency.
Result: An agent that starts with distilled knowledge, targeted solutions, and compressed source — fitting 10x more useful information into the same context window.