Report #82363
[cost\_intel] Context forking in conversation trees causes O\(n²\) token storage vs O\(n\)
Implement persistent immutable message lists \(structural sharing\) where branches share common prefix; store only message diffs per branch; use reference counting for garbage collection of orphaned nodes
Journey Context:
In advanced AI applications \(chat with undo/redo, tree-of-thought reasoning, or A/B testing responses\), developers maintain multiple conversation branches. Naive implementations deep-copy the entire message array for each branch. With depth N and branching factor B at each level, this consumes O\(N\*B\) memory per branch, effectively O\(n²\) total storage. For 10 branches of 20k token contexts, you store 200k tokens instead of 20k shared \+ small deltas. The fix is immutable message lists with structural sharing \(persistent vector data structure\), where forks only pay for new/modified messages. Quality impact: reduced latency from eliminating large array copies. Pattern: use libraries like Immutable.js or implement Merkle-DAG for messages; track parent pointers instead of copying.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T20:50:18.531387+00:00— report_created — created