A single API key with concurrency and rate-limit metadata. Attributes ---------- key : API key string (e.g. ``"sk-or-xxx"``). base_url : Optional base URL override for the provider endpoint. model : Model label to use with this key. rate_limited :
| 79 | |
| 80 | @dataclass |
| 81 | class ApiKey: |
| 82 | """A single API key with concurrency and rate-limit metadata. |
| 83 | |
| 84 | Attributes |
| 85 | ---------- |
| 86 | key : |
| 87 | API key string (e.g. ``"sk-or-xxx"``). |
| 88 | base_url : |
| 89 | Optional base URL override for the provider endpoint. |
| 90 | model : |
| 91 | Model label to use with this key. |
| 92 | rate_limited : |
| 93 | ``True`` when this key is cooling down after a 429 response. |
| 94 | rate_limited_until : |
| 95 | Monotonic timestamp when this key becomes eligible again after a |
| 96 | 429. Only meaningful when *rate_limited* is ``True``. |
| 97 | consecutive_429 : |
| 98 | Count of consecutive rate-limit hits. Used to compute the next |
| 99 | backoff duration via :math:`30 \\times 2^n` seconds, capped at 300. |
| 100 | total_requests : |
| 101 | Cumulative request count served by this key. Used for |
| 102 | least-loaded scheduling. |
| 103 | active_requests : |
| 104 | Number of callers currently using this key. |
| 105 | max_concurrent : |
| 106 | Maximum number of simultaneous callers allowed on this key |
| 107 | (default 5). One key serves up to this many concurrent LLM calls. |
| 108 | """ |
| 109 | |
| 110 | key: str |
| 111 | base_url: str | None |
| 112 | model: str |
| 113 | rate_limited: bool = False |
| 114 | rate_limited_until: float = 0.0 |
| 115 | consecutive_429: int = 0 |
| 116 | total_requests: int = 0 |
| 117 | active_requests: int = 0 |
| 118 | max_concurrent: int = _DEFAULT_MAX_CONCURRENT_PER_KEY |
| 119 | |
| 120 | @property |
| 121 | def available(self) -> bool: |
| 122 | """``True`` when this key can accept at least one more caller.""" |
| 123 | return not self.rate_limited and self.active_requests < self.max_concurrent |
| 124 | |
| 125 | |
| 126 | # --------------------------------------------------------------------------- |
no outgoing calls