context-optimizerAdvanced context management with auto-compaction and dynamic context optimization for DeepSeek's 64k context window. Features intelligent compaction (merging, summarizing, extracting), query-aware relevance scoring, and hierarchical memory system with context archive. Logs optimization events to chat.
Install via ClawdBot CLI:
clawdbot install ad2546/context-optimizerAdvanced context management optimized for DeepSeek's 64k context window. Provides intelligent pruning, compression, and token optimization to prevent context overflow while preserving important information.
import { createContextPruner } from './lib/index.js';
const pruner = createContextPruner({
contextLimit: 64000, // DeepSeek's limit
autoCompact: true, // Enable automatic compaction
dynamicContext: true, // Enable dynamic relevance-based context
strategies: ['semantic', 'temporal', 'extractive', 'adaptive'],
queryAwareCompaction: true, // Compact based on current query relevance
});
await pruner.initialize();
// Process messages with auto-compaction and dynamic context
const processed = await pruner.processMessages(messages, currentQuery);
// Get context health status
const status = pruner.getStatus();
console.log(`Context health: ${status.health}, Relevance scores: ${status.relevanceScores}`);
// Manual compaction when needed
const compacted = await pruner.autoCompact(messages, currentQuery);
// When something isn't in current context, search archive
const archiveResult = await pruner.retrieveFromArchive('query about previous conversation', {
maxContextTokens: 1000,
minRelevance: 0.4,
});
if (archiveResult.found) {
// Add relevant snippets to current context
const archiveContext = archiveResult.snippets.join('\n\n');
// Use archiveContext in your prompt
console.log(`Found ${archiveResult.sources.length} relevant sources`);
console.log(`Retrieved ${archiveResult.totalTokens} tokens from archive`);
}
The context archive provides a RAM vs Storage approach:
{
contextLimit: 64000, // DeepSeek's context window
autoCompact: true, // Enable automatic compaction
compactThreshold: 0.75, // Start compacting at 75% usage
aggressiveCompactThreshold: 0.9, // Aggressive compaction at 90%
dynamicContext: true, // Enable dynamic context management
relevanceDecay: 0.95, // Relevance decays 5% per time step
minRelevanceScore: 0.3, // Minimum relevance to keep
queryAwareCompaction: true, // Compact based on current query relevance
strategies: ['semantic', 'temporal', 'extractive', 'adaptive'],
preserveRecent: 10, // Always keep last N messages
preserveSystem: true, // Always keep system messages
minSimilarity: 0.85, // Semantic similarity threshold
// Archive settings
enableArchive: true, // Enable hierarchical memory system
archivePath: './context-archive',
archiveSearchLimit: 10,
archiveMaxSize: 100 * 1024 * 1024, // 100MB
archiveIndexing: true,
// Chat logging
logToChat: true, // Log optimization events to chat
chatLogLevel: 'brief', // 'brief', 'detailed', or 'none'
chatLogFormat: '📊 {action}: {details}', // Format for chat messages
// Performance
batchSize: 5, // Messages to process in batch
maxCompactionRatio: 0.5, // Maximum 50% compaction in one pass
}
The context optimizer can log events directly to chat:
// Example chat log messages:
// 📊 Context optimized: Compacted 15 messages → 8 (47% reduction)
// 📊 Archive search: Found 3 relevant snippets (42% similarity)
// 📊 Dynamic context: Filtered 12 low-relevance messages
// Configure logging:
const pruner = createContextPruner({
logToChat: true,
chatLogLevel: 'brief', // Options: 'brief', 'detailed', 'none'
chatLogFormat: '📊 {action}: {details}',
// Custom log handler (optional)
onLog: (level, message, data) => {
if (level === 'info' && data.action === 'compaction') {
// Send to chat
console.log(`🧠Context optimized: ${message}`);
}
}
});
Add to your Clawdbot config:
skills:
context-pruner:
enabled: true
config:
contextLimit: 64000
autoPrune: true
The pruner will automatically monitor context usage and apply appropriate pruning strategies to stay within DeepSeek's 64k limit.
Generated Mar 1, 2026
Assists writers and content creators in managing extensive research and drafts within AI context limits. It compacts background information and references, allowing focus on current writing tasks without losing key details.
Enables AI chatbots to handle long conversation histories with customers by summarizing past interactions and retrieving relevant archived details. This maintains context for personalized support while avoiding token overflow.
Helps legal professionals analyze lengthy contracts or case files by compacting redundant sections and extracting key clauses. The archive system allows quick retrieval of precedents without overwhelming the AI's context window.
Supports researchers in synthesizing large volumes of papers and notes by merging similar findings and summarizing older studies. Dynamic context ensures relevance to current research queries, enhancing literature review efficiency.
Facilitates AI-driven project tracking by compacting historical updates and meeting notes, while preserving priority tasks. Archive retrieval helps recall past decisions or dependencies when planning new phases.
Offer the context optimizer as a cloud-based API service with tiered pricing based on usage volume, such as tokens processed or archive storage. This model targets developers and enterprises needing scalable context management for AI applications.
Sell customized on-premise licenses to large organizations requiring data privacy and integration with existing AI systems. This includes premium support, training, and tailored configuration for specific industry needs.
Provide a free open-source version with basic compaction features to attract individual users and small teams. Monetize through paid upgrades for advanced capabilities like hierarchical memory, detailed analytics, and priority support.
💬 Integration Tip
Start by enabling auto-compaction with default settings to handle token limits, then gradually customize strategies like query-aware relevance based on your specific use case for optimal performance.
Use CodexBar CLI local cost usage to summarize per-model usage for Codex or Claude, including the current (most recent) model or a full model breakdown. Trigger when asked for model-level usage/cost data from codexbar, or when you need a scriptable per-model summary from codexbar cost JSON.
Gemini CLI for one-shot Q&A, summaries, and generation.
Research any topic from the last 30 days on Reddit + X + Web, synthesize findings, and write copy-paste-ready prompts. Use when the user wants recent social/web research on a topic, asks "what are people saying about X", or wants to learn current best practices. Requires OPENAI_API_KEY and/or XAI_API_KEY for full Reddit+X access, falls back to web search.
Check Antigravity account quotas for Claude and Gemini models. Shows remaining quota and reset times with ban detection.
Manages free AI models from OpenRouter for OpenClaw. Automatically ranks models by quality, configures fallbacks for rate-limit handling, and updates opencla...
Manages free AI models from OpenRouter for OpenClaw. Automatically ranks models by quality, configures fallbacks for rate-limit handling, and updates openclaw.json. Use when the user mentions free AI, OpenRouter, model switching, rate limits, or wants to reduce AI costs.