A lightweight, zero-dependency context sanitizer that keeps models sharp, eliminates conversational loops, and cuts token bills by 20% to 50%.
Have you ever noticed that the longer an LLM conversation goes, the worse it gets?
- The Self-Attention Echo Loop: If the assistant opens with "Certainly! As you mentioned..." or brings up an assumption on Turn 2, by Turn 10 it fixates on that phrase, repeating it obsessively like a broken record.
- Context Poisoning: Outdated bug traces and discarded code attempts remain in the history forever. The model sees the buggy code 5 times and the working fix only onceโfrequently hallucinating or regressing back to old errors.
- Token Bloat: Chats balloon from 500 tokens to 15,000 tokens within a few turns. Over 40% of the payload is repetitive assistant filler and stale error logs.
AntiEcho is a deterministic, sub-millisecond context hygiene layer. It intercepts your message history before it reaches the model and:
- โ๏ธ Breaks Echo Loops: Strips repetitive assistant boilerplate, conversational filler, and cross-turn echoed phrases.
- ๐งน Collapses Stale Errors: Detects verbose stack traces and compiler errors that were already resolved in earlier turns and collapses them into 1-line pointers.
- ๐ฆ Supersedes Old Code Drafts: Replaces outdated, multi-line code drafts from earlier turns with concise references so the model only focuses on the latest implementation.
- ๐ง Distills Active Memory: Extracts critical user constraints ("Language: Python 3.12", "Avoid external ORMs") and pins them cleanly in the system prompt.
Raw Chat History (40 messages, 12k tokens, repetitive loops, stale errors)
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AntiEcho โ
โ โ
โ 1. โ๏ธ Anti-Echo & Loop Detector โ
โ Strips repetitive assistant boilerplate & phrases โ
โ โ
โ 2. ๐ง Active Memory Distiller โ
โ Distills old turns into Facts & Constraints โ
โ โ
โ 3. ๐งน Stale Context Pruner โ
โ Collapses fixed errors & superseded code drafts โ
โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
Cleaned Context (Sharp messages + pinned state, -35% tokens, 0 hallucinations)
pip install antiecho(Zero heavy dependencies. No PyTorch, no Docker, no external databases.)
Works with OpenAI, Anthropic Claude, Gemini, Groq, Ollama, LangChain, or raw dicts:
import antiecho
messages = [
{"role": "system", "content": "You are a coding assistant."},
{"role": "user", "content": "We are using Python 3.12 and SQLite. Write a migration script."},
{"role": "assistant", "content": "Certainly! As you mentioned, we are using Python 3.12 and SQLite..."},
{"role": "user", "content": "I got an error: Traceback (most recent call last)... OperationalError: unable to open db"},
{"role": "assistant", "content": "Here is the fix: create the directory first."},
{"role": "user", "content": "Awesome, that worked! Now add email to the schema."},
{"role": "assistant", "content": "Certainly! As you mentioned, we are using Python 3.12 and SQLite..."} # <-- Echo loop!
]
# Clean and de-echo in 1 line
clean_messages = antiecho.clean(messages)
# Inspect the savings
stats = antiecho.get_last_stats()
print(stats.summary())
# Output: "AntiEcho: Saved 184 tokens (-28.4%) | Pruned 4 echoes, 1 resolved error | Pinned 2 active facts"Automatically sanitize conversations in-flight without touching the rest of your codebase:
from openai import OpenAI
from antiecho import wrap
# Wrap your client once
client = wrap(OpenAI())
# Calls work exactly as usual, but context is sanitized automatically before API delivery!
response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)AntiEcho includes a real-time interactive terminal chat that connects directly to live models (via OpenRouter or OpenAI) and dynamically inspects context on every single turn:
# Chat with live models in real time and watch AntiEcho sanitize context on the fly:
python chat.py
# Or via the CLI tool:
python -m antiecho.cli chatWhat it looks like in your terminal:
โญโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโฎ
โ Live Metric โ Before โ After (AntiEcho) โ Turn Impact โ
โโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโค
โ Estimated Tokens โ 1,199 โ 1,185 โ -14 (1.2%) โ
โ Characters โ 4,776 โ 4,719 โ -57 chars โ
โ Echoes & Boilerplate โ None โ Stripped cleanly โ 0 removed โ
โ Resolved Error Traces โ 1 detected โ Collapsed to 1-line โ 1 collapsed โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโฏ
๐ Active Pinned Directives: Language: Python 3.12 โข Database: SQLite โข No ORMs
# Run the built-in multi-turn simulation demo:
python -m antiecho.cli inspect
# Or inspect your own chat transcript JSON:
python -m antiecho.cli inspect my_chat.jsonfrom antiecho import AntiEcho
echo = AntiEcho(
strip_boilerplate=True, # Remove "Certainly! As you mentioned...", "I hope this helps!"
detect_repeated_phrases=True, # Detect cross-turn conversational loops
collapse_resolved_errors=True, # Compress stack traces once user confirms the fix
prune_superseded_code=True, # Replace outdated code implementations with pointers
pin_active_memory=True, # Extract and pin constraints to system prompt
max_recent_turns_raw=6 # Number of most recent turns to leave untouched
)
clean_messages = echo.clean(messages)| Feature | Microsoft LLMLingua | MemGPT / Letta | Raw LangChain Buffer | AntiEcho |
|---|---|---|---|---|
| Execution Latency | ~200ms โ 1,000ms | ~150ms | ~1ms | < 1.5ms |
| Dependencies | PyTorch + 500MB weights | Docker, Postgres, Vector DB | None | Zero (Pure Python) |
| Stops Echo Loops | โ | โ | โ | โ Yes |
| Collapses Resolved Bugs | โ | โ | โ | โ Yes |
| Superseded Code Pruning | โ | โ | โ | โ Yes |
| 1-Line SDK Wrapper | โ | โ | โ | โ Yes |
Contributions are warmly welcomed! Feel free to open an issue or pull request.
- Fork the repository
- Install test dependencies:
pip install pytest rich click - Run tests:
pytest
Distributed under the MIT License. See LICENSE for more information.