import threading from enum import Enum from collections import defaultdict class UsageFeature(str, Enum): TEXT = "text" IMAGE = "image" VIDEO = "video" EMR = "emr" SPEECH = "speech" class UsageTracker: """ Simple in-memory usage tracker. - Not durable: resets on process restart. - Thread-safe for single-process deployments. """ TEXT_FREE_LIMIT = 10 def __init__(self) -> None: self._lock = threading.Lock() self._counts: dict[str, dict[UsageFeature, int]] = defaultdict( lambda: defaultdict(int) ) def record(self, user_id: str, feature: UsageFeature) -> int: with self._lock: self._counts[user_id][feature] += 1 return self._counts[user_id][feature] def get_count(self, user_id: str, feature: UsageFeature) -> int: with self._lock: return self._counts[user_id][feature] # Singleton instance shared across dependencies usage_tracker = UsageTracker()