: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from collections import defaultdict
from typing import Optional, cast
from products.warehouse_sources.sources.anthropic._config import AnthropicSourceConfig
from products.warehouse_sources.sources.anthropic.anthropic import (
ANTHROPIC_VERSION_2023_06_01,
ANTHROPIC_VERSION_2026_10_21,
AnthropicResumeConfig,
anthropic_source,
check_analytics_access,
check_rbac_group_access,
check_rbac_role_access,
validate_credentials as validate_anthropic_credentials,
)
from products.warehouse_sources.sources.anthropic.settings import (
ANALYTICS_PATH_PREFIX,
ANTHROPIC_ENDPOINTS,
ENDPOINT_RETIRED_ERROR,
ENDPOINTS,
INCREMENTAL_FIELDS,
RBAC_GROUPS_PATH,
RBAC_ROLES_PATH,
AccessFamily,
)
from products.warehouse_sources.sources.sdk import (
CanonicalDescriptions,
DataWarehouseSourceCategory,
ExternalDataSourceType,
FieldType,
ReleaseStatus,
ResumableSource,
ResumableSourceManager,
SourceConfig,
SourceFieldInputConfig,
SourceFieldInputConfigType,
SourceInputs,
SourceRegistry,
SourceResponse,
SourceSchema,
detect,
)
@SourceRegistry.register
class AnthropicSource(ResumableSource[AnthropicSourceConfig, AnthropicResumeConfig]):
codebase_detection = detect(
env=(
"^ANTHROPIC_",
"^CLAUDE_CODE_OAUTH_TOKEN$",
"^CLAUDE_CODE_USE_BEDROCK$",
"^CLAUDE_CODE_USE_VERTEX$",
"^CLAUDE_CODE_USE_FOUNDRY$",
"^CLAUDE_CODE_USE_ANTHROPIC_AWS$",
"^CLAUDE_CODE_USE_MANTLE$",
),
npm=(
"@anthropic-ai/sdk",
"@anthropic-ai/bedrock-sdk",
"@anthropic-ai/vertex-sdk",
"@anthropic-ai/foundry-sdk",
"@anthropic-ai/aws-sdk",
"@anthropic-ai/google-cloud-sdk",
"@anthropic-ai/claude-agent-sdk",
"@anthropic-ai/claude-code",
"@anthropic-ai/tokenizer",
"@ai-sdk/anthropic",
"@ai-sdk/anthropic-aws",
"@langchain/anthropic",
"@llamaindex/anthropic",
"@tanstack/ai-anthropic",
"@effect/ai-anthropic",
"@genkit-ai/anthropic",
),
pypi=(
"anthropic",
"claude-agent-sdk",
"claude-code-sdk",
"anthropic-bedrock",
"langchain-anthropic",
"llama-index-llms-anthropic",
),
composer=("anthropic-ai/sdk", "mozex/anthropic-php", "mozex/anthropic-laravel"),
go=(
"github.com/anthropics/anthropic-sdk-go",
"github.com/anthropics/anthropic-cli",
"github.com/liushuangls/go-anthropic",
),
hex=("anthropix",),
maven=(
"com.anthropic:anthropic-java",
"com.anthropic:anthropic-java-core",
"com.anthropic:anthropic-java-client-okhttp",
"com.anthropic:anthropic-java-bedrock",
"com.anthropic:anthropic-java-vertex",
"com.anthropic:anthropic-java-aws",
"com.anthropic:anthropic-java-foundry",
"com.anthropic:anthropic-java-google-cloud",
"com.anthropic:anthropic-java-mcp",
"org.springframework.ai:spring-ai-anthropic",
"org.springframework.ai:spring-ai-starter-model-anthropic",
"org.springframework.ai:spring-ai-anthropic-spring-boot-starter",
"dev.langchain4j:langchain4j-anthropic",
),
pub=("anthropic_sdk_dart",),
rubygems=("anthropic", "ruby-anthropic"),
swiftpm=("github.com/anthropics/claudeforfoundationmodels", "github.com/jamesrochabrun/swiftanthropic"),
)
lists_tables_without_credentials = True # static endpoint catalog — safe for public docs
supported_versions = (ANTHROPIC_VERSION_2023_06_01, ANTHROPIC_VERSION_2026_10_21)
default_version = ANTHROPIC_VERSION_2026_10_21
api_docs_url = "https://platform.claude.com/docs/en/api/versioning"
@property
def source_type(self) -> ExternalDataSourceType:
return ExternalDataSourceType.ANTHROPIC
@property
def get_source_config(self) -> SourceConfig:
return SourceConfig(
name=ExternalDataSourceType.ANTHROPIC,
category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
label="Anthropic",
releaseStatus=ReleaseStatus.GA,
caption="""Enter your Anthropic Admin API key to pull your organization's Claude usage, cost, and admin data into the PostHog Data warehouse.
Create an Admin API key (prefixed `sk-ant-admin...`) in your [Anthropic Console](https://console.anthropic.com/settings/admin-keys). Only organization admins can create one, and the Admin API is not available for individual accounts.
Some tables need a Claude Enterprise key instead, created by your primary owner in [claude.ai organization settings](https://claude.ai/admin-settings/api-access). A key works with one API only, so pick the one that matches the tables you want.
The per-seat activity, cost and token usage tables, and the connector, plugin, skill and summary tables, come from the Claude Enterprise Analytics API. They need a Claude Enterprise key carrying the `read:analytics` scope.
The group, group membership and custom role tables come from the Claude Enterprise user management API. They need an Admin API key carrying the `read:rbac_groups` scope for the group tables, or `read:members` for the custom role tables.""",
iconPath="/static/services/anthropic.svg",
docsUrl="https://posthog.com/docs/cdp/sources/anthropic",
keywords=["llm", "claude", "ai usage", "cost"],
fields=cast(
list[FieldType],
[
SourceFieldInputConfig(
name="api_key",
label="Admin API key",
type=SourceFieldInputConfigType.PASSWORD,
required=True,
placeholder="sk-ant-admin...",
secret=True,
),
],
),
)
def get_canonical_descriptions(self) -> CanonicalDescriptions:
from products.warehouse_sources.sources.anthropic.canonical_descriptions import CANONICAL_DESCRIPTIONS
return CANONICAL_DESCRIPTIONS
def get_retry_exhausted_errors(self) -> dict[str, str]:
# The report endpoints share one organization rate limit, and Anthropic can ask for a wait of
# up to an hour once it is spent. Every client of the organization draws on that limit, so
# the action the customer has is to stop the other tools that read the same reports.
return {
"HTTP 429 for https://api.anthropic.com": (
"Your organization's Anthropic report rate limit ran out, and PostHog retries on its own. "
"Pause other tools that read your usage or cost reports to free it up."
),
}
def get_non_retryable_errors(self) -> dict[str, str | None]:
rbac_group_denied = "This table comes from the Claude Enterprise user management API, which your key can't reach. Reconnect the source with an Admin API key created in claude.ai for all your linked organizations, carrying the read:rbac_groups scope, or turn this table's sync off."
rbac_role_denied = "This table comes from the Claude Enterprise user management API, which your key can't reach. Reconnect the source with an Admin API key created in claude.ai carrying the read:members scope, or turn this table's sync off."
return {
# Matched before the generic 403 below, because the first matching pattern supplies the
# message and a denial from this path is about a missing scope, not admin access.
f"403 Client Error: Forbidden for url: https://api.anthropic.com{ANALYTICS_PATH_PREFIX}": "This table comes from the Claude Enterprise Analytics API, which your key can't reach. Reconnect the source with a Claude Enterprise key carrying the read:analytics scope, or turn this table's sync off.",
# An organization that is not on Claude Enterprise does not serve these routes at all,
# so a 404 here means the same thing as a 403 and must not retry either.
f"403 Client Error: Forbidden for url: https://api.anthropic.com{RBAC_GROUPS_PATH}": rbac_group_denied,
f"404 Client Error: Not Found for url: https://api.anthropic.com{RBAC_GROUPS_PATH}": rbac_group_denied,
f"403 Client Error: Forbidden for url: https://api.anthropic.com{RBAC_ROLES_PATH}": rbac_role_denied,
f"404 Client Error: Not Found for url: https://api.anthropic.com{RBAC_ROLES_PATH}": rbac_role_denied,
"401 Client Error: Unauthorized for url: https://api.anthropic.com": "Your Anthropic Admin API key is invalid or has been revoked. Create a new Admin API key in the Anthropic Console, then reconnect.",
"403 Client Error: Forbidden for url: https://api.anthropic.com": "Your Anthropic API key does not have organization admin access. Use an Admin API key (prefixed sk-ant-admin) created by an organization admin, then reconnect.",
ENDPOINT_RETIRED_ERROR: "Anthropic no longer offers this table, so it can't sync. PostHog has turned its sync off for you, and any rows already imported stay in your warehouse.",
}
def get_schemas(
self,
config: AnthropicSourceConfig,
team_id: int,
with_counts: bool = False,
names: list[str] | None = None,
force_refresh: bool = False,
api_version: str | None = None,
) -> list[SourceSchema]:
def _build_schema(endpoint: str) -> SourceSchema:
endpoint_config = ANTHROPIC_ENDPOINTS[endpoint]
return SourceSchema(
name=endpoint,
supports_incremental=endpoint_config.supports_incremental,
supports_append=endpoint_config.supports_append,
incremental_fields=INCREMENTAL_FIELDS.get(endpoint, []),
should_sync_default=endpoint_config.should_sync_default,
default_incremental_lookback_seconds=endpoint_config.default_incremental_lookback_seconds,
)
schemas = [_build_schema(endpoint) for endpoint in ENDPOINTS]
if names is not None:
names_set = set(names)
schemas = [s for s in schemas if s.name in names_set]
return schemas
def get_endpoint_permissions(
self,
config: AnthropicSourceConfig,
team_id: int,
endpoints: list[str],
api_version: str | None = None,
) -> dict[str, str | None]:
probes = {
AccessFamily.ANALYTICS: check_analytics_access,
AccessFamily.RBAC_GROUPS: check_rbac_group_access,
AccessFamily.RBAC_ROLES: check_rbac_role_access,
}
resolved_version = self.resolve_api_version(api_version)
permissions: dict[str, str | None] = dict.fromkeys(endpoints)
by_family: dict[AccessFamily, list[str]] = defaultdict(list)
for name in endpoints:
endpoint_config = ANTHROPIC_ENDPOINTS.get(name)
if endpoint_config is not None and endpoint_config.access_family is not None:
by_family[endpoint_config.access_family].append(name)
for family, names in by_family.items():
# Every endpoint in a family needs the same access, so one probe answers for all of them.
reason = probes[family](config.api_key, resolved_version)
for name in names:
permissions[name] = reason
return permissions
def validate_credentials(
self,
config: AnthropicSourceConfig,
team_id: int,
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
return validate_anthropic_credentials(config.api_key, self.resolve_api_version(api_version))
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[AnthropicResumeConfig]:
return ResumableSourceManager[AnthropicResumeConfig](inputs, AnthropicResumeConfig)
def source_for_pipeline(
self,
config: AnthropicSourceConfig,
resumable_source_manager: ResumableSourceManager[AnthropicResumeConfig],
inputs: SourceInputs,
) -> SourceResponse:
return anthropic_source(
api_key=config.api_key,
endpoint=inputs.schema_name,
team_id=inputs.team_id,
job_id=inputs.job_id,
resumable_source_manager=resumable_source_manager,
api_version=self.resolve_api_version(inputs.api_version),
db_incremental_field_last_value=inputs.db_incremental_field_last_value
if inputs.should_use_incremental_field
else None,
)