Skip to content

The agent that grows with you

refactor(agent/usage_pricing,web_search_registry): single-pass rate accumulation; slice-rotate the keyless ring

Teknium9f652b5677a6
diff --git a/agent/usage_pricing.py b/agent/usage_pricing.py
index 2a4fa028a03..d40b269ccf9 100644
--- a/agent/usage_pricing.py
+++ b/agent/usage_pricing.py
@@ -537,7 +537,7 @@ def estimate_usage_cost(
# Whole-request context tier (e.g. Gemini Pro >200k prompts): above the
# threshold the *_above rates apply to the entire request; None falls back.
above = entry.tier_threshold_tokens is not None and usage.prompt_tokens > entry.tier_threshold_tokens
- buckets = []
+ amount = _ZERO
for tokens, rate, rate_above, note in (
(usage.input_tokens, entry.input_cost_per_million, entry.input_cost_per_million_above, ()),
(usage.output_tokens, entry.output_cost_per_million, entry.output_cost_per_million_above, ()),
@@ -548,14 +548,11 @@ def estimate_usage_cost(
):
if above and rate_above is not None:
rate = rate_above
- if tokens and rate is None:
- return _unknown_cost(entry.source, *note)
- buckets.append((tokens, rate))
-
- amount = _ZERO
- for tokens, rate in buckets:
- if rate is not None:
- amount += Decimal(tokens) * rate / _ONE_MILLION
+ if rate is None:
+ if tokens:
+ return _unknown_cost(entry.source, *note)
+ continue
+ amount += Decimal(tokens) * rate / _ONE_MILLION
if entry.request_cost is not None and usage.request_count:
amount += Decimal(usage.request_count) * entry.request_cost
diff --git a/agent/web_search_registry.py b/agent/web_search_registry.py
index 4bf0eb6b919..5913f2fa906 100644
--- a/agent/web_search_registry.py
+++ b/agent/web_search_registry.py
@@ -74,9 +74,7 @@ def _keyless_preference() -> tuple:
from plugins.web.keyless_mcp import _KEYLESS_RING, _ring_cursor
start = _ring_cursor % len(_KEYLESS_RING)
- return tuple(
- _KEYLESS_RING[(start + i) % len(_KEYLESS_RING)] for i in range(len(_KEYLESS_RING))
- )
+ return tuple(_KEYLESS_RING[start:] + _KEYLESS_RING[:start])
except Exception as exc: # noqa: BLE001 — ring optional in stripped envs
logger.debug("keyless ring order unavailable: %s", exc)
return _KEYLESS_PREFERENCE
@@ -94,9 +92,10 @@ def _resolve(configured: Optional[str], *, capability: str) -> Optional[WebSearc
snapshot = _registry.merged()
def _capable(p: WebSearchProvider) -> bool:
- if capability == "search":
- return bool(p.supports_search())
- return bool(p.supports_extract()) if capability == "extract" else False
+ return bool(
+ p.supports_search() if capability == "search"
+ else p.supports_extract() if capability == "extract" else False
+ )
def _available(p: WebSearchProvider) -> bool:
return is_available_safe(p, logger, "provider %s.is_available() raised %s")