Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Add token usage tracking and per-user budget enforcement
Murtaza Nasir3d08d7889116
diff --git a/src/api/admin.py b/src/api/admin.pyindex 34dfa940..5b05d1df 100644--- a/src/api/admin.py+++ b/src/api/admin.py@@ -17,6 +17,7 @@ from src.models import *from src.utils import *from src.services.retention import is_recording_exempt_from_deletion, get_retention_days_for_recording, process_auto_deletionfrom src.services.embeddings import EMBEDDINGS_AVAILABLE, process_recording_chunks+from src.services.token_tracking import token_trackerfrom src.config.startup import get_file_monitor_functions# Create blueprint@@ -130,7 +131,11 @@ def admin_get_users():# Get recordings count and storage usedrecordings_count = len(user.recordings)storage_used = sum(r.file_size for r in user.recordings if r.file_size) or 0-++ # Get current month token usage+ current_usage = token_tracker.get_monthly_usage(user.id)+ usage_percentage = (current_usage / user.monthly_token_budget * 100) if user.monthly_token_budget else 0+user_data.append({'id': user.id,'username': user.username,@@ -138,7 +143,10 @@ def admin_get_users():'is_admin': user.is_admin,'can_share_publicly': user.can_share_publicly,'recordings_count': recordings_count,- 'storage_used': storage_used+ 'storage_used': storage_used,+ 'monthly_token_budget': user.monthly_token_budget,+ 'current_token_usage': current_usage,+ 'token_usage_percentage': round(usage_percentage, 1)})return jsonify(user_data)@@ -175,19 +183,23 @@ def admin_add_user():username=data['username'],email=data['email'],password=hashed_password,- is_admin=data.get('is_admin', False)+ is_admin=data.get('is_admin', False),+ monthly_token_budget=data.get('monthly_token_budget'))-+db.session.add(new_user)db.session.commit()-+return jsonify({'id': new_user.id,'username': new_user.username,'email': new_user.email,'is_admin': new_user.is_admin,'recordings_count': 0,- 'storage_used': 0+ 'storage_used': 0,+ 'monthly_token_budget': new_user.monthly_token_budget,+ 'current_token_usage': 0,+ 'token_usage_percentage': 0}), 201@@ -229,12 +241,24 @@ def admin_update_user(user_id):if 'can_share_publicly' in data:user.can_share_publicly = data['can_share_publicly']+ if 'monthly_token_budget' in data:+ # Allow setting to None (unlimited) or a positive integer+ budget = data['monthly_token_budget']+ if budget is None or budget == '' or budget == 0:+ user.monthly_token_budget = None+ else:+ user.monthly_token_budget = int(budget)+db.session.commit()# Get recordings count and storage usedrecordings_count = len(user.recordings)storage_used = sum(r.file_size for r in user.recordings if r.file_size) or 0+ # Get current month token usage+ current_usage = token_tracker.get_monthly_usage(user.id)+ usage_percentage = (current_usage / user.monthly_token_budget * 100) if user.monthly_token_budget else 0+return jsonify({'id': user.id,'username': user.username,@@ -242,7 +266,10 @@ def admin_update_user(user_id):'is_admin': user.is_admin,'can_share_publicly': user.can_share_publicly,'recordings_count': recordings_count,- 'storage_used': storage_used+ 'storage_used': storage_used,+ 'monthly_token_budget': user.monthly_token_budget,+ 'current_token_usage': current_usage,+ 'token_usage_percentage': round(usage_percentage, 1)})@@ -368,6 +395,111 @@ def admin_get_stats():'total_queries': total_queries})++# --- Token Usage Stats ---++@admin_bp.route('/admin/token-stats', methods=['GET'])+@login_required+def admin_get_token_stats():+ """Get overall token usage statistics."""+ if not current_user.is_admin:+ return jsonify({'error': 'Unauthorized'}), 403++ try:+ # Get today's usage+ today_usage = token_tracker.get_today_usage()++ # Get current month usage for all users+ monthly_stats = token_tracker.get_monthly_stats(months=1)+ current_month = monthly_stats[-1] if monthly_stats else {'tokens': 0, 'cost': 0}++ # Get per-user stats for current month+ user_stats = token_tracker.get_user_stats()++ # Calculate totals+ total_monthly_tokens = current_month.get('tokens', 0)+ total_monthly_cost = current_month.get('cost', 0)++ return jsonify({+ 'today': today_usage,+ 'current_month': {+ 'tokens': total_monthly_tokens,+ 'cost': total_monthly_cost+ },+ 'user_count_with_usage': len([u for u in user_stats if u['current_usage'] > 0]),+ 'users_over_80_percent': len([u for u in user_stats if u['percentage'] >= 80]),+ 'users_at_100_percent': len([u for u in user_stats if u['percentage'] >= 100])+ })++ except Exception as e:+ current_app.logger.error(f"Error getting token stats: {e}")+ return jsonify({'error': str(e)}), 500+++@admin_bp.route('/admin/token-stats/daily', methods=['GET'])+@login_required+def admin_get_daily_token_stats():+ """Get daily token usage for charts (last 30 days)."""+ if not current_user.is_admin:+ return jsonify({'error': 'Unauthorized'}), 403++ try:+ days = request.args.get('days', 30, type=int)+ user_id = request.args.get('user_id', type=int)++ daily_stats = token_tracker.get_daily_stats(days=days, user_id=user_id)++ return jsonify({+ 'stats': daily_stats,+ 'days': days+ })++ except Exception as e:+ current_app.logger.error(f"Error getting daily token stats: {e}")+ return jsonify({'error': str(e)}), 500+++@admin_bp.route('/admin/token-stats/monthly', methods=['GET'])+@login_required+def admin_get_monthly_token_stats():+ """Get monthly token usage for charts (last 12 months)."""+ if not current_user.is_admin:+ return jsonify({'error': 'Unauthorized'}), 403++ try:+ months = request.args.get('months', 12, type=int)++ monthly_stats = token_tracker.get_monthly_stats(months=months)++ return jsonify({+ 'stats': monthly_stats,+ 'months': months+ })++ except Exception as e:+ current_app.logger.error(f"Error getting monthly token stats: {e}")+ return jsonify({'error': str(e)}), 500+++@admin_bp.route('/admin/token-stats/users', methods=['GET'])+@login_required+def admin_get_user_token_stats():+ """Get per-user token usage for current month."""+ if not current_user.is_admin:+ return jsonify({'error': 'Unauthorized'}), 403++ try:+ user_stats = token_tracker.get_user_stats()++ return jsonify({+ 'users': user_stats+ })++ except Exception as e:+ current_app.logger.error(f"Error getting user token stats: {e}")+ return jsonify({'error': str(e)}), 500++# --- Transcript Template Routes ---diff --git a/src/api/inquire.py b/src/api/inquire.pyindex 643f0a25..9c1b20aa 100644--- a/src/api/inquire.py+++ b/src/api/inquire.py@@ -17,7 +17,7 @@ from src.database import dbfrom src.models import *from src.utils import *from src.services.embeddings import get_accessible_recording_ids, semantic_search_chunks-from src.services.llm import call_llm_completion, call_chat_completion, process_streaming_with_thinking, client, chat_client+from src.services.llm import call_llm_completion, call_chat_completion, process_streaming_with_thinking, client, chat_client, TokenBudgetExceeded# Create blueprintinquire_bp = Blueprint('inquire', __name__)@@ -248,7 +248,9 @@ Examples:{"role": "user", "content": router_prompt}],temperature=0.1,- max_tokens=10+ max_tokens=10,+ user_id=user_id,+ operation_type='query_routing')raw_decision = router_response.choices[0].message.content@@ -279,11 +281,13 @@ Use proper markdown formatting including headings (##), bold (**text**), bullet],temperature=0.7,max_tokens=int(os.environ.get("CHAT_MAX_TOKENS", "2000")),- stream=True+ stream=True,+ user_id=user_id,+ operation_type='chat')# Use helper function to process streaming with thinking tag support- for response in process_streaming_with_thinking(stream):+ for response in process_streaming_with_thinking(stream, user_id=user_id, operation_type='chat', model_name=os.environ.get('LLM_MODEL')):yield responsereturn@@ -325,7 +329,9 @@ Respond with only a JSON array of strings: ["term1", "term2", "term3", ...]"""{"role": "user", "content": enrichment_prompt}],temperature=0.3,- max_tokens=200+ max_tokens=200,+ user_id=user_id,+ operation_type='query_enrichment')raw_content = enrichment_response.choices[0].message.content@@ -635,7 +641,9 @@ Order your response with notes from the most recent meetings first. Always use pmessages=messages,temperature=0.7,max_tokens=int(os.environ.get("CHAT_MAX_TOKENS", "2000")),- stream=True+ stream=True,+ user_id=user_id,+ operation_type='chat')# Buffer content to detect full transcript requests@@ -697,11 +705,13 @@ Order your response with notes from the most recent meetings first. Always use pmessages=updated_messages,temperature=0.7,max_tokens=int(os.environ.get("CHAT_MAX_TOKENS", "2000")),- stream=True+ stream=True,+ user_id=user_id,+ operation_type='chat')-+# Use helper function to process streaming with thinking tag support- for response in process_streaming_with_thinking(new_stream):+ for response in process_streaming_with_thinking(new_stream, user_id=user_id, operation_type='chat', model_name=os.environ.get('CHAT_MODEL')):yield responsereturnelse:@@ -768,6 +778,9 @@ Order your response with notes from the most recent meetings first. Always use pyield f"data: {json.dumps({'end_of_stream': True})}\n\n"+ except TokenBudgetExceeded as e:+ app.logger.warning(f"Token budget exceeded for user {user_id}: {e}")+ yield f"data: {json.dumps({'error': str(e), 'budget_exceeded': True})}\n\n"except Exception as e:app.logger.error(f"Error in enhanced chat generation: {e}")yield f"data: {json.dumps({'error': str(e)})}\n\n"diff --git a/src/api/recordings.py b/src/api/recordings.pyindex 32702c07..9923bdc4 100644--- a/src/api/recordings.py+++ b/src/api/recordings.py@@ -29,7 +29,7 @@ from src.services.speaker import update_speaker_usage, identify_unidentified_spefrom src.services.speaker_embedding_matcher import update_speaker_embeddingfrom src.services.speaker_snippets import create_speaker_snippetsfrom src.services.document import process_markdown_to_docx-from src.services.llm import client, chat_client, call_llm_completion, call_chat_completion, process_streaming_with_thinking+from src.services.llm import client, chat_client, call_llm_completion, call_chat_completion, process_streaming_with_thinking, TokenBudgetExceededfrom src.services.embeddings import process_recording_chunksfrom src.file_exporter import export_recording, mark_export_as_deletedfrom src.utils.ffprobe import get_codec_info, get_creation_date, FFProbeError@@ -982,7 +982,9 @@ JSON Response:{"role": "user", "content": prompt}],temperature=0.2,- response_format={"type": "json_object"}+ response_format={"type": "json_object"},+ user_id=current_user.id,+ operation_type='speaker_identification')response_content = completion.choices[0].message.contentcurrent_app.logger.info(f"[Auto-Identify] LLM Raw Response: {response_content}")@@ -2773,22 +2775,31 @@ Additional context and notes about the meeting:messages.extend(message_history)messages.append({"role": "user", "content": user_message})+ # Capture context before generator starts (app context may not be available inside generator)+ user_id = current_user.id+ app = current_app._get_current_object()+def generate():try:- # Enable streaming+ # Enable streaming with user_id for budget enforcementstream = call_chat_completion(messages=messages,temperature=0.7,max_tokens=int(os.environ.get("CHAT_MAX_TOKENS", "2000")),- stream=True+ stream=True,+ user_id=user_id,+ operation_type='chat')# Use helper function to process streaming with thinking tag support- for response in process_streaming_with_thinking(stream):+ for response in process_streaming_with_thinking(stream, user_id=user_id, operation_type='chat', app=app):yield response+ except TokenBudgetExceeded as e:+ app.logger.warning(f"Token budget exceeded for user {user_id}: {e}")+ yield f"data: {json.dumps({'error': str(e), 'budget_exceeded': True})}\n\n"except Exception as e:- current_app.logger.error(f"Error during chat stream generation: {str(e)}")+ app.logger.error(f"Error during chat stream generation: {str(e)}")# Yield an error message in SSE formatyield f"data: {json.dumps({'error': str(e)})}\n\n"diff --git a/src/api/system.py b/src/api/system.pyindex e1db2ea0..9bd537d6 100644--- a/src/api/system.py+++ b/src/api/system.py@@ -17,6 +17,7 @@ from src.utils import *from src.config.version import get_versionfrom src.services.llm import TEXT_MODEL_BASE_URL, TEXT_MODEL_NAMEfrom src.config.app_config import ASR_BASE_URL+from src.services.token_tracking import token_tracker# Create blueprintsystem_bp = Blueprint('system', __name__)@@ -79,6 +80,38 @@ def save_user_preferences():'ui_language': current_user.ui_language})++@system_bp.route('/api/user/token-budget', methods=['GET'])+@login_required+def get_user_token_budget():+ """Get current user's token budget status."""+ try:+ user = current_user++ # If user has no budget, return null to indicate unlimited+ if not user.monthly_token_budget:+ return jsonify({+ 'has_budget': False,+ 'budget': None,+ 'usage': 0,+ 'percentage': 0+ })++ # Get current usage+ current_usage = token_tracker.get_monthly_usage(user.id)+ percentage = (current_usage / user.monthly_token_budget) * 100++ return jsonify({+ 'has_budget': True,+ 'budget': user.monthly_token_budget,+ 'usage': current_usage,+ 'percentage': round(percentage, 1)+ })+ except Exception as e:+ current_app.logger.error(f"Error getting token budget for user {current_user.id}: {e}")+ return jsonify({'error': str(e)}), 500++# --- System Info API Endpoint ---diff --git a/src/init_db.py b/src/init_db.pyindex 9d5c42bb..34293989 100644--- a/src/init_db.py+++ b/src/init_db.py@@ -230,6 +230,10 @@ def initialize_database(app):if add_column_if_not_exists(engine, 'user', 'can_share_publicly', 'BOOLEAN DEFAULT 1'):app.logger.info("Added can_share_publicly column to user table")+ # Token budget for rate limiting+ if add_column_if_not_exists(engine, 'user', 'monthly_token_budget', 'INTEGER'):+ app.logger.info("Added monthly_token_budget column to user table")+# Add source tracking columns to internal_share tableif add_column_if_not_exists(engine, 'internal_share', 'source_type', 'VARCHAR(20) DEFAULT "manual"'):app.logger.info("Added source_type column to internal_share table")diff --git a/src/models/__init__.py b/src/models/__init__.pyindex 7312d268..3149db09 100644--- a/src/models/__init__.py+++ b/src/models/__init__.py@@ -27,6 +27,7 @@ from .system import SystemSettingfrom .audit import ShareAuditLogfrom .push_subscription import PushSubscriptionfrom .processing_job import ProcessingJob+from .token_usage import TokenUsage# Export all models__all__ = [@@ -57,4 +58,5 @@ __all__ = ['SystemSetting','PushSubscription','ProcessingJob',+ 'TokenUsage',]diff --git a/src/models/token_usage.py b/src/models/token_usage.pynew file mode 100644index 00000000..0868fff9--- /dev/null+++ b/src/models/token_usage.py@@ -0,0 +1,44 @@+"""+Token usage tracking model for monitoring LLM API consumption.+"""++from datetime import datetime, date+from src.database import db+++class TokenUsage(db.Model):+ """Daily token usage aggregates per user per operation type."""+ __tablename__ = 'token_usage'++ id = db.Column(db.Integer, primary_key=True)+ user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)+ date = db.Column(db.Date, nullable=False, default=date.today)+ operation_type = db.Column(db.String(50), nullable=False)++ # Token counts (from API response.usage)+ prompt_tokens = db.Column(db.Integer, default=0)+ completion_tokens = db.Column(db.Integer, default=0)+ total_tokens = db.Column(db.Integer, default=0)++ # Cost tracking (OpenRouter provides this)+ cost = db.Column(db.Float, default=0.0)++ # Request count for this day/operation+ request_count = db.Column(db.Integer, default=0)++ # Model info+ model_name = db.Column(db.String(100), nullable=True)++ created_at = db.Column(db.DateTime, default=datetime.utcnow)+ updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)++ # Relationships+ user = db.relationship('User', backref=db.backref('token_usage', lazy='dynamic'))++ __table_args__ = (+ db.UniqueConstraint('user_id', 'date', 'operation_type', name='uq_user_date_op'),+ db.Index('idx_token_user_date', 'user_id', 'date'),+ )++ def __repr__(self):+ return f'<TokenUsage {self.user_id} {self.date} {self.operation_type}: {self.total_tokens}>'diff --git a/src/models/user.py b/src/models/user.pyindex 7ac35519..fc05ebf3 100644--- a/src/models/user.py+++ b/src/models/user.py@@ -32,6 +32,9 @@ class User(db.Model, UserMixin):company = db.Column(db.String(100), nullable=True)diarize = db.Column(db.Boolean, default=False)+ # Token budget (None = unlimited)+ monthly_token_budget = db.Column(db.Integer, nullable=True)+def __repr__(self):return f"User('{self.username}', '{self.email}')"diff --git a/src/services/llm.py b/src/services/llm.pyindex d654c08c..e473b38c 100644--- a/src/services/llm.py+++ b/src/services/llm.py@@ -12,6 +12,14 @@ from openai import OpenAI# Use standard logging instead of current_app.logger for context independencelogger = logging.getLogger(__name__)++class TokenBudgetExceeded(Exception):+ """Raised when user exceeds their token budget."""+ def __init__(self, message, usage_percentage=100):+ self.message = message+ self.usage_percentage = usage_percentage+ super().__init__(message)+from src.utils import safe_json_loads, extract_json_object# Configuration - use TEXT_MODEL_* variables for LLM@@ -128,7 +136,8 @@ def is_using_openai_api():-def call_llm_completion(messages, temperature=0.7, response_format=None, stream=False, max_tokens=None):+def call_llm_completion(messages, temperature=0.7, response_format=None, stream=False, max_tokens=None,+ user_id=None, operation_type=None):"""Centralized function for LLM API calls with proper error handling and logging.@@ -138,6 +147,8 @@ def call_llm_completion(messages, temperature=0.7, response_format=None, stream=response_format: Optional response format dict (e.g., {"type": "json_object"})stream: Whether to stream the responsemax_tokens: Optional maximum tokens to generate+ user_id: Optional user ID for token tracking and budget enforcement+ operation_type: Optional operation type for token tracking (e.g., 'summarization', 'chat')Returns:OpenAI completion object or generator (if streaming)@@ -148,6 +159,21 @@ def call_llm_completion(messages, temperature=0.7, response_format=None, stream=if not TEXT_MODEL_API_KEY:raise ValueError("TEXT_MODEL_API_KEY not configured")+ # Check budget before making the call+ if user_id and operation_type:+ try:+ from src.services.token_tracking import token_tracker+ can_proceed, usage_pct, msg = token_tracker.check_budget(user_id)+ if not can_proceed:+ raise TokenBudgetExceeded(msg, usage_pct)+ if usage_pct >= 80:+ logger.warning(f"User {user_id} at {usage_pct:.1f}% of token budget")+ except TokenBudgetExceeded:+ raise+ except Exception as e:+ # Log but don't block on budget check errors+ logger.warning(f"Budget check failed for user {user_id}: {e}")+try:# Check if we're using GPT-5 with OpenAI APIusing_gpt5 = is_gpt5_model(TEXT_MODEL_NAME) and is_using_openai_api()@@ -158,6 +184,10 @@ def call_llm_completion(messages, temperature=0.7, response_format=None, stream="stream": stream}+ # Add stream_options to get usage in final chunk for streaming+ if stream:+ completion_args["stream_options"] = {"include_usage": True}+if using_gpt5:# GPT-5 models don't support temperature, top_p, or logprobs# They use reasoning_effort and verbosity instead@@ -186,6 +216,22 @@ def call_llm_completion(messages, temperature=0.7, response_format=None, stream=response = client.chat.completions.create(**completion_args)+ # Track usage for non-streaming calls+ if user_id and operation_type and not stream and response.usage:+ try:+ from src.services.token_tracking import token_tracker+ token_tracker.record_usage(+ user_id=user_id,+ operation_type=operation_type,+ prompt_tokens=response.usage.prompt_tokens,+ completion_tokens=response.usage.completion_tokens,+ total_tokens=response.usage.total_tokens,+ model_name=TEXT_MODEL_NAME,+ cost=getattr(response.usage, 'cost', None)+ )+ except Exception as e:+ logger.warning(f"Failed to record token usage: {e}")+# Debug log for empty responsesif not stream and response.choices:content = response.choices[0].message.content@@ -199,12 +245,15 @@ def call_llm_completion(messages, temperature=0.7, response_format=None, stream=return response+ except TokenBudgetExceeded:+ raiseexcept Exception as e:logger.error(f"LLM API call failed: {e}")raise-def call_chat_completion(messages, temperature=0.7, response_format=None, stream=False, max_tokens=None):+def call_chat_completion(messages, temperature=0.7, response_format=None, stream=False, max_tokens=None,+ user_id=None, operation_type=None):"""Chat-specific LLM completion function. Uses dedicated chat model if configured,otherwise falls back to standard TEXT_MODEL configuration.@@ -215,6 +264,8 @@ def call_chat_completion(messages, temperature=0.7, response_format=None, streamresponse_format: Optional response format dict (e.g., {"type": "json_object"})stream: Whether to stream the responsemax_tokens: Optional maximum tokens to generate+ user_id: Optional user ID for token tracking and budget enforcement+ operation_type: Optional operation type for token tracking (e.g., 'chat')Returns:OpenAI completion object or generator (if streaming)@@ -228,6 +279,21 @@ def call_chat_completion(messages, temperature=0.7, response_format=None, streamif not chat_config['api_key']:raise ValueError("Chat model API key not configured")+ # Check budget before making the call+ if user_id and operation_type:+ try:+ from src.services.token_tracking import token_tracker+ can_proceed, usage_pct, msg = token_tracker.check_budget(user_id)+ if not can_proceed:+ raise TokenBudgetExceeded(msg, usage_pct)+ if usage_pct >= 80:+ logger.warning(f"User {user_id} at {usage_pct:.1f}% of token budget")+ except TokenBudgetExceeded:+ raise+ except Exception as e:+ # Log but don't block on budget check errors+ logger.warning(f"Budget check failed for user {user_id}: {e}")+try:model_name = chat_config['model_name']base_url = chat_config['base_url'] or ''@@ -241,6 +307,10 @@ def call_chat_completion(messages, temperature=0.7, response_format=None, stream"stream": stream}+ # Add stream_options to get usage in final chunk for streaming+ if stream:+ completion_args["stream_options"] = {"include_usage": True}+if using_gpt5:logger.debug(f"Using GPT-5 chat model: {model_name}")# Use chat-specific GPT-5 settings from config@@ -259,6 +329,22 @@ def call_chat_completion(messages, temperature=0.7, response_format=None, streamresponse = effective_client.chat.completions.create(**completion_args)+ # Track usage for non-streaming calls+ if user_id and operation_type and not stream and response.usage:+ try:+ from src.services.token_tracking import token_tracker+ token_tracker.record_usage(+ user_id=user_id,+ operation_type=operation_type,+ prompt_tokens=response.usage.prompt_tokens,+ completion_tokens=response.usage.completion_tokens,+ total_tokens=response.usage.total_tokens,+ model_name=model_name,+ cost=getattr(response.usage, 'cost', None)+ )+ except Exception as e:+ logger.warning(f"Failed to record token usage: {e}")+# Debug log for empty responsesif not stream and response.choices:content = response.choices[0].message.content@@ -267,6 +353,8 @@ def call_chat_completion(messages, temperature=0.7, response_format=None, streamreturn response+ except TokenBudgetExceeded:+ raiseexcept Exception as e:logger.error(f"Chat LLM API call failed: {e}")raise@@ -297,18 +385,55 @@ def format_api_error_message(error_str):return f"[Summary generation failed: {error_str}]"-def process_streaming_with_thinking(stream):+def process_streaming_with_thinking(stream, user_id=None, operation_type=None, model_name=None, app=None):"""Generator that processes a streaming response and separates thinking content.Yields SSE-formatted data with 'delta' for regular content and 'thinking' for thinking content.++ Args:+ stream: The streaming response from the LLM API+ user_id: Optional user ID for token tracking+ operation_type: Optional operation type for token tracking+ model_name: Optional model name for token tracking+ app: Optional Flask app instance for database context in generators"""content_buffer = ""in_thinking = Falsethinking_buffer = ""for chunk in stream:- content = chunk.choices[0].delta.content- if content:+ # Check for usage in final chunk (from stream_options={'include_usage': True})+ if hasattr(chunk, 'usage') and chunk.usage and user_id and operation_type:+ try:+ from src.services.token_tracking import token_tracker+ # Use app context if provided (needed for generators where context may be lost)+ if app:+ with app.app_context():+ token_tracker.record_usage(+ user_id=user_id,+ operation_type=operation_type,+ prompt_tokens=chunk.usage.prompt_tokens,+ completion_tokens=chunk.usage.completion_tokens,+ total_tokens=chunk.usage.total_tokens,+ model_name=model_name or TEXT_MODEL_NAME,+ cost=getattr(chunk.usage, 'cost', None)+ )+ else:+ token_tracker.record_usage(+ user_id=user_id,+ operation_type=operation_type,+ prompt_tokens=chunk.usage.prompt_tokens,+ completion_tokens=chunk.usage.completion_tokens,+ total_tokens=chunk.usage.total_tokens,+ model_name=model_name or TEXT_MODEL_NAME,+ cost=getattr(chunk.usage, 'cost', None)+ )+ except Exception as e:+ logger.warning(f"Failed to record streaming token usage: {e}")++ # Process content delta+ if chunk.choices and chunk.choices[0].delta.content:+ content = chunk.choices[0].delta.contentcontent_buffer += content# Process the buffer to detect and handle thinking tagsdiff --git a/src/services/token_tracking.py b/src/services/token_tracking.pynew file mode 100644index 00000000..d05390da--- /dev/null+++ b/src/services/token_tracking.py@@ -0,0 +1,270 @@+"""+Token usage tracking service for monitoring LLM API consumption and budget enforcement.+"""++import logging+from datetime import date, datetime, timedelta+from typing import Tuple, Optional, Dict, List++from sqlalchemy import func, extract++from src.database import db+from src.models.token_usage import TokenUsage+from src.models.user import User++logger = logging.getLogger(__name__)+++class TokenTracker:+ """Service for recording and checking token usage."""++ OPERATION_TYPES = [+ 'summarization',+ 'chat',+ 'title_generation',+ 'event_extraction',+ 'query_routing',+ 'query_enrichment'+ ]++ def record_usage(+ self,+ user_id: int,+ operation_type: str,+ prompt_tokens: int,+ completion_tokens: int,+ total_tokens: int,+ model_name: str = None,+ cost: float = None+ ):+ """+ Record token usage - upserts into daily aggregate.++ Args:+ user_id: User ID who made the request+ operation_type: Type of operation (summarization, chat, etc.)+ prompt_tokens: Number of input tokens+ completion_tokens: Number of output tokens+ total_tokens: Total tokens (prompt + completion)+ model_name: Name of the model used+ cost: API cost if available (e.g., from OpenRouter)+ """+ try:+ today = date.today()++ # Find or create today's record for this user/operation+ usage = TokenUsage.query.filter_by(+ user_id=user_id,+ date=today,+ operation_type=operation_type+ ).first()++ if usage:+ # Update existing record+ usage.prompt_tokens += prompt_tokens+ usage.completion_tokens += completion_tokens+ usage.total_tokens += total_tokens+ usage.request_count += 1+ if cost:+ usage.cost += cost+ if model_name:+ usage.model_name = model_name # Update to latest model used+ else:+ # Create new record+ usage = TokenUsage(+ user_id=user_id,+ date=today,+ operation_type=operation_type,+ prompt_tokens=prompt_tokens,+ completion_tokens=completion_tokens,+ total_tokens=total_tokens,+ request_count=1,+ model_name=model_name,+ cost=cost or 0.0+ )+ db.session.add(usage)++ db.session.commit()+ logger.debug(f"Recorded {total_tokens} tokens for user {user_id}, operation {operation_type}")+ return usage++ except Exception as e:+ logger.error(f"Failed to record token usage: {e}")+ db.session.rollback()+ return None++ def get_monthly_usage(self, user_id: int, year: int = None, month: int = None) -> int:+ """Get total tokens used by a user in a given month."""+ if year is None:+ year = date.today().year+ if month is None:+ month = date.today().month++ result = db.session.query(func.sum(TokenUsage.total_tokens)).filter(+ TokenUsage.user_id == user_id,+ extract('year', TokenUsage.date) == year,+ extract('month', TokenUsage.date) == month+ ).scalar()++ return result or 0++ def get_monthly_cost(self, user_id: int, year: int = None, month: int = None) -> float:+ """Get total cost for a user in a given month."""+ if year is None:+ year = date.today().year+ if month is None:+ month = date.today().month++ result = db.session.query(func.sum(TokenUsage.cost)).filter(+ TokenUsage.user_id == user_id,+ extract('year', TokenUsage.date) == year,+ extract('month', TokenUsage.date) == month+ ).scalar()++ return result or 0.0++ def check_budget(self, user_id: int) -> Tuple[bool, float, Optional[str]]:+ """+ Check if user is within budget.++ Returns:+ (can_proceed, usage_percentage, message)+ - can_proceed: False if hard cap (100%) reached+ - usage_percentage: 0-100++ - message: Warning/error message if applicable+ """+ try:+ user = db.session.get(User, user_id)+ if not user or not user.monthly_token_budget:+ return (True, 0, None) # No budget = unlimited++ current_usage = self.get_monthly_usage(user_id)+ budget = user.monthly_token_budget+ percentage = (current_usage / budget) * 100++ if percentage >= 100:+ return (False, percentage,+ f"Monthly token budget exceeded ({percentage:.1f}%). Contact admin for more tokens.")+ elif percentage >= 80:+ return (True, percentage,+ f"Warning: {percentage:.1f}% of monthly token budget used.")+ else:+ return (True, percentage, None)++ except Exception as e:+ logger.error(f"Failed to check budget for user {user_id}: {e}")+ # Fail open - allow the request if we can't check+ return (True, 0, None)++ def get_daily_stats(self, days: int = 30, user_id: int = None) -> List[Dict]:+ """Get daily token usage for charts."""+ start_date = date.today() - timedelta(days=days - 1)++ query = db.session.query(+ TokenUsage.date,+ TokenUsage.operation_type,+ func.sum(TokenUsage.total_tokens).label('tokens'),+ func.sum(TokenUsage.cost).label('cost')+ ).filter(TokenUsage.date >= start_date)++ if user_id:+ query = query.filter(TokenUsage.user_id == user_id)++ results = query.group_by(TokenUsage.date, TokenUsage.operation_type).all()++ # Organize by date+ stats = {}+ for r in results:+ date_str = r.date.isoformat()+ if date_str not in stats:+ stats[date_str] = {'date': date_str, 'total': 0, 'cost': 0.0, 'by_operation': {}}+ stats[date_str]['total'] += r.tokens or 0+ stats[date_str]['cost'] += r.cost or 0+ stats[date_str]['by_operation'][r.operation_type] = r.tokens or 0++ # Fill in missing dates with zeros+ all_dates = []+ current = start_date+ while current <= date.today():+ date_str = current.isoformat()+ if date_str not in stats:+ stats[date_str] = {'date': date_str, 'total': 0, 'cost': 0.0, 'by_operation': {}}+ all_dates.append(date_str)+ current += timedelta(days=1)++ return [stats[d] for d in sorted(all_dates)]++ def get_monthly_stats(self, months: int = 12) -> List[Dict]:+ """Get monthly token usage for charts."""+ results = db.session.query(+ extract('year', TokenUsage.date).label('year'),+ extract('month', TokenUsage.date).label('month'),+ func.sum(TokenUsage.total_tokens).label('tokens'),+ func.sum(TokenUsage.cost).label('cost')+ ).group_by('year', 'month').order_by('year', 'month').all()++ # Get last N months+ monthly_data = [+ {+ 'year': int(r.year),+ 'month': int(r.month),+ 'tokens': r.tokens or 0,+ 'cost': r.cost or 0+ }+ for r in results+ ]++ return monthly_data[-months:] if len(monthly_data) > months else monthly_data++ def get_user_stats(self) -> List[Dict]:+ """Get per-user token usage breakdown for current month."""+ today = date.today()++ results = db.session.query(+ User.id,+ User.username,+ User.monthly_token_budget,+ func.sum(TokenUsage.total_tokens).label('usage'),+ func.sum(TokenUsage.cost).label('cost')+ ).outerjoin(+ TokenUsage,+ (User.id == TokenUsage.user_id) &+ (extract('year', TokenUsage.date) == today.year) &+ (extract('month', TokenUsage.date) == today.month)+ ).group_by(User.id).all()++ return [+ {+ 'user_id': r.id,+ 'username': r.username,+ 'monthly_budget': r.monthly_token_budget,+ 'current_usage': r.usage or 0,+ 'cost': r.cost or 0,+ 'percentage': ((r.usage or 0) / r.monthly_token_budget * 100)+ if r.monthly_token_budget else 0+ }+ for r in results+ ]++ def get_today_usage(self, user_id: int = None) -> Dict:+ """Get today's token usage."""+ today = date.today()++ query = db.session.query(+ func.sum(TokenUsage.total_tokens).label('tokens'),+ func.sum(TokenUsage.cost).label('cost')+ ).filter(TokenUsage.date == today)++ if user_id:+ query = query.filter(TokenUsage.user_id == user_id)++ result = query.first()++ return {+ 'tokens': result.tokens or 0,+ 'cost': result.cost or 0+ }+++# Singleton instance+token_tracker = TokenTracker()diff --git a/src/tasks/processing.py b/src/tasks/processing.pyindex 6fe8a092..83ba1803 100644--- a/src/tasks/processing.py+++ b/src/tasks/processing.py@@ -261,7 +261,9 @@ Title:"""{"role": "user", "content": prompt_text}],temperature=0.7,- max_tokens=5000+ max_tokens=5000,+ user_id=recording.user_id,+ operation_type='title_generation')raw_response = completion.choices[0].message.content@@ -499,7 +501,9 @@ Summarization Instructions:{"role": "user", "content": prompt_text}],temperature=0.5,- max_tokens=int(os.environ.get("SUMMARY_MAX_TOKENS", "3000"))+ max_tokens=int(os.environ.get("SUMMARY_MAX_TOKENS", "3000")),+ user_id=recording.user_id,+ operation_type='summarization')raw_response = completion.choices[0].message.content@@ -742,7 +746,9 @@ You must respond with valid JSON format only."""],temperature=0.2,response_format={"type": "json_object"},- max_tokens=3000+ max_tokens=3000,+ user_id=recording.user_id,+ operation_type='event_extraction')response_content = completion.choices[0].message.contentdiff --git a/static/js/app.modular.js b/static/js/app.modular.jsindex 79d5332b..1c25b435 100644--- a/static/js/app.modular.js+++ b/static/js/app.modular.js@@ -116,6 +116,12 @@ document.addEventListener('DOMContentLoaded', async () => {const isSidebarCollapsed = ref(false);const searchTipsExpanded = ref(false);const isUserMenuOpen = ref(false);+ const tokenBudget = ref({+ has_budget: false,+ budget: null,+ usage: 0,+ percentage: 0+ });const isDarkMode = ref(false);const currentColorScheme = ref('blue');const showColorSchemeModal = ref(false);@@ -439,7 +445,7 @@ document.addEventListener('DOMContentLoaded', async () => {isLoadingMore, searchDebounceTimer,// UI- browser, isSidebarCollapsed, searchTipsExpanded, isUserMenuOpen, isDarkMode,+ browser, isSidebarCollapsed, searchTipsExpanded, isUserMenuOpen, tokenBudget, isDarkMode,currentColorScheme, showColorSchemeModal, windowWidth, mobileTab, isMetadataExpanded,showSortOptions, currentLanguage, currentLanguageName, availableLanguages, showLanguageMenu,colorSchemes, isMobileScreen, isMobileDevice,@@ -558,6 +564,17 @@ document.addEventListener('DOMContentLoaded', async () => {showToast(message, 'fa-exclamation-circle', duration, 'error');};+ const loadTokenBudget = async () => {+ try {+ const response = await fetch('/api/user/token-budget');+ if (response.ok) {+ tokenBudget.value = await response.json();+ }+ } catch (error) {+ console.error('Error loading token budget:', error);+ }+ };+// Helper function to calculate global segment index in bubble viewconst getBubbleGlobalIndex = (rowIndex, bubbleIndex) => {if (!processedTranscription.value.bubbleRows) return 0;@@ -782,7 +799,8 @@ document.addEventListener('DOMContentLoaded', async () => {t, tc, setGlobalError, showToast, formatFileSize, formatDisplayDate, formatShortDate,formatStatus, getStatusClass, formatTime, formatDuration, formatEventDateTime,getDateForSorting, isToday, isYesterday, isThisWeek, isLastWeek, isThisMonth, isLastMonth, isSameDay,- nextTick+ nextTick,+ onChatComplete: loadTokenBudget // Refresh token budget after chat};// =========================================================================@@ -1851,7 +1869,8 @@ document.addEventListener('DOMContentLoaded', async () => {await Promise.all([recordingsComposable.loadRecordings(),recordingsComposable.loadTags(),- recordingsComposable.loadSpeakers()+ recordingsComposable.loadSpeakers(),+ loadTokenBudget()]);// Load config@@ -2046,6 +2065,7 @@ document.addEventListener('DOMContentLoaded', async () => {formatDateTime: formatEventDateTime, // Alias for recovery modalsetGlobalError,showToast,+ loadTokenBudget,getContrastTextColor,getBubbleGlobalIndex,formatRecordingMode,diff --git a/static/js/modules/composables/chat.js b/static/js/modules/composables/chat.jsindex 33bef7a2..ad21fb43 100644--- a/static/js/modules/composables/chat.js+++ b/static/js/modules/composables/chat.js@@ -9,7 +9,7 @@ export function useChat(state, utils) {isChatLoading, chatMessagesRef, selectedRecording, csrfToken} = state;- const { showToast, setGlobalError } = utils;+ const { showToast, setGlobalError, onChatComplete } = utils;// Helper function to check if chat is scrolled to bottom (within bottom 5%)const isChatScrolledToBottom = () => {@@ -158,6 +158,9 @@ export function useChat(state, utils) {return;}if (data.error) {+ if (data.budget_exceeded) {+ throw new Error('Monthly token budget exceeded. Please contact your administrator.');+ }throw new Error(data.error);}} catch (e) {@@ -189,6 +192,10 @@ export function useChat(state, utils) {if (isChatScrolledToBottom()) {scrollChatToBottom();}+ // Refresh token budget after chat completion+ if (onChatComplete) {+ onChatComplete();+ }}};diff --git a/static/js/modules/composables/reprocess.js b/static/js/modules/composables/reprocess.jsindex fa541d33..8b2e58ef 100644--- a/static/js/modules/composables/reprocess.js+++ b/static/js/modules/composables/reprocess.js@@ -15,7 +15,7 @@ export function useReprocess(state, utils) {currentlyProcessingFile, uploadQueue} = state;- const { showToast, setGlobalError } = utils;+ const { showToast, setGlobalError, onChatComplete } = utils;// Store for active polling intervalsconst reprocessingPolls = new Map();@@ -340,6 +340,8 @@ export function useReprocess(state, utils) {if (statusData.status === 'COMPLETED') {showToast('Processing completed!', 'fa-check-circle');+ // Refresh token budget after LLM operations complete+ if (onChatComplete) onChatComplete();}}} else if (statusData.status === 'FAILED') {diff --git a/static/js/modules/composables/speakers.js b/static/js/modules/composables/speakers.jsindex a37ddece..5c6c0df7 100644--- a/static/js/modules/composables/speakers.js+++ b/static/js/modules/composables/speakers.js@@ -19,7 +19,7 @@ export function useSpeakers(state, utils, processedTranscription) {voiceSuggestions, loadingVoiceSuggestions} = state;- const { showToast, setGlobalError } = utils;+ const { showToast, setGlobalError, onChatComplete } = utils;// Current speaker highlight statelet currentSpeakerId = null;@@ -401,6 +401,8 @@ export function useSpeakers(state, utils, processedTranscription) {}showToast('Summary updated!', 'fa-check-circle');+ // Refresh token budget after LLM operation+ if (onChatComplete) onChatComplete();} else if (statusData.status === 'FAILED' || statusData.status === 'ERROR') {// Stop polling if it failedclearInterval(pollInterval);@@ -765,6 +767,9 @@ export function useSpeakers(state, utils, processedTranscription) {showToast('No speakers could be identified from the context.', 'fa-info-circle');}+ // Refresh token budget after LLM operation+ if (onChatComplete) onChatComplete();+} catch (error) {console.error('Auto Identify Speakers Error:', error);showToast(`Error: ${error.message}`, 'fa-exclamation-circle', 5000, 'error');diff --git a/static/js/modules/composables/upload.js b/static/js/modules/composables/upload.jsindex e1696395..069210b8 100644--- a/static/js/modules/composables/upload.js+++ b/static/js/modules/composables/upload.js@@ -18,7 +18,7 @@ export function useUpload(state, utils) {const { computed, nextTick } = Vue;- const { setGlobalError, showToast, formatFileSize } = utils;+ const { setGlobalError, showToast, formatFileSize, onChatComplete } = utils;// Compute selected tags from IDsconst selectedTags = computed(() => {@@ -441,6 +441,8 @@ export function useUpload(state, utils) {pollInterval.value = null;resetCurrentFileProcessingState();isProcessingActive.value = false;+ // Refresh token budget after LLM operations+ if (onChatComplete) onChatComplete();startProcessingQueue();return;@@ -465,6 +467,8 @@ export function useUpload(state, utils) {pollInterval.value = null;resetCurrentFileProcessingState();isProcessingActive.value = false;+ // Refresh token budget after LLM operations+ if (onChatComplete) onChatComplete();startProcessingQueue();return;}@@ -479,6 +483,8 @@ export function useUpload(state, utils) {pollInterval.value = null;resetCurrentFileProcessingState();isProcessingActive.value = false;+ // Refresh token budget after LLM operations+ if (onChatComplete) onChatComplete();startProcessingQueue();return;}diff --git a/templates/admin.html b/templates/admin.htmlindex 6fed0422..a99bce22 100644--- a/templates/admin.html+++ b/templates/admin.html@@ -15,6 +15,7 @@<script src="{{ url_for('static', filename='vendor/js/tailwind.min.js') }}"></script><!-- All dependencies bundled locally for offline support --><script src="{{ url_for('static', filename='vendor/js/vue.global.js') }}"></script>+<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js"></script><!-- All dependencies bundled locally for offline support --><link rel="stylesheet" href="{{ url_for('static', filename='vendor/css/fontawesome.min.css') }}"><link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">@@ -399,8 +400,124 @@</div></div></div>++ <!-- Token Usage Section -->+ <div class="mt-6">+ <h4 class="text-lg font-medium text-[var(--text-secondary)] mb-4">+ <i class="fas fa-coins mr-2"></i> Token Usage Statistics+ </h4>++ <!-- Token Usage Summary Cards -->+ <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 mb-6">+ <div class="bg-[var(--bg-tertiary)] p-4 rounded-lg border border-[var(--border-primary)] shadow-sm">+ <div class="flex items-center">+ <div class="p-3 rounded-full bg-[var(--bg-info-light)] text-[var(--text-info-strong)]">+ <i class="fas fa-calendar-day text-xl"></i>+ </div>+ <div class="ml-4">+ <p class="text-sm font-medium text-[var(--text-muted)]">Today's Tokens</p>+ <p class="text-2xl font-semibold text-[var(--text-primary)]">${ tokenStats.today?.tokens?.toLocaleString() || 0 }</p>+ </div>+ </div>+ </div>++ <div class="bg-[var(--bg-tertiary)] p-4 rounded-lg border border-[var(--border-primary)] shadow-sm">+ <div class="flex items-center">+ <div class="p-3 rounded-full bg-[var(--bg-success-light)] text-[var(--text-success-strong)]">+ <i class="fas fa-calendar-alt text-xl"></i>+ </div>+ <div class="ml-4">+ <p class="text-sm font-medium text-[var(--text-muted)]">This Month</p>+ <p class="text-2xl font-semibold text-[var(--text-primary)]">${ tokenStats.current_month?.tokens?.toLocaleString() || 0 }</p>+ </div>+ </div>+ </div>++ <div class="bg-[var(--bg-tertiary)] p-4 rounded-lg border border-[var(--border-primary)] shadow-sm">+ <div class="flex items-center">+ <div class="p-3 rounded-full bg-[var(--bg-warn-light)] text-[var(--text-warn-strong)]">+ <i class="fas fa-dollar-sign text-xl"></i>+ </div>+ <div class="ml-4">+ <p class="text-sm font-medium text-[var(--text-muted)]">Monthly Cost</p>+ <p class="text-2xl font-semibold text-[var(--text-primary)]">$${ (tokenStats.current_month?.cost || 0).toFixed(4) }</p>+ </div>+ </div>+ </div>++ <div class="bg-[var(--bg-tertiary)] p-4 rounded-lg border border-[var(--border-primary)] shadow-sm">+ <div class="flex items-center">+ <div class="p-3 rounded-full" :class="tokenStats.users_at_100_percent > 0 ? 'bg-[var(--bg-danger-light)] text-[var(--text-danger-strong)]' : 'bg-[var(--bg-accent)] text-[var(--text-accent)]'">+ <i class="fas fa-exclamation-triangle text-xl"></i>+ </div>+ <div class="ml-4">+ <p class="text-sm font-medium text-[var(--text-muted)]">Budget Warnings</p>+ <p class="text-2xl font-semibold text-[var(--text-primary)]">+ <span v-if="tokenStats.users_at_100_percent > 0" class="text-[var(--text-danger-strong)]">${ tokenStats.users_at_100_percent } blocked</span>+ <span v-else-if="tokenStats.users_over_80_percent > 0" class="text-[var(--text-warn-strong)]">${ tokenStats.users_over_80_percent } warning</span>+ <span v-else class="text-[var(--text-success-strong)]">None</span>+ </p>+ </div>+ </div>+ </div>+ </div>++ <!-- Token Usage Charts -->+ <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">+ <!-- Daily Usage Chart -->+ <div class="bg-[var(--bg-tertiary)] p-4 rounded-lg border border-[var(--border-primary)] shadow-sm">+ <h5 class="text-md font-medium text-[var(--text-secondary)] mb-3">Daily Token Usage (Last 30 Days)</h5>+ <div class="h-64">+ <canvas id="dailyTokenChart"></canvas>+ </div>+ </div>++ <!-- Monthly Usage Chart -->+ <div class="bg-[var(--bg-tertiary)] p-4 rounded-lg border border-[var(--border-primary)] shadow-sm">+ <h5 class="text-md font-medium text-[var(--text-secondary)] mb-3">Monthly Token Usage (Last 12 Months)</h5>+ <div class="h-64">+ <canvas id="monthlyTokenChart"></canvas>+ </div>+ </div>+ </div>++ <!-- Per-User Token Usage -->+ <div class="mt-6 bg-[var(--bg-tertiary)] p-4 rounded-lg border border-[var(--border-primary)] shadow-sm">+ <h5 class="text-md font-medium text-[var(--text-secondary)] mb-3">User Token Usage (This Month)</h5>+ <div class="space-y-3 max-h-64 overflow-y-auto">+ <div v-for="user in tokenUserStats" :key="user.user_id" class="flex justify-between items-center p-2 bg-[var(--bg-secondary)] rounded-lg border border-[var(--border-primary)]">+ <div class="flex items-center flex-1">+ <i class="fas fa-user text-[var(--text-accent)] mr-2"></i>+ <span class="text-sm font-medium text-[var(--text-primary)]">${ user.username }</span>+ </div>+ <div class="flex items-center space-x-4 flex-1">+ <div class="flex-1">+ <div class="w-full bg-[var(--bg-tertiary)] rounded-full h-2">+ <div class="h-2 rounded-full"+ :class="{+ 'bg-[var(--text-danger-strong)]': user.percentage >= 100,+ 'bg-[var(--text-warn-strong)]': user.percentage >= 80 && user.percentage < 100,+ 'bg-[var(--text-success-strong)]': user.percentage < 80+ }"+ :style="{width: Math.min(user.percentage || 0, 100) + '%'}">+ </div>+ </div>+ </div>+ <div class="text-sm text-[var(--text-secondary)] text-right min-w-[120px]">+ <span class="font-medium">${ user.current_usage?.toLocaleString() || 0 }</span>+ <span v-if="user.monthly_budget" class="text-[var(--text-muted)]"> / ${ user.monthly_budget?.toLocaleString() }</span>+ <span v-else class="text-[var(--text-muted)]"> (unlimited)</span>+ </div>+ </div>+ </div>+ <div v-if="tokenUserStats.length === 0" class="text-center text-sm text-[var(--text-muted)] p-2">+ No token usage data available+ </div>+ </div>+ </div>+ </div></div>-+<!-- System Settings Tab --><div v-show="activeTab === 'settings'"><div class="flex justify-between items-center mb-4">@@ -862,6 +979,11 @@ Summarization Instructions:<span class="ml-3 text-sm text-[var(--text-secondary)]">${t('adminDashboard.adminUser')}</span></label></div>+ <div class="mb-4">+ <label class="block text-sm font-medium text-[var(--text-secondary)] mb-1">Monthly Token Budget</label>+ <input v-model.number="newUser.monthlyTokenBudget" type="number" min="5000" step="5000" placeholder="Leave empty for unlimited" class="w-full px-3 py-2 border border-[var(--border-secondary)] rounded-md focus:outline-none focus:ring-2 focus:ring-[var(--border-focus)] bg-[var(--bg-input)] text-[var(--text-primary)]">+ <p class="text-xs text-[var(--text-muted)] mt-1">Set a monthly token limit for AI features. Leave empty for unlimited.</p>+ </div><div class="flex justify-end space-x-3"><button type="button" @click="showAddUserModal = false" class="px-4 py-2 border border-[var(--border-secondary)] text-[var(--text-secondary)] rounded-md hover:bg-[var(--bg-tertiary)]">${t('buttons.cancel')}</button><button type="submit" class="px-4 py-2 bg-[var(--bg-button)] text-[var(--text-button)] rounded-md hover:bg-[var(--bg-button-hover)]">${t('adminDashboard.addUser')}</button>@@ -869,7 +991,7 @@ Summarization Instructions:</form></div></div>-+<!-- Edit User Modal --><div v-if="showEditUserModal" @click.self="showEditUserModal = false" class="fixed inset-0 bg-black bg-opacity-70 backdrop-blur-sm flex items-center justify-center z-50 p-4"><div class="bg-[var(--bg-secondary)] p-6 rounded-lg shadow-xl w-full max-w-md">@@ -897,6 +1019,20 @@ Summarization Instructions:<span class="ml-3 text-sm text-[var(--text-secondary)]">${t('adminDashboard.adminUser')}</span></label></div>+ <div class="mb-4">+ <label class="block text-sm font-medium text-[var(--text-secondary)] mb-1">Monthly Token Budget</label>+ <input v-model.number="editingUser.monthly_token_budget" type="number" min="5000" step="5000" placeholder="Leave empty for unlimited" class="w-full px-3 py-2 border border-[var(--border-secondary)] rounded-md focus:outline-none focus:ring-2 focus:ring-[var(--border-focus)] bg-[var(--bg-input)] text-[var(--text-primary)]">+ <p class="text-xs text-[var(--text-muted)] mt-1">Set a monthly token limit for AI features. Leave empty for unlimited.</p>+ <div v-if="editingUser.monthly_token_budget && editingUser.current_token_usage !== undefined" class="mt-2">+ <div class="flex justify-between text-xs text-[var(--text-muted)] mb-1">+ <span>Current usage: ${ editingUser.current_token_usage?.toLocaleString() || 0 } tokens</span>+ <span :class="{'text-[var(--text-danger-strong)]': editingUser.token_usage_percentage >= 100, 'text-[var(--text-warn-strong)]': editingUser.token_usage_percentage >= 80 && editingUser.token_usage_percentage < 100}">${ editingUser.token_usage_percentage || 0 }%</span>+ </div>+ <div class="w-full bg-[var(--bg-tertiary)] rounded-full h-2">+ <div class="h-2 rounded-full" :class="{'bg-[var(--text-danger-strong)]': editingUser.token_usage_percentage >= 100, 'bg-[var(--text-warn-strong)]': editingUser.token_usage_percentage >= 80 && editingUser.token_usage_percentage < 100, 'bg-[var(--text-success-strong)]': editingUser.token_usage_percentage < 80}" :style="{width: Math.min(editingUser.token_usage_percentage || 0, 100) + '%'}"></div>+ </div>+ </div>+ </div><div class="flex justify-end space-x-3"><button type="button" @click="showEditUserModal = false" class="px-4 py-2 border border-[var(--border-secondary)] text-[var(--text-secondary)] rounded-md hover:bg-[var(--bg-tertiary)]">Cancel</button><button type="submit" class="px-4 py-2 bg-[var(--bg-button)] text-[var(--text-button)] rounded-md hover:bg-[var(--bg-button-hover)]">${t('adminDashboard.updateUser')}</button>@@ -904,7 +1040,7 @@ Summarization Instructions:</form></div></div>-+<!-- Delete User Confirmation Modal --><div v-if="showDeleteUserModal" @click.self="showDeleteUserModal = false" class="fixed inset-0 bg-black bg-opacity-70 backdrop-blur-sm flex items-center justify-center z-50 p-4"><div class="bg-[var(--bg-secondary)] p-6 rounded-lg shadow-xl w-full max-w-md">@@ -1456,6 +1592,16 @@ Summarization Instructions:failed_recordings: 0,top_users: []});+ const tokenStats = ref({+ today: { tokens: 0, cost: 0 },+ current_month: { tokens: 0, cost: 0 },+ user_count_with_usage: 0,+ users_over_80_percent: 0,+ users_at_100_percent: 0+ });+ const tokenUserStats = ref([]);+ const dailyTokenChart = ref(null);+ const monthlyTokenChart = ref(null);const settings = ref([]);const isLoadingUsers = ref(true);const isLoadingSettings = ref(false);@@ -1472,7 +1618,8 @@ Summarization Instructions:email: '',password: '',confirmPassword: '',- isAdmin: false+ isAdmin: false,+ monthlyTokenBudget: null});const editingUser = ref(null);const userToDelete = ref(null);@@ -1593,7 +1740,7 @@ Summarization Instructions:throw new Error('Rate limit exceeded. Please wait a moment and try again.');}if (!response.ok) throw new Error('Failed to load settings');-+const data = await response.json();settings.value = data;} catch (error) {@@ -1603,7 +1750,153 @@ Summarization Instructions:isLoadingSettings.value = false;}};-++ // Token Stats Functions+ const loadTokenStats = async () => {+ try {+ // Load summary stats+ const statsResponse = await fetch('/admin/token-stats');+ if (statsResponse.ok) {+ tokenStats.value = await statsResponse.json();+ }++ // Load user stats+ const userStatsResponse = await fetch('/admin/token-stats/users');+ if (userStatsResponse.ok) {+ const data = await userStatsResponse.json();+ tokenUserStats.value = data.users || [];+ }++ // Load and render charts+ await loadTokenCharts();+ } catch (error) {+ console.error('Error loading token stats:', error);+ }+ };++ const loadTokenCharts = async () => {+ try {+ // Load daily stats+ const dailyResponse = await fetch('/admin/token-stats/daily?days=30');+ if (dailyResponse.ok) {+ const dailyData = await dailyResponse.json();+ renderDailyChart(dailyData.stats);+ }++ // Load monthly stats+ const monthlyResponse = await fetch('/admin/token-stats/monthly?months=12');+ if (monthlyResponse.ok) {+ const monthlyData = await monthlyResponse.json();+ renderMonthlyChart(monthlyData.stats);+ }+ } catch (error) {+ console.error('Error loading token charts:', error);+ }+ };++ const renderDailyChart = (data) => {+ const ctx = document.getElementById('dailyTokenChart');+ if (!ctx) return;++ // Destroy existing chart if any+ if (dailyTokenChart.value) {+ dailyTokenChart.value.destroy();+ }++ const labels = data.map(d => d.date);+ const tokens = data.map(d => d.total);++ // Detect dark mode+ const isDark = document.documentElement.classList.contains('dark');+ const textColor = isDark ? '#9ca3af' : '#6b7280';+ const gridColor = isDark ? '#374151' : '#e5e7eb';++ dailyTokenChart.value = new Chart(ctx, {+ type: 'bar',+ data: {+ labels: labels,+ datasets: [{+ label: 'Tokens',+ data: tokens,+ backgroundColor: isDark ? 'rgba(99, 102, 241, 0.7)' : 'rgba(79, 70, 229, 0.7)',+ borderColor: isDark ? 'rgba(99, 102, 241, 1)' : 'rgba(79, 70, 229, 1)',+ borderWidth: 1+ }]+ },+ options: {+ responsive: true,+ maintainAspectRatio: false,+ plugins: {+ legend: { display: false }+ },+ scales: {+ x: {+ ticks: { color: textColor, maxRotation: 45, minRotation: 45 },+ grid: { color: gridColor }+ },+ y: {+ beginAtZero: true,+ ticks: { color: textColor },+ grid: { color: gridColor }+ }+ }+ }+ });+ };++ const renderMonthlyChart = (data) => {+ const ctx = document.getElementById('monthlyTokenChart');+ if (!ctx) return;++ // Destroy existing chart if any+ if (monthlyTokenChart.value) {+ monthlyTokenChart.value.destroy();+ }++ const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];+ const labels = data.map(d => `${monthNames[d.month - 1]} ${d.year}`);+ const tokens = data.map(d => d.tokens);++ // Detect dark mode+ const isDark = document.documentElement.classList.contains('dark');+ const textColor = isDark ? '#9ca3af' : '#6b7280';+ const gridColor = isDark ? '#374151' : '#e5e7eb';++ monthlyTokenChart.value = new Chart(ctx, {+ type: 'line',+ data: {+ labels: labels,+ datasets: [{+ label: 'Tokens',+ data: tokens,+ fill: true,+ backgroundColor: isDark ? 'rgba(34, 197, 94, 0.2)' : 'rgba(22, 163, 74, 0.2)',+ borderColor: isDark ? 'rgba(34, 197, 94, 1)' : 'rgba(22, 163, 74, 1)',+ borderWidth: 2,+ tension: 0.3+ }]+ },+ options: {+ responsive: true,+ maintainAspectRatio: false,+ plugins: {+ legend: { display: false }+ },+ scales: {+ x: {+ ticks: { color: textColor },+ grid: { color: gridColor }+ },+ y: {+ beginAtZero: true,+ ticks: { color: textColor },+ grid: { color: gridColor }+ }+ }+ }+ });+ };+const showEditSettingModal = ref(false);const editingSetting = ref(null);const settingNewValue = ref('');@@ -1785,7 +2078,8 @@ Summarization Instructions:username: newUser.value.username,email: newUser.value.email,password: newUser.value.password,- is_admin: newUser.value.isAdmin+ is_admin: newUser.value.isAdmin,+ monthly_token_budget: newUser.value.monthlyTokenBudget || null})});@@ -1800,7 +2094,8 @@ Summarization Instructions:email: '',password: '',confirmPassword: '',- isAdmin: false+ isAdmin: false,+ monthlyTokenBudget: null};showAddUserModal.value = false;@@ -1824,9 +2119,10 @@ Summarization Instructions:const payload = {username: editingUser.value.username,email: editingUser.value.email,- is_admin: editingUser.value.is_admin+ is_admin: editingUser.value.is_admin,+ monthly_token_budget: editingUser.value.monthly_token_budget || null};-+// Only include password if it was changedif (editingUser.value.password) {payload.password = editingUser.value.password;@@ -2598,6 +2894,7 @@ Summarization Instructions:if (!isTeamAdminOnly.value) {loadUsers();loadStats();+ loadTokenStats();// Load settings first, then default prompt can use that dataif (activeTab.value === 'settings' || activeTab.value === 'prompts') {@@ -2695,6 +2992,8 @@ Summarization Instructions:activeTab,users,stats,+ tokenStats,+ tokenUserStats,settings,isLoadingUsers,isLoadingSettings,@@ -2768,6 +3067,7 @@ Summarization Instructions:loadUsers,loadStats,loadSettings,+ loadTokenStats,editSetting,saveSettingValue,updateSetting,diff --git a/templates/components/header.html b/templates/components/header.htmlindex 3b0d2e64..00555960 100644--- a/templates/components/header.html+++ b/templates/components/header.html@@ -16,7 +16,9 @@</div><!-- Right side: User menu and controls -->- <div class="flex items-center gap-3">+ <div class="flex items-center gap-2 sm:gap-3">+ {% include 'components/token_budget_indicator.html' %}+<!-- Inquire Mode Button -->{% if inquire_mode_enabled %}<a href="/inquire"diff --git a/templates/components/token_budget_indicator.html b/templates/components/token_budget_indicator.htmlnew file mode 100644index 00000000..1f450a40--- /dev/null+++ b/templates/components/token_budget_indicator.html@@ -0,0 +1,17 @@+<!-- Token Budget Indicator Component -->+<!-- Only shows if user has a budget limit set -->+<div v-if="tokenBudget && tokenBudget.has_budget"+ class="hidden sm:flex items-center gap-1.5 px-2 py-1 rounded text-xs text-[var(--text-muted)] hover:text-[var(--text-secondary)] transition-colors cursor-default"+ :title="'Token usage: ' + (tokenBudget.usage || 0).toLocaleString() + ' / ' + (tokenBudget.budget || 0).toLocaleString()">+ <i class="fas fa-coins text-[10px]"+ :style="tokenBudget.percentage >= 100 ? {color: '#ef4444'} : tokenBudget.percentage >= 80 ? {color: '#f59e0b'} : {}"></i>+ <span :style="tokenBudget.percentage >= 100 ? {color: '#ef4444'} : tokenBudget.percentage >= 80 ? {color: '#f59e0b'} : {}">${ tokenBudget.percentage }%</span>+ <div class="w-12 h-1 bg-[var(--bg-tertiary)] rounded-full overflow-hidden">+ <div class="h-full rounded-full transition-all duration-300 bg-[var(--text-accent)]"+ :style="{+ width: Math.min(tokenBudget.percentage, 100) + '%',+ backgroundColor: tokenBudget.percentage >= 100 ? '#ef4444' : tokenBudget.percentage >= 80 ? '#f59e0b' : null+ }">+ </div>+ </div>+</div>diff --git a/templates/inquire.html b/templates/inquire.htmlindex cdfbe1db..6d1ff9ae 100644--- a/templates/inquire.html+++ b/templates/inquire.html@@ -149,9 +149,11 @@</div><!-- Right side: User menu -->- <div class="flex items-center gap-3">+ <div class="flex items-center gap-2 sm:gap-3">+ {% include 'components/token_budget_indicator.html' %}+<!-- New Recording Button -->- <a href="/"+ <a href="/"class="px-3 py-1.5 bg-[var(--bg-button)] text-[var(--text-button)] rounded-lg hover:bg-[var(--bg-button-hover)] transition-colors text-sm flex items-center gap-2"><i class="fas fa-plus"></i><span class="hidden sm:inline">${t('nav.newRecording')}</span>@@ -434,6 +436,19 @@<!-- Toast Container --><div id="toastContainer" class="fixed top-4 right-4 z-50 space-y-2"></div>++ <!-- Global Error Banner -->+ <div v-if="globalError" class="fixed top-16 left-1/2 transform -translate-x-1/2 z-50 max-w-lg w-full mx-4">+ <div class="bg-red-600 text-white px-4 py-3 rounded-lg shadow-lg flex items-center justify-between">+ <div class="flex items-center">+ <i class="fas fa-exclamation-circle mr-3"></i>+ <span class="text-sm font-medium">${ globalError }</span>+ </div>+ <button @click="globalError = null" class="ml-4 text-white hover:text-gray-200">+ <i class="fas fa-times"></i>+ </button>+ </div>+ </div><!-- Color Scheme Modal --><div v-if="showColorSchemeModal" class="color-scheme-modal" @click.self="closeColorSchemeModal"><div class="color-scheme-modal-content">@@ -604,6 +619,12 @@const inquireMessagesRef = ref(null);const globalError = ref(null);const shouldAutoScroll = ref(true); // Track if we should auto-scroll+ const tokenBudget = ref({+ has_budget: false,+ budget: null,+ usage: 0,+ percentage: 0+ });// --- Computed ---const hasActiveFilters = computed(() => {@@ -636,6 +657,17 @@}};+ const loadTokenBudget = async () => {+ try {+ const response = await fetch('/api/user/token-budget');+ if (response.ok) {+ tokenBudget.value = await response.json();+ }+ } catch (error) {+ console.error('Error loading token budget:', error);+ }+ };+const loadAvailableFilters = async () => {try {const response = await fetch('/api/inquire/available_filters');@@ -723,6 +755,9 @@} else if (data.end_of_stream) {return;} else if (data.error) {+ if (data.budget_exceeded) {+ throw new Error('Monthly token budget exceeded. Please contact your administrator for more tokens.');+ }throw new Error(data.error);}} catch (parseError) {@@ -733,10 +768,11 @@}} catch (error) {console.error('Error in inquire chat:', error);- setGlobalError('Chat failed. Please try again.');+ setGlobalError(error.message || 'Chat failed. Please try again.', 10000);} finally {isInquireChatLoading.value = false;showProcessingStatus.value = false;+ loadTokenBudget(); // Refresh token usage after chatawait nextTick();scrollToBottom(); // Use smart scrolling}@@ -846,7 +882,8 @@applyThemeFromStorage();loadAvailableFilters();-+ loadTokenBudget();+// Add scroll listener for smart auto-scroll after DOM is readyawait nextTick();if (inquireMessagesRef.value) {@@ -872,6 +909,7 @@isInquireChatLoading,inquireMessagesRef,globalError,+ tokenBudget,isUserMenuOpen,isMobileSidebarOpen,chatProcessingStatus,