import time from collections import defaultdict class RetryBudget: def __init__(self, run_limit=12, step_limit=3): self.run_limit = run_limit self.step_limit = step_limit self._total = 0 self._by_step = defaultdict(int) def can_retry(self, step: str) -> bool: if self._total >= self.run_limit: return False return self._by_step[step] < self.step_limit def charge(self, step: str) -> None: self._total += 1 self._by_step[step] += 1 def state(self) -> dict: return {"total": self._total, "by_step": dict(self._by_step)}