Skip to content

The agent that grows with you

fix(free-tier): the 402 sign-in refusal applies on the welcome host only

kshitijk4poorb6a7e591b669
diff --git a/agent/error_classifier.py b/agent/error_classifier.py
index 529b23ec5c9..0c47639e8a5 100644
--- a/agent/error_classifier.py
+++ b/agent/error_classifier.py
@@ -791,7 +791,7 @@ def _nous_welcome_tier(c: _Ctx) -> Optional[Verdict]:
The parsed refusal rides ``error_context`` so the terminal copy can say what happened.
"""
from hermes_cli.anon_auth import (
- WELCOME_TIER_GATE_REASONS, parse_welcome_refusal, welcome_route_refusal)
+ WELCOME_TIER_GATE_REASONS, parse_welcome_refusal, route_is_welcome_host, welcome_route_refusal)
status = c.status_code
if not c.anonymous:
# A named credential's fairshare 429 is an ordinary rate limit, whatever its body says. The
@@ -801,8 +801,9 @@ def _nous_welcome_tier(c: _Ctx) -> Optional[Verdict]:
return _v(_R.format_error, retryable=False, should_fallback=True,
error_context={"welcome_route": "named_on_welcome_host"})
return None
- if status == 402:
- # The free tier has no credits to top up: a payment wall on it is the tier refusing.
+ if status == 402 and route_is_welcome_host(c.base_url):
+ # The free tier has no credits to top up: a payment wall on it is the tier refusing. Off the welcome
+ # host a free-tier JWT keeps the ordinary 402 handling (the route heal, billing copy).
refusal = {"reason": "refused", "retry_after": 0, "alternates": [], "upgrade_url": ""}
return _v(_R.auth_permanent, retryable=False, should_fallback=True,
error_context={"welcome_refusal": refusal})
diff --git a/tests/agent/test_free_tier_rate_limit_class.py b/tests/agent/test_free_tier_rate_limit_class.py
index 6f21f5fd6bf..58a2dc44403 100644
--- a/tests/agent/test_free_tier_rate_limit_class.py
+++ b/tests/agent/test_free_tier_rate_limit_class.py
@@ -91,6 +91,14 @@ def test_named_account_same_response_is_not_a_free_tier_refusal(case_id, status,
assert "To sign in: /login." not in result["final_response"]
+def test_anonymous_402_off_the_welcome_host_keeps_ordinary_402_handling():
+ paid = "https://inference-api.nousresearch.com/v1"
+ error = _error(402, {"error": {"message": "payment required"}})
+ classified = classify_api_error(error, provider="nous", model=MODEL, base_url=paid, api_key=make_jwt())
+ assert "welcome_refusal" not in classified.error_context
+ assert classified.reason.value == "billing"
+
+
@pytest.mark.parametrize("remaining,reason,retry_after", [("750", "at_capacity", 4), ("0", "rate_limited", 2000)])
def test_header_wait_is_the_exhausted_bucket_reset_else_retry_after(remaining, reason, retry_after):
headers = {"Retry-After": "4", "x-ratelimit-remaining-requests-1h": remaining,