Monitor Shannon entropy in LLM token distributions to detect sudden uncertainty spikes, then trigger adaptive prompt consolidation to realign conversation context and improve accuracy by 56.6% and reliability by 35.3%.
Installer avec Codex ou Claude Copiez ce prompt, collez-le dans Codex, Claude ou un autre assistant, puis laissez-le vérifier la page du skill et l'installer pour vous.
Une commande directe contourne le prompt de vérification. Examinez la source avant de l'exécuter.
Monitor Shannon entropy in LLM token distributions to detect sudden uncertainty spikes, then trigger adaptive prompt consolidation to realign conversation context and improve accuracy by 56.6% and reliability by 35.3%.
Multi-turn conversations with LLMs degrade over time as context accumulates and the model loses track of earlier instructions or goals. Rather than treating uncertainty as noise to eliminate, ERGO uses entropy—the model's own uncertainty signal—to detect when the conversation has drifted, then triggers guided context consolidation.
The key insight is that sharp entropy spikes indicate critical moments: when the model suddenly becomes uncertain about what to do next, that's a signal that instructions have become misaligned or context has become incoherent. By monitoring per-token entropy and consolidating context when entropy spikes, ERGO prevents compounding errors while maintaining conversation flow.
Core Concept
ERGO operates on three principles:
Entropy Monitoring: Track Shannon entropy of next-token probability distributions at each step
Spike Detection: Identify sudden increases in entropy (threshold-based or learned)
Prompt Consolidation: When entropy spikes, synthesize conversation state and reinject clear instructions
Adaptive Thresholds: Learn task-specific entropy baselines during warm-up phase
The method improves multi-turn reasoning tasks where instructions are revealed incrementally by detecting when the model has lost context and automatically recovering.
Architecture Overview
Token-Level Entropy Computation: Calculate H(P) for each generated token
Entropy Baseline Tracker: Maintain running exponential moving average of baseline entropy
Spike Detector: Compare current entropy to baseline; flag when difference exceeds threshold
Context Compressor: Summarize conversation history to essential information
Instruction Injector: Synthesize high-level task state and goals
Prompt Rewriter: Insert consolidated context + original instructions back into conversation
Implementation Steps
The core algorithm computes entropy per token and triggers consolidation when entropy spikes. This example shows entropy monitoring and context reset.
"""
Monitor entropy in multi-turn conversations and trigger consolidation
when the model becomes suddenly uncertain.
"""
def
__init__
self,
entropy_window_size=20,
baseline_alpha=0.9,
spike_threshold=0.5, # Increase in entropy (nats)
consolidation_interval=10# Max turns before forced consolidation
self
self
None
self
# EMA decay rate
self
self
self
0
def
compute_entropy
self, token_logits
"""
Compute Shannon entropy over token distribution.
Args:
token_logits: shape (vocab_size,) or (batch, vocab_size)
Returns:
entropy: scalar or (batch,) in nats
"""
1
sum
1e-10
1
return
def
should_consolidate
self, token_entropy, conversation_length
"""
Detect if context should be consolidated based on entropy spike
or forced consolidation interval.
"""
"""
Compress conversation to essential information and reinject task context.
Args:
conversation_history: list of (role, text) tuples
original_task_instruction: original user instruction
summarizer_model: LLM used to summarize
Returns:
consolidated_prompt: string with compressed context
"""
# Summarize recent conversation
f"""
Summarize the following conversation in 2-3 sentences, focusing on:
1. What goal is the user trying to achieve?
2. What progress has been made?
3. What is the next step?
Conversation:
{format_conversation(conversation_history[-10:])} # Last 10 turns
Summary:
"""
100
# Reconstruct high-level state
f"""
Based on this conversation summary and original task, what is the current state?
Original Task: {original_task_instruction}
Summary: {summary}
Current State (2-3 lines):
"""
80
# Inject back into conversation
f"""
<CONTEXT_CONSOLIDATION>
Task: {original_task_instruction}
Progress Summary: {summary}
Current State: {current_state}
</CONTEXT_CONSOLIDATION>
Please continue with the task, keeping the above context in mind.
"""
Entropy thresholds should be calibrated on task-specific validation data. Simple tasks (factual QA) have lower baseline entropy; complex reasoning tasks higher. Start with 0.5 nats and adjust based on false positive rate.
Practical Guidance
Task Type
Baseline Entropy
Spike Threshold
Consolidation Interval
Factual QA
1.5-2.0 nats
0.4
15 turns
Multi-step reasoning
2.5-3.5 nats
0.6
10 turns
Open-ended dialogue
3.0-4.0 nats
0.8
12 turns
When to Use:
Multi-turn conversations with incremental instructions
Tasks where model context degrades over time (long conversations)
You need to detect model confusion without explicit verification
Task has clear entropy baseline you can establish
When NOT to Use:
Single-turn generation (no multi-turn context drift)
Tasks where entropy naturally spikes (open-ended generation)