On this page
SDK Telemetry Event Schema
This is the shared contract every SDK telemetry implementation follows. The Rust, Python, and TypeScript SDKs all capture the same event names with the same properties so cross-SDK funnel analysis in PostHog works without drift. Implementers should be able to produce a valid PostHog capture payload for any event below by reading only this page.
The published, sidebar-linked version of this contract lives at
SDK telemetry event schema. The opt-in default and
distinct_id strategy quoted below come from the telemetry decision
record and the opt-in ADR (docs/decisions/sdk-telemetry-opt-in.md).
Reconciliation with PostHog Event Taxonomy brief
The four sdk.* funnel event names and property allow-lists are authoritative
in docs/analytics/events.md and the binding
contract at SDK telemetry (schema_version 2). This
page restates that contract; do not invent parallel milestone names.
Common properties
Every sdk.* funnel event carries these identity fields in addition to its
event-specific allow-listed properties. SDK capture bypasses the Rust
capture() allow-list, so SDK authors own conformance.
| Property | Type | Required | Description |
|---|---|---|---|
distinct_id | string | required | Stable per-developer id. See Distinct_id strategy. |
trace_id | string | required | Fresh UUID generated per emission. |
timestamp | string (ISO-8601 / RFC 3339 UTC) | required | When the event occurred. |
SDK onboarding funnel events
Each of the four events fires once per install. Property allow-lists match
docs/analytics/events.md.
sdk.initialized
| Property | Type | Required | Description |
|---|---|---|---|
language | string | required | rust | typescript | python |
version | string | required | SDK package version |
environment | string | required | prod | dev |
user_id | string | required | Carries distinct_id |
sdk.first_api_call
| Property | Type | Required | Description |
|---|---|---|---|
latency_ms | integer | required | Request round-trip in ms |
endpoint | string | required | Path only, query stripped |
response_status | integer | required | HTTP status code |
sdk.first_order_placed
| Property | Type | Required | Description |
|---|---|---|---|
success | boolean | required | Whether placement succeeded |
model_tier | string | required | Requested model / tier |
order_size | integer | required | Requested token quantity |
sdk.first_order_settled
| Property | Type | Required | Description |
|---|---|---|---|
end_to_end_latency_ms | integer | required | Placement → settlement in ms |
settlement_status | string | required | "success" or "failed" |
API call pattern event
api_call
Fires on every SDK HTTP call (after opt-in), success or failure. This event powers SDK API coverage and health/usage analysis.
| Property | Type | Required | Description |
|---|---|---|---|
endpoint | string | required | Path template, not the concrete URL. Path params are replaced with placeholders, e.g. /v1/sellers/{id}/asks. Never carries user data or query values. |
method | string | required | HTTP method, e.g. POST, GET, DELETE. |
response_time_ms | integer | required | Wall-clock time from request send to response received, in milliseconds. |
success | boolean | required | true when status_code is 2xx, else false. |
status_code | integer | required | HTTP status code returned, e.g. 200, 408, 502. |
The canonical endpoint path templates (aligned to the gateway API the SDKs
already call) are:
| Template | Methods |
|---|---|
/v1/chat/completions | POST |
/v1/sellers/register | POST |
/v1/sellers/{id}/asks | POST |
/v1/sellers/{id}/asks/{ask_id} | DELETE |
/v1/sellers/{id}/capacity | POST |
/v1/sellers/{id}/trades | GET |
Opt-in and disclosure
Quoted verbatim from the telemetry decision record
and the opt-in ADR (docs/decisions/sdk-telemetry-opt-in.md):
- Default: opt-in by default — telemetry is off unless the developer explicitly enables it.
- Constructor/config flag:
telemetry_enabled(Rust fieldtelemetry_enabled, Python kwargtelemetry_enabled, TypeScript fieldtelemetryEnabled). Default value:false. - Environment variable:
TOKEN_GATEWAY_TELEMETRY(1/trueenable,0/falsedisable). The env var wins when both it and the flag are set. - Disclosure surface: a telemetry section in each SDK README plus a single first-run log line emitted when telemetry initialises.
Reusable disclosure copy (SDK READMEs paste this with minimal variation):
This SDK can send anonymous usage telemetry (onboarding milestones and API call patterns) to help improve the AI Token Gateway. Telemetry is off by default. To enable it, set
telemetry_enabled: truewhen constructing the client, or set theTOKEN_GATEWAY_TELEMETRY=1environment variable. No API keys, request bodies, or response bodies are ever collected. See the SDK telemetry event schema for the full list of events and properties.
PostHog configuration
(This section fulfils the parent task's in-scope item 4 — the shared PostHog project API key and host configuration convention.)
All three SDKs read the PostHog project API key and host from the same
environment variables and expose the same client-config field, so telemetry
configuration is identical across languages. The canonical names below are
namespaced under TOKEN_GATEWAY_ to avoid colliding with a developer's own
PostHog environment variables in their application.
| Setting | Canonical env var | Default |
|---|---|---|
| PostHog project API key | TOKEN_GATEWAY_POSTHOG_API_KEY | none — telemetry stays off if unset even when telemetry_enabled is true |
| PostHog host endpoint | TOKEN_GATEWAY_POSTHOG_HOST | https://app.posthog.com |
Per-SDK client-config field for the PostHog project API key (each following its language's naming convention):
| SDK | Config field | Case convention |
|---|---|---|
| Rust | posthog_api_key | snake_case struct field |
| Python | posthog_api_key | snake_case kwarg |
| TypeScript | posthogApiKey | camelCase interface field |
Rules every SDK honours:
- The project API key must be configurable via the env var or the config field — it must never be hardcoded into SDK source.
- When a config field is provided it takes precedence over the env var; when
neither is set and
telemetry_enabledis true, the SDK logs the first-run disclosure but sends no events (it has no destination key). - The host defaults to
https://app.posthog.comand is overridable viaTOKEN_GATEWAY_POSTHOG_HOST.
Distinct_id strategy
Quoted verbatim from the telemetry decision record:
- Generation: a random UUIDv4 generated the first time telemetry initialises. Never derived from the API key or any secret.
- Persistence: written to
<config_dir>/token-gateway/telemetry-id, where<config_dir>is the OS user config directory. Overridable viaTOKEN_GATEWAY_TELEMETRY_DISTINCT_ID, which takes precedence over the file. - Fallback: if the config directory is unreadable or unwritable, generate a per-process ephemeral UUIDv4 and continue without failing the SDK call.
Contracts summary
Grep-checkable identifiers all three SDKs must use verbatim:
- Event names:
package_install_detected,api_key_generated,first_authenticated_request,first_successful_api_call,first_order_placement,api_call. - Common properties:
distinct_id,sdk_language,sdk_version,install_timestamp. api_callproperties:endpoint,method,response_time_ms,success,status_code.- Opt-in flag / env var:
telemetry_enabled/TOKEN_GATEWAY_TELEMETRY. - PostHog config env vars:
TOKEN_GATEWAY_POSTHOG_API_KEY,TOKEN_GATEWAY_POSTHOG_HOST. - Distinct_id override:
TOKEN_GATEWAY_TELEMETRY_DISTINCT_ID.