Skip to main content

LLM context contradiction detector and pruner — give your AI a sleep cycle

Project description

delta-prune

LLM context contradiction detector and pruner.

Problem: When LLMs accumulate contradictory information in their context, reasoning accuracy collapses (GPT-4o-mini: 100% → 10%, Gemini: 100% → 0%). This isn't a context length problem — it's a contradiction problem. Making the window bigger doesn't help.

Solution: Scan the conversation for contradictions before sending to the LLM. Remove or annotate them. Three lines of code.

Paper: "Cognitive Sleep for LLMs: How Contradiction Metabolism Prevents Context Rot"

Install

pip install delta-prune                  # core (zero dependencies)
pip install delta-prune[fast]            # with embedding pre-filter (recommended)
pip install delta-prune[openai]          # with OpenAI backend

Quick Start

from delta_prune import DeltaPrune
from delta_prune.llm import ClaudeCLI

prune = DeltaPrune(llm=ClaudeCLI())

messages = [
    {"role": "user", "content": "My favorite food is curry."},
    {"role": "assistant", "content": "Curry, nice!"},
    {"role": "user", "content": "I hate curry. I like ramen."},
    {"role": "assistant", "content": "Ramen it is!"},
    {"role": "user", "content": "What's my favorite food?"},
]

result = prune(messages)
clean_messages = result.messages  # contradictions annotated
print(f"delta = {result.delta}")  # contradiction density
print(f"conflicts = {len(result.conflicts)}")

Strategies

Strategy Behavior
"annotate" (default) Add a system message listing detected contradictions
"prune" Remove messages containing the older side of contradictions
"report" Detect only, return original messages unchanged
prune = DeltaPrune(llm=llm, strategy="prune")     # remove old contradictions
prune = DeltaPrune(llm=llm, strategy="annotate")   # add context annotation
prune = DeltaPrune(llm=llm, strategy="report")     # detect only, no changes

LLM Backends

from delta_prune.llm import ClaudeCLI, OllamaLLM, OpenAILLM

# Claude CLI (subscription, $0)
prune = DeltaPrune(llm=ClaudeCLI(model="sonnet"))

# Local Ollama
prune = DeltaPrune(llm=OllamaLLM(model="gemma3:27b"))

# OpenAI API
prune = DeltaPrune(llm=OpenAILLM(model="gpt-4o-mini"))

Performance: Embedding Pre-Filter

For conversations with many messages, use an embedding to avoid O(n²) LLM calls:

from delta_prune.embedding import SentenceTransformerEmbedding

prune = DeltaPrune(
    llm=ClaudeCLI(),
    embedding=SentenceTransformerEmbedding(),  # local, free
    similarity_threshold=0.7,                  # only check similar pairs
    max_llm_pairs=30,                          # hard cap on LLM calls
)
Mode 20 claims 50 claims 100 claims
No embedding (O(n²)) 190 LLM calls 1,225 calls 4,950 calls
With embedding ~10-30 LLM calls ~10-30 calls ~10-30 calls

Install with: pip install delta-prune[fast]

Language

English is the default. Japanese is available:

prune = DeltaPrune(llm=llm, locale="ja")  # Japanese prompts

How It Works

messages (raw conversation)
    ↓
① Extract: pull factual claims from each message (LLM)
    → [("likes curry", turn=0), ("hates curry, likes ramen", turn=2)]
    ↓
② Pre-filter (optional): embedding similarity → keep only similar pairs
    → [("curry:like" ↔ "curry:hate")] — skip unrelated pairs
    ↓
③ Detect: LLM checks filtered pairs for contradictions
    → [conflict: "food preference changed"]
    ↓
④ Resolve: prune / annotate / report
    ↓
⑤ Return: clean messages + delta score + conflict details

Delta Score

result.delta = contradiction density (conflicts / claims). 0.0 = clean, higher = more contradictions.

Based on the survival equation: S = μ × e^(-δ×k). Reducing δ has an exponential effect on reasoning quality.

Background

Based on research showing that context rot is caused by contradiction accumulation, not context length. Tested across 8 LLM models with statistically significant results (Kruskal-Wallis p=0.027, complete rank separation). See DeltaZero for the full research.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

delta_prune-0.2.0.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

delta_prune-0.2.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file delta_prune-0.2.0.tar.gz.

File metadata

  • Download URL: delta_prune-0.2.0.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for delta_prune-0.2.0.tar.gz
Algorithm Hash digest
SHA256 590352331a55359d967b90cb3abf059e7f098f8c65bf0f9f021bc93bda1dbbde
MD5 969755f0424f7e6cb33d038eae51ea9c
BLAKE2b-256 33b1b651f81362142e7fb07e7b62b441292eecd35cae7d16fd423084bfd4a173

See more details on using hashes here.

File details

Details for the file delta_prune-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: delta_prune-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for delta_prune-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 471d13948615bf1ff6bbf91a5d9fd9a8e31b829e97d2b6a49fdcb25359b0a53e
MD5 5f6e1cdfdc6f1d8f2c53a8345cca477b
BLAKE2b-256 78cc9fdd423716f67e3a7fd09957dc72eb9856da1928ced7f4719e4da0511d1d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page