The Enhanced Context Counter is a sophisticated Function Filter for OpenWebUI that provides real-time monitoring and analytics for LLM interactions. It tracks token usage, estimates costs, monitors performance metrics, and provides actionable insights through a configurable status display. The system supports a wide range of LLMs through multi-source model detection and offers extensive customization options via Valves and UserValves.
Key Features
- Comprehensive Model Support: Multi-source model detection using OpenRouter API, exports, hardcoded defaults, and user-defined custom models in Valves
- Advanced Token Counting: Primary tiktoken-based counting with intelligent fallbacks, content-specific adjustments, and calibration factors.
- Cost Estimation & Budgeting: Precise cost calculation with input/output breakdown and multi-level budget tracking (daily, monthly, session).
- Performance Analytics: Real-time token rate calculation, adaptive window sizing, and comprehensive session statistics.
- Intelligent Context Management: Context window monitoring with progress visualization, warnings, and smart trimming suggestions.
- Persistent Cost Tracking: File-based tracking (cross-chat) with thread-safe operations for user, daily, and monthly costs.
- Highly Configurable UI: Customizable status line with modular components and visual indicators.
Other Features
- Image Token Estimation: Heuristic-based calculation using defaults, resolution analysis, and model-specific overrides.
- Calibration Integration: Status display based on external calibration results for accuracy verification.
- Error Resilience: Graceful fallbacks for missing dependencies, API failures, and unrecognized models.
- Content-Type Detection: Specialized handling for different content types (code, JSON, tables, etc.).
- Cache Optimization: Token counting cache with adaptive pruning for performance enhancement.
- Cost Optimization Hints: Actionable suggestions for reducing costs based on usage patterns.
- Extensive Logging: Configurable logging with rotation for diagnostics and troubleshooting.
Valve Configuration Guide
The function offers extensive customization through Valves (global settings) and UserValves (per-user overrides):
Core Valves
- [Model Detection]: Configure model recognition with
fuzzy_match_threshold, vendor_family_map, and heuristic_rules.
- [Token Counting]: Adjust accuracy with
model_correction_factors and content_correction_factors.
- [Cost/Budget]: Set
budget_amount, monthly_budget_amount, and budget_tracking_mode for financial controls.
- [UI/UX]: Customize display with toggles like
show_progress_bar, show_cost, and progress_bar_style.
- [Performance]: Fine-tune with
adaptive_rate_averaging and related window settings.
- [Cache]: Optimize with
enable_token_cache and token_cache_size.
- [Warnings]: Configure alerts with percentage thresholds for context and budget usage.
UserValves
Users can override global settings with personal preferences:
- Custom budget amounts and warning thresholds
- Model aliases for simplified model references
- Personal correction factors for token counting accuracy
- Visual style preferences for the status display
UI Status Line Breakdown
The status line provides a comprehensive overview of the current session's metrics in a compact format:
๐ช 48/1.0M tokens (0.00%) [โฑโฑโฑโฑโฑ] | ๐ฝ5/๐ผ43 | ๐ฐ $0.000000 | ๐ฆ Daily: $0.009221/$100.00 (0.0%) | โฑ๏ธ 5.1s (8.4 t/s) | ๐๏ธ $99.99 left (0.01%) this month | Text: 48 | ๐ง Not Calibrated
Status Components
- ๐ช 48/1.0M tokens (0.00%): Total tokens used / context window size with percentage
- [โฑโฑโฑโฑโฑ]: Visual progress bar showing context window usage
- ๐ฝ5/๐ผ43: Input/Output token breakdown (5 input, 43 output)
- ๐ฐ $0.000000: Total estimated cost for the current session
- ๐ฆ Daily: $0.009221/$100.00 (0.0%): Daily budget usage (spent/total and percentage)
- โฑ๏ธ 5.1s (8.4 t/s): Elapsed time and tokens per second rate
- ๐๏ธ $99.99 left (0.01%) this month: Monthly budget status (remaining amount and percentage used)
- Text: 48: Text token count (excludes image tokens if present)
- ๐ง Not Calibrated: Calibration status of token counting accuracy
Display Modes
The status line adapts to different levels of detail based on configuration:
Minimal: Shows only essential information (tokens, context percentage)
๐ช 48/1.0M tokens (0.00%)
Standard: Includes core metrics (default mode)
๐ช 48/1.0M tokens (0.00%) [โฑโฑโฑโฑโฑ] | ๐ฝ5/๐ผ43 | ๐ฐ $0.000000 | โฑ๏ธ 5.1s (8.4 t/s)
Detailed: Displays all available metrics including budgets, token breakdowns, and calibration status
๐ช 48/1.0M tokens (0.00%) [โฑโฑโฑโฑโฑ] | ๐ฝ5/๐ผ43 | ๐ฐ $0.000000 | ๐ฆ Daily: $0.009221/$100.00 (0.0%) | โฑ๏ธ 5.1s (8.4 t/s) | ๐๏ธ $99.99 left (0.01%) this month | Text: 48 | ๐ง Not Calibrated
The display automatically adjusts based on available space and configured preferences in the Valves settings.
Roadmap
- Enhanced model family detection with ML-based classification
- Advanced content-specific token counting with specialized encoders
- Interactive UI components for real-time adjustments and analytics
- Predictive budget forecasting based on usage patterns
- Cross-session analytics with visualization and reporting
- API for external integration with monitoring and alerting systems