Enhanced Context Tracker v4
functionA comprehensive context window tracker and metrics dashboard for OpenWebUI that provides real-time feedback on token usage, cost tracking, and performance metrics for all major LLM models.
@alexgrama7 v4.0
@alexgrama7 v4.0 325 class Valves(BaseModel):326 """Configuration valves for the Enhanced Context Counter function."""327 328 # VALIDATION ERROR FIX: The structured `custom_models` list field was removed due to persistent329 # Pydantic validation errors when loading from saved config. The issue occurred because330 # the nested structure with custom model objects couldn't be properly deserialized from331 # the saved JSON configuration, causing validation errors on startup.332 #333 # SOLUTION: The `custom_models_plaintext` field below provides a more robust alternative334 # that uses simple string parsing instead of complex nested objects. This approach is335 # more resilient to serialization/deserialization issues and allows for easier user input.336 # The corresponding commented-out code for the structured approach has been removed for clarity. 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, andheuristic_rules. - [Token Counting]: Adjust accuracy with
model_correction_factorsandcontent_correction_factors. - [Cost/Budget]: Set
budget_amount,monthly_budget_amount, andbudget_tracking_modefor financial controls. - [UI/UX]: Customize display with toggles like
show_progress_bar,show_cost, andprogress_bar_style. - [Performance]: Fine-tune with
adaptive_rate_averagingand related window settings. - [Cache]: Optimize with
enable_token_cacheandtoken_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

