Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(anthropic): say why an admin api key failed validation (#118402)

Tom Owerse18cc86fc645
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/anthropic/anthropic.py b/products/warehouse_sources/backend/temporal/data_imports/sources/anthropic/anthropic.py
index d249afaef6bd..ce69085f16da 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/anthropic/anthropic.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/anthropic/anthropic.py
@@ -253,17 +253,36 @@ def _format_rfc3339(value: Any) -> str:
return dt.strftime("%Y-%m-%dT%H:%M:%SZ")
-def validate_credentials(api_key: str) -> bool:
+INVALID_ADMIN_KEY_MESSAGE = (
+ "Your Anthropic Admin API key is invalid or has been revoked. Create a new Admin API key in the "
+ "Anthropic Console, then reconnect."
+)
+STANDARD_KEY_MESSAGE = (
+ "This is a standard Anthropic API key. Create an Admin API key (prefixed sk-ant-admin) in the "
+ "Anthropic Console, then reconnect."
+)
+VALIDATION_UNREACHABLE_MESSAGE = (
+ "PostHog couldn't reach Anthropic to check your Admin API key. Wait a few minutes, then try again."
+)
+
+
+def validate_credentials(api_key: str) -> tuple[bool, Optional[str]]:
# A single cheap probe against the smallest list endpoint confirms the admin key is genuine.
# 200 => valid. 403 => valid key without a scope we probed here; still a real key, so accept it
# at create time (sync-time 403s are caught by get_non_retryable_errors). 401 => bad key.
- ok, _status = validate_via_probe(
+ ok, status = validate_via_probe(
lambda: make_tracked_session(redact_values=(api_key,)),
f"{ANTHROPIC_BASE_URL}/v1/organizations/users?limit=1",
headers={"x-api-key": api_key, **_version_headers()},
ok_statuses=(200, 403),
)
- return ok
+ if ok:
+ return True, None
+ if status is None or status == 429 or status >= 500:
+ return False, VALIDATION_UNREACHABLE_MESSAGE
+ if api_key.strip().startswith("sk-ant-api"):
+ return False, STANDARD_KEY_MESSAGE
+ return False, INVALID_ADMIN_KEY_MESSAGE
def _check_path_access(api_key: str, path: str, missing_reason: str) -> Optional[str]:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/anthropic/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/anthropic/source.py
index 8402f28814bd..108cae03e3ad 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/anthropic/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/anthropic/source.py
@@ -243,10 +243,7 @@ The group, group membership and custom role tables come from the Claude Enterpri
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
- if validate_anthropic_credentials(config.api_key):
- return True, None
-
- return False, "Invalid Anthropic Admin API key"
+ return validate_anthropic_credentials(config.api_key)
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[AnthropicResumeConfig]:
return ResumableSourceManager[AnthropicResumeConfig](inputs, AnthropicResumeConfig)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/anthropic/tests/test_anthropic.py b/products/warehouse_sources/backend/temporal/data_imports/sources/anthropic/tests/test_anthropic.py
index ea7fd6b86bf8..0174e65f2395 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/anthropic/tests/test_anthropic.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/anthropic/tests/test_anthropic.py
@@ -9,6 +9,9 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.common.s
)
from products.warehouse_sources.backend.temporal.data_imports.sources.anthropic.anthropic import (
ANALYTICS_ACCESS_MISSING,
+ INVALID_ADMIN_KEY_MESSAGE,
+ STANDARD_KEY_MESSAGE,
+ VALIDATION_UNREACHABLE_MESSAGE,
AnthropicResumeConfig,
_analytics_windows,
_claude_code_start_day,
@@ -231,11 +234,19 @@ class TestUsageReportGroupByFallback:
class TestValidateCredentials:
- @parameterized.expand([("ok", 200, True), ("forbidden_scope", 403, True), ("unauthorized", 401, False)])
- def test_status_mapping(self, _name: str, status: int, expected: bool) -> None:
+ @parameterized.expand(
+ [
+ ("ok", "sk-ant-admin-test", 200, (True, None)),
+ ("forbidden_scope", "sk-ant-admin-test", 403, (True, None)),
+ ("unauthorized", "sk-ant-admin-test", 401, (False, INVALID_ADMIN_KEY_MESSAGE)),
+ ("standard_key", "sk-ant-api03-test", 401, (False, STANDARD_KEY_MESSAGE)),
+ ("server_error", "sk-ant-admin-test", 503, (False, VALIDATION_UNREACHABLE_MESSAGE)),
+ ]
+ )
+ def test_status_mapping(self, _name: str, api_key: str, status: int, expected: tuple[bool, str | None]) -> None:
# 403 is accepted at create time (real key, unprobed scope); 401 means a bad key.
with scripted_network([ScriptedResponse(status=status)]) as network:
- assert validate_credentials("sk-ant-admin-test") is expected
+ assert validate_credentials(api_key) == expected
assert network.requests_log[0].path == "/v1/organizations/users"
assert network.requests_log[0].param("limit") == "1"