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.

10.5 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.openrouter._config import OpenRouterSourceConfig
from products.warehouse_sources.sources.openrouter.openrouter import (
    OpenRouterResumeConfig,
    get_key_info,
    openrouter_source,
)
from products.warehouse_sources.sources.openrouter.settings import ENDPOINTS, INCREMENTAL_FIELDS, OPENROUTER_ENDPOINTS
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    detect,
)

_MANAGEMENT_KEY_REQUIRED = (
    "Requires an OpenRouter management API key. This key is a regular inference key, which can read only "
    "the models, model endpoints, providers, task classifications, session costs, and batches tables."
)

_INFERENCE_KEY_REQUIRED = (
    "Requires a regular OpenRouter API key. Batches belong to the workspace of the key that reads them, "
    "and a management key isn't tied to a workspace."
)

# The organization members, workspaces and end users endpoints (and the workspace members fan-out,
# which lists workspaces first) resolve only when the management key's account belongs to an
# OpenRouter organization; an account without one gets a 404. Retrying can't create an
# organization, so we stop and tell the customer.
_NO_ORGANIZATION = (
    "Your OpenRouter account isn't part of an organization, so this table can't sync. To sync it, "
    "reconnect with a management key that belongs to an organization."
)


@SourceRegistry.register
class OpenRouterSource(ResumableSource[OpenRouterSourceConfig, OpenRouterResumeConfig]):
    codebase_detection = detect(
        env=("^OPENROUTER_", "^OPENROUTER_API_KEY$"),
        npm=("@openrouter/ai-sdk-provider", "@openrouter/sdk"),
        pypi=("openrouter",),
    )

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs

    api_docs_url = "https://openrouter.ai/docs/api-reference"

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.OPENROUTER

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.OPENROUTER,
            category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
            label="OpenRouter",
            releaseStatus=ReleaseStatus.GA,
            caption="""Enter your OpenRouter API key to pull your OpenRouter usage and account data into the PostHog Data warehouse.

Use a **management API key** (create one under [Settings -> Management Keys](https://openrouter.ai/settings/keys)) so the activity, API keys, credits, end users, organization members, workspaces, and workspace members tables can sync. A regular inference key can read only the models, model endpoints, providers, task classifications, session costs, and batches tables.

A management key can't read batches. To sync them, connect a second OpenRouter source with a regular API key.""",
            iconPath="/static/services/openrouter.svg",
            docsUrl="https://posthog.com/docs/cdp/sources/openrouter",
            keywords=["llm", "ai gateway", "usage", "inference"],
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="sk-or-...",
                        secret=True,
                    ),
                ],
            ),
        )

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.openrouter.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            # A bad or revoked key surfaces as an HTTPError from `raise_for_status()`. Match the stable
            # status text and base host, not the per-request path/query.
            "401 Client Error: Unauthorized for url: https://openrouter.ai": "Your OpenRouter API key is invalid or has been revoked. Create a new key in your OpenRouter dashboard, then reconnect.",
            "403 Client Error: Forbidden for url: https://openrouter.ai": "Your OpenRouter API key is missing the management scope needed to sync this data. Use a management API key (Settings -> Management Keys), then reconnect.",
            # Match the org-scoped paths specifically, not a bare host 404, so a genuine bad path on
            # another endpoint still surfaces instead of being silently disabled. The query string is
            # dropped as the volatile part.
            "404 Client Error: Not Found for url: https://openrouter.ai/api/v1/organization/members": _NO_ORGANIZATION,
            "404 Client Error: Not Found for url: https://openrouter.ai/api/v1/workspaces": _NO_ORGANIZATION,
            # OpenRouter forwards `/end-users` to `/external-users`, which lists the organization's
            # registrations, so the same missing organization 404s here.
            "404 Client Error: Not Found for url: https://openrouter.ai/api/v1/end-users": _NO_ORGANIZATION,
        }

    def get_schemas(
        self,
        config: OpenRouterSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        def _description(endpoint: str) -> str | None:
            if endpoint == "activity":
                return "Daily usage rollups; only the last 30 completed UTC days are available"
            if endpoint == "batches":
                return "Batch jobs in the API key's workspace; needs a regular API key, not a management key"
            if endpoint == "task_classifications":
                return (
                    "Share of all OpenRouter traffic by task type over the last 7 days, across every OpenRouter account"
                )
            if endpoint == "session_costs":
                return "Median cost per session by agent harness and model, across every OpenRouter account, refreshed weekly"
            return None

        def _build_schema(endpoint: str) -> SourceSchema:
            has_incremental = len(INCREMENTAL_FIELDS.get(endpoint, [])) > 0
            return SourceSchema(
                name=endpoint,
                supports_incremental=has_incremental,
                # /activity re-fetches the (possibly partial) watermark day each run, so it relies on
                # merge to dedupe — append would materialize duplicates. Every other table is small
                # and full-refresh only.
                supports_append=False,
                incremental_fields=INCREMENTAL_FIELDS.get(endpoint, []),
                should_sync_default=OPENROUTER_ENDPOINTS[endpoint].should_sync_default,
                description=_description(endpoint),
            )

        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 validate_credentials(
        self,
        config: OpenRouterSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        info = get_key_info(config.api_key)
        if info is None:
            return False, "Invalid OpenRouter API key. Create a key in your OpenRouter dashboard and try again."

        # At source-create (no schema_name) any genuine key is accepted — the catalog tables sync with
        # an inference key, and get_endpoint_permissions reports which tables need a management key.
        # When validating a specific management table, require the management scope.
        if schema_name is not None:
            endpoint = OPENROUTER_ENDPOINTS.get(schema_name)
            if endpoint is not None and endpoint.requires_management_key and not info.get("is_management_key"):
                return False, f"The '{schema_name}' table requires an OpenRouter management API key."
            if endpoint is not None and endpoint.requires_inference_key and info.get("is_management_key"):
                return False, f"The '{schema_name}' table requires a regular OpenRouter API key, not a management key."

        return True, None

    def get_endpoint_permissions(
        self, config: OpenRouterSourceConfig, team_id: int, endpoints: list[str], api_version: str | None = None
    ) -> dict[str, str | None]:
        info = get_key_info(config.api_key)
        # A None here is a transient /key failure, not a denial (the key was already validated at
        # create). Only a genuine 200 that reports a non-management key marks the management tables.
        if info is None:
            return dict.fromkeys(endpoints)

        is_management_key = bool(info.get("is_management_key"))

        result: dict[str, str | None] = {}
        for name in endpoints:
            endpoint = OPENROUTER_ENDPOINTS.get(name)
            if endpoint is not None and endpoint.requires_management_key and not is_management_key:
                result[name] = _MANAGEMENT_KEY_REQUIRED
            elif endpoint is not None and endpoint.requires_inference_key and is_management_key:
                result[name] = _INFERENCE_KEY_REQUIRED
            else:
                result[name] = None
        return result

    def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[OpenRouterResumeConfig]:
        return ResumableSourceManager[OpenRouterResumeConfig](inputs, OpenRouterResumeConfig)

    def source_for_pipeline(
        self,
        config: OpenRouterSourceConfig,
        resumable_source_manager: ResumableSourceManager[OpenRouterResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return openrouter_source(
            api_key=config.api_key,
            endpoint=inputs.schema_name,
            logger=inputs.logger,
            resumable_source_manager=resumable_source_manager,
            should_use_incremental_field=inputs.should_use_incremental_field,
            db_incremental_field_last_value=inputs.db_incremental_field_last_value
            if inputs.should_use_incremental_field
            else None,
        )