If youβre running local LLMs for agent memory, youβve hit the wall: your session logs and memory files exceed the context window. agentdream, an open-source Python tool, tackles this by "dreaming" over agent data using a sophisticated chunked map-reduce strategy. Instead of forcing a massive input into a single call, it splits the work, processes chunks individually, and merges the results deterministically. This approach allows agents to maintain long-term memory even with constrained context budgets, such as those found in models like llama3.1:8b.
The Jigsaw Puzzle Analogy and Budget Constraints
The core analogy is a 5,000-piece jigsaw puzzle on a table that only holds 500 pieces. You can't lay it all out at once. agentdream sorts pieces onto trays (chunks), works one tray at a time, and joins the finished sections. Three settings control this: max_chunk_tokens (default 60,000), chunk_overlap (default 400 tokens), and max_chunks (default 64). For small local models, users can shrink the budget via command line flags, such as --max-chunk-tokens 6000. The tool uses a crude but dependency-free token estimator (len(text) + 3) // 4 to keep the core lightweight.
Smart Sorting and Overlap Strategies
Before chunking, agentdream reorders memory entries to group likely duplicates and related subjects. It uses Jaccard similarity scores on token sets and subjects to pull similar notes together, ensuring that a single LLM call sees related context. For transcripts, chunks overlap within a session to capture context that might span boundaries, such as a problem and its subsequent fix. However, the tool strictly prevents rewriting entries that were only partially seen by a model, marking them as read-only to avoid hallucinated corrections.
Deterministic Reduction Without LLMs
After the map phase, a reduce step merges proposals without calling an LLM. This ensures consistency and speed. Identical proposals are collapsed, and overlapping merges are combined transitively. If conflicts ariseβsuch as two chunks proposing different corrections for the same entryβa fixed tie-breaker decides the winner based on evidence length, confidence, and operation type. Losers arenβt discarded; they are logged in the winnerβs history as alternative proposals, preserving the audit trail.
Key Takeaways
- Chunking is essential for local LLMs with small context windows, but naive splitting fails; you must sort related data together first.
- The reduce step should be deterministic code, not another LLM call, to ensure reproducible results regardless of chunk size.
- Content-based IDs ensure that the same memory operation gets the same identifier, whether it was processed in one chunk or twelve.
- Overlap is critical for session transcripts to maintain context continuity, but it must be capped to avoid wasting tokens.
The Bottom Line
agentdream proves that you don't need a 128k context window to have a smart agent; you just need to be smarter about how you feed it data. The zero-dependency approach is a breath of fresh air in a bloatware ecosystem.