fix(tracker): close alpha accounting review gaps
Address post-review blockers from the alpha hardening diff: charge non-stream completions that omit usage, include prompt tokens in max_charge_per_request enforcement, and wire the CLI to a persisted local registry contract by default. Verification: .venv/bin/python -m compileall -q packages tests; focused billing/CLI checks passed; full pytest: 314 passed, 3 skipped, 1 env failure from existing meshnet-node on port 7000.
This commit is contained in:
@@ -1293,15 +1293,43 @@ def _request_total_token_upper_bound(body: dict) -> int | None:
|
||||
|
||||
def _billable_non_stream_tokens(payload: dict, request_body: dict) -> int:
|
||||
reported = _usage_total_tokens(payload)
|
||||
if reported is None:
|
||||
return 0
|
||||
billable = max(0, reported)
|
||||
upper_bound = _request_total_token_upper_bound(request_body)
|
||||
if reported is None:
|
||||
completion_estimate = _observed_non_stream_completion_tokens(payload)
|
||||
if completion_estimate > 0:
|
||||
billable = completion_estimate + (_estimate_prompt_tokens(request_body) or 0)
|
||||
elif upper_bound is not None:
|
||||
billable = upper_bound
|
||||
else:
|
||||
return 0
|
||||
return min(billable, upper_bound) if upper_bound is not None else billable
|
||||
billable = max(0, reported)
|
||||
if upper_bound is not None:
|
||||
billable = min(billable, upper_bound)
|
||||
return billable
|
||||
|
||||
|
||||
def _observed_non_stream_completion_tokens(payload: dict) -> int:
|
||||
choices = payload.get("choices")
|
||||
if not isinstance(choices, list):
|
||||
return 0
|
||||
observed = 0
|
||||
for choice in choices:
|
||||
if not isinstance(choice, dict):
|
||||
continue
|
||||
message = choice.get("message")
|
||||
estimated: int | None = None
|
||||
if isinstance(message, dict):
|
||||
estimated = _estimate_text_tokens(message.get("content"))
|
||||
if estimated is None:
|
||||
estimated = _estimate_text_tokens(choice.get("text"))
|
||||
if estimated is not None:
|
||||
observed += estimated
|
||||
elif choice:
|
||||
observed += 1
|
||||
return observed
|
||||
|
||||
|
||||
def _observed_stream_tokens(payload: dict) -> int:
|
||||
choices = payload.get("choices")
|
||||
if not isinstance(choices, list):
|
||||
@@ -1881,7 +1909,8 @@ class _TrackerHandler(http.server.BaseHTTPRequestHandler):
|
||||
"code": "missing_token_limit",
|
||||
}})
|
||||
return
|
||||
estimated_charge = server.billing.price_for(model) * token_limit / 1000.0
|
||||
total_token_bound = _request_total_token_upper_bound(body) or token_limit
|
||||
estimated_charge = server.billing.price_for(model) * total_token_bound / 1000.0
|
||||
if estimated_charge > server.max_charge_per_request:
|
||||
self._send_json(402, {"error": {
|
||||
"message": (
|
||||
|
||||
Reference in New Issue
Block a user