Skip to content

The agent that grows with you

fix(models): discover Anthropic pool API keys

Kevin Yin8ede2e147278
diff --git a/hermes_cli/models.py b/hermes_cli/models.py
index 0486c25fbc3..7107f7109f5 100644
--- a/hermes_cli/models.py
+++ b/hermes_cli/models.py
@@ -4519,6 +4519,36 @@ def clear_provider_models_cache(provider: Optional[str] = None) -> None:
pass
+def _resolve_anthropic_pool_catalog_credentials() -> tuple[str, str]:
+ """Return a read-only API-key pool credential for model discovery.
+
+ ``resolve_anthropic_token()`` intentionally ignores ``api_key`` pool
+ entries because its runtime contract is OAuth-oriented. The model catalog
+ supports regular ``x-api-key`` auth, so it needs a narrow fallback that
+ preserves the credential's configured endpoint instead of sending a
+ proxy-scoped key to Anthropic's public host.
+ """
+ try:
+ from agent.credential_pool import AUTH_TYPE_API_KEY
+ from hermes_cli.auth import read_credential_pool
+
+ for entry in read_credential_pool("anthropic"):
+ if not isinstance(entry, dict):
+ continue
+ if entry.get("auth_type") != AUTH_TYPE_API_KEY:
+ continue
+ token = str(entry.get("access_token") or "").strip()
+ if not token:
+ continue
+ endpoint = str(
+ entry.get("base_url") or entry.get("inference_base_url") or ""
+ ).strip()
+ return token, endpoint
+ except Exception:
+ pass
+ return "", ""
+
+
def _fetch_anthropic_models(
timeout: float = 5.0,
*,
@@ -4527,8 +4557,9 @@ def _fetch_anthropic_models(
) -> Optional[list[str]]:
"""Fetch available models from the Anthropic /v1/models endpoint.
- Uses resolve_anthropic_token() to find credentials (env vars or
- Claude Code auto-discovery) unless api_key is provided explicitly.
+ Uses resolve_anthropic_token() to find credentials (env vars, OAuth,
+ or Claude Code auto-discovery) unless api_key is provided explicitly. If
+ those sources are empty, a read-only API-key credential_pool entry is used.
Returns sorted model IDs or None.
"""
try:
@@ -4536,7 +4567,12 @@ def _fetch_anthropic_models(
except ImportError:
return None
+ resolved_base_url = base_url
token = (api_key or "").strip() or resolve_anthropic_token()
+ if not token:
+ token, pool_base_url = _resolve_anthropic_pool_catalog_credentials()
+ if not resolved_base_url and pool_base_url:
+ resolved_base_url = pool_base_url
if not token:
return None
@@ -4551,7 +4587,7 @@ def _fetch_anthropic_models(
def _do_request(h: dict[str, str]):
req = urllib.request.Request(
- _anthropic_models_url(base_url),
+ _anthropic_models_url(resolved_base_url),
headers=h,
)
with _urlopen_model_catalog_request(req, timeout=timeout) as resp:
diff --git a/tests/hermes_cli/test_anthropic_pool_model_discovery.py b/tests/hermes_cli/test_anthropic_pool_model_discovery.py
new file mode 100644
index 00000000000..d36c7d69eeb
--- /dev/null
+++ b/tests/hermes_cli/test_anthropic_pool_model_discovery.py
@@ -0,0 +1,86 @@
+from __future__ import annotations
+
+import json
+
+from hermes_cli import models
+
+
+class _Response:
+ def __init__(self, payload: dict):
+ self._payload = payload
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *_args):
+ return False
+
+ def read(self) -> bytes:
+ return json.dumps(self._payload).encode()
+
+
+def test_anthropic_picker_discovers_models_with_pool_api_key(monkeypatch):
+ """A direct API key stored only in auth.json must reach /v1/models."""
+ monkeypatch.setattr(models, "_get_model_config_dict", lambda: {"provider": "nous"})
+ monkeypatch.setattr(
+ "agent.anthropic_adapter.resolve_anthropic_token",
+ lambda: None,
+ )
+ monkeypatch.setattr(
+ "hermes_cli.auth.read_credential_pool",
+ lambda provider: [
+ {
+ "auth_type": "api_key",
+ "access_token": "sk-ant-api03-pool-key",
+ "base_url": "https://api.anthropic.com",
+ }
+ ] if provider == "anthropic" else [],
+ )
+
+ captured = {}
+
+ def _open(request, *, timeout):
+ captured["url"] = request.full_url
+ captured["headers"] = {key.lower(): value for key, value in request.header_items()}
+ captured["timeout"] = timeout
+ return _Response({"data": [{"id": "claude-opus-5"}]})
+
+ monkeypatch.setattr(models, "_urlopen_model_catalog_request", _open)
+
+ result = models.provider_model_ids("anthropic")
+
+ assert "claude-opus-5" in result
+ assert captured["url"] == "https://api.anthropic.com/v1/models"
+ assert captured["headers"]["x-api-key"] == "sk-ant-api03-pool-key"
+ assert "authorization" not in captured["headers"]
+
+
+def test_anthropic_pool_api_key_uses_its_configured_endpoint(monkeypatch):
+ """Never send a proxy-scoped pool credential to Anthropic's public host."""
+ monkeypatch.setattr(
+ "agent.anthropic_adapter.resolve_anthropic_token",
+ lambda: None,
+ )
+ monkeypatch.setattr(
+ "hermes_cli.auth.read_credential_pool",
+ lambda _provider: [
+ {
+ "auth_type": "api_key",
+ "access_token": "proxy-key",
+ "base_url": "https://proxy.example/anthropic/v1",
+ }
+ ],
+ )
+
+ captured = {}
+
+ def _open(request, *, timeout):
+ captured["url"] = request.full_url
+ captured["headers"] = {key.lower(): value for key, value in request.header_items()}
+ return _Response({"data": [{"id": "claude-proxy-model"}]})
+
+ monkeypatch.setattr(models, "_urlopen_model_catalog_request", _open)
+
+ assert models._fetch_anthropic_models() == ["claude-proxy-model"]
+ assert captured["url"] == "https://proxy.example/anthropic/v1/models"
+ assert captured["headers"]["x-api-key"] == "proxy-key"