Connection
Base URL, credentials and a reachable server.
Bring arrival timestamps and request costs into the same project discussion so capped refill and admission use one explicit state transition.
Base URL, credentials and a reachable server.
A model available through the configured endpoint.
Context limits and tool support are determined by the model and server.
from math import isfinite
def admit(tokens, last, now, capacity, rate, cost):
values = (tokens, last, now, capacity, rate, cost)
if not all(type(x) in (int, float) and isfinite(x) for x in values):
raise ValueError("finite numbers required")
if not (capacity > 0 and rate > 0 and 0 < cost <= capacity
and 0 <= tokens <= capacity and now >= last):
raise ValueError("invalid bucket state")
refill = min(capacity, tokens + (now - last) * rate)
allowed = refill >= cost
return (refill - cost if allowed else refill, now), allowed