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.

3.7 KB
Raw Blame
from dataclasses import dataclass, field

from products.warehouse_sources.sources.sdk import IncrementalField, IncrementalFieldType

# Helicone runs regional deployments. An API key belongs to a single org in one region, so the
# host is chosen by the `region` form field rather than a user-supplied URL (no SSRF surface —
# the set is fixed).
HELICONE_HOSTS: dict[str, str] = {
    "us": "https://api.helicone.ai",
    "eu": "https://eu.api.helicone.ai",
}

REQUESTS_ENDPOINT = "requests"
SESSIONS_ENDPOINT = "sessions"
USERS_ENDPOINT = "users"
PROMPTS_ENDPOINT = "prompts"
PROPERTIES_ENDPOINT = "properties"
EVAL_SCORES_ENDPOINT = "eval_scores"

# /v1/request/query-clickhouse is Helicone's bulk-export endpoint; their export CLI pages it with
# limit/offset up to 10k rows per request. Rows can embed full request/response bodies, so we keep
# pages an order of magnitude smaller to bound response sizes.
REQUESTS_PAGE_SIZE = 1000
SESSIONS_PAGE_SIZE = 1000
PROMPTS_PAGE_SIZE = 100

# Bound the first incremental sync of the request log to a recent window so an initial backfill of
# a high-volume org doesn't attempt to page through years of history. Subsequent incremental syncs
# only fetch rows newer than the stored cursor.
REQUESTS_DEFAULT_LOOKBACK_DAYS = 365


@dataclass(frozen=False)
class HeliconeEndpointConfig:
    name: str
    path: str
    primary_keys: list[str]
    supports_incremental: bool = False
    supports_append: bool = False
    incremental_fields: list[IncrementalField] = field(default_factory=list)
    # Must be a STABLE datetime field (never an update-tracking one) so partitions don't rewrite
    # on every sync.
    partition_key: str | None = None


HELICONE_ENDPOINTS: dict[str, HeliconeEndpointConfig] = {
    REQUESTS_ENDPOINT: HeliconeEndpointConfig(
        name=REQUESTS_ENDPOINT,
        path="/v1/request/query-clickhouse",
        primary_keys=["request_id"],
        supports_incremental=True,
        supports_append=True,
        # The request log is append-only, so the creation timestamp is a reliable server-side
        # cursor (the filter AST supports gte on request_created_at).
        incremental_fields=[
            {
                "label": "request_created_at",
                "type": IncrementalFieldType.DateTime,
                "field": "request_created_at",
                "field_type": IncrementalFieldType.DateTime,
            },
        ],
        partition_key="request_created_at",
    ),
    # Sessions, users, and prompts are aggregates/metadata whose rows change in place (a session
    # accrues requests, a user accrues cost) with no server-side "changed since" filter, so they
    # are full-refresh only.
    SESSIONS_ENDPOINT: HeliconeEndpointConfig(
        name=SESSIONS_ENDPOINT,
        path="/v1/session/query",
        primary_keys=["session_id"],
    ),
    USERS_ENDPOINT: HeliconeEndpointConfig(
        name=USERS_ENDPOINT,
        path="/v1/user/query",
        primary_keys=["user_id"],
    ),
    PROMPTS_ENDPOINT: HeliconeEndpointConfig(
        name=PROMPTS_ENDPOINT,
        path="/v1/prompt-2025/query",
        primary_keys=["id"],
    ),
    # Org-wide catalogs of the custom property keys and eval score names seen on requests. The
    # per-request values already ride on the requests table (`properties`, `scores`).
    PROPERTIES_ENDPOINT: HeliconeEndpointConfig(
        name=PROPERTIES_ENDPOINT,
        path="/v1/property/query",
        primary_keys=["property"],
    ),
    EVAL_SCORES_ENDPOINT: HeliconeEndpointConfig(
        name=EVAL_SCORES_ENDPOINT,
        path="/v1/evals/scores",
        primary_keys=["score"],
    ),
}

ENDPOINTS = tuple(HELICONE_ENDPOINTS.keys())

INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {
    name: config.incremental_fields for name, config in HELICONE_ENDPOINTS.items()
}