Skip to content

The agent that grows with you

feat(telemetry): count provider fallback activations in shared metrics

teknium117ecd501ad42
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index 06f340ae47b..e30572fec47 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -2181,6 +2181,8 @@ def try_activate_fallback(agent, reason: "FailoverReason | None" = None, reset_a
agent._provider_fallback_active = True
agent._provider_fallback_route = (str(fb_model), str(fb_provider))
_log_fallback_activated(agent, reason, old_model, old_provider, fb_model, fb_provider)
+ from hermes_cli.observability.shared_metrics_events import record_fallback
+ record_fallback(from_provider=old_provider, to_provider=fb_provider, reason=reason)
# The stale-call streak measured the OLD provider; carrying it over would
# short-circuit the fresh fallback before its first stream attempt.
_reset_stale_streak(agent)
diff --git a/tests/agent/test_provider_fallback.py b/tests/agent/test_provider_fallback.py
index 290b56cf0e6..af83fcf114f 100644
--- a/tests/agent/test_provider_fallback.py
+++ b/tests/agent/test_provider_fallback.py
@@ -133,6 +133,24 @@ class TestFallbackChainAdvancement:
assert agent._fallback_index == 2
assert agent._rate_limit_backoff_count == 1
+ def test_activation_counts_once_with_classifier_reason(self, monkeypatch):
+ """A skipped candidate is not an activation; the one that binds records its FailoverReason."""
+ from hermes_cli.observability import shared_metrics_events
+ from hermes_cli.observability.shared_metrics_fields import fallback_fields
+
+ calls = []
+ monkeypatch.setattr(shared_metrics_events, "record_fallback", lambda **kw: calls.append(kw))
+ agent = _make_agent(fallback_model=[
+ {"provider": "broken", "model": "nope"}, {"provider": "zai", "model": "glm-5.2"},
+ ])
+ agent.provider = "openai-codex"
+ with patch("agent.auxiliary_client.resolve_provider_client",
+ side_effect=[(None, None), (_mock_client(base_url="https://api.z.ai/v1"), "glm-5.2")]):
+ assert agent._try_activate_fallback(FailoverReason.rate_limit) is True
+
+ assert [(kw["from_provider"], kw["to_provider"]) for kw in calls] == [("openai-codex", "zai")]
+ assert fallback_fields(**calls[0])["error_class"] == "rate_limit"
+
def test_skips_provider_that_raises_to_next(self):
"""If resolve_provider_client raises, skip to next in chain."""
fbs = [