Linking OpenAI as a source

Let AI connect your sources for you

Skip the manual setup — run this in your project and the wizard auto-detects your databases and APIs and connects them to PostHog.

Learn more
PostHog Wizard hedgehog

Contents

The OpenAI connector syncs your organization's API usage, spend, and admin data into the PostHog Data Warehouse, so you can analyze your AI costs alongside your product data. With an optional project API key, you can also sync models, stored chat completions, Batch API jobs, and evaluation results.

Prerequisites

You need an OpenAI organization and an Admin API key. Only organization owners can create Admin API keys, and a regular project API key can't read organization usage or costs.

To sync models, chat completions, batches, and evals tables, you also need a project API key. OpenAI doesn't accept Admin API keys for these endpoints. The project API key only includes data from its project, and a restricted key needs read access to each table you want to sync.

Adding a data source

  1. In PostHog, go to the Sources tab of the data pipeline section.
  2. Click + New source and click Link next to this source.
  3. Enter your credentials (see Configuration below) and click Next.
  4. Select the tables you want to sync, choose a sync method and frequency, then click Import.

Once the syncs are complete, you can start querying this data in PostHog.

When linking OpenAI, you'll need:

  • Admin API key – create one in your OpenAI organization settings by clicking Create admin key. The key starts with sk-admin.
  • Project API key (optional) – create one in your OpenAI API keys dashboard. The key starts with sk-proj-. Enables syncing models, chat completions, batches, and evaluation data. Only includes data from the key's project.

Sync modes

Each table can be synced in one of several modes, depending on what the source supports:

  • Webhook (when available) – the source pushes changes to PostHog in real time. Fastest freshness, lowest ongoing cost, and the only mode that reliably captures updates and deletes.
  • Incremental – only new or updated rows are synced on each run, using a cursor field (such as an updated_at timestamp). Cheaper than a full refresh, but deletes aren't captured.
  • Append only – new rows are appended using a cursor field; existing rows are never updated. Ideal for immutable, append-only tables like event logs.
  • Full refresh – the whole table is reloaded on every sync. Use it when a table has no reliable cursor or when you need deletions reflected.

See sync methods for a full explanation of how each mode works and how to choose between them.

The usage and costs tables sync incrementally on start_time, and audit logs on effective_at. Each incremental sync re-reads the trailing day of usage and cost buckets, since OpenAI keeps restating a day's bucket as late usage lands; the overlap is deduplicated automatically.

Usage and cost rows are daily buckets broken down by project, API key, model, and the other dimensions OpenAI supports for each endpoint. The id column is a synthesized key derived from the bucket start and those dimensions.

The models, chat completions, batches, evals, eval runs, and eval run output items tables use full refresh sync because OpenAI's API doesn't support server-side time filtering for these endpoints.

Configuration

OptionTypeRequired
Admin API keypasswordYes
Project API key (optional)passwordNo

Supported tables

TableDescriptionSync methodIncremental fieldPrimary key
usage_completions

Chat and text completions token usage aggregated into daily buckets, broken down by project, user, API key, model, batch, and service tier.

Incremental, Full refreshstart_time—
usage_embeddings

Embeddings token usage aggregated into daily buckets, broken down by project, user, API key, and model.

Incremental, Full refreshstart_time—
usage_moderations

Moderations token usage aggregated into daily buckets, broken down by project, user, API key, and model.

Incremental, Full refreshstart_time—
usage_images

Image generation usage aggregated into daily buckets, broken down by project, user, API key, model, image size, and source.

Incremental, Full refreshstart_time—
usage_audio_speeches

Text-to-speech usage aggregated into daily buckets, broken down by project, user, API key, and model.

Incremental, Full refreshstart_time—
usage_audio_transcriptions

Speech-to-text usage aggregated into daily buckets, broken down by project, user, API key, and model.

Incremental, Full refreshstart_time—
usage_vector_stores

Vector store storage usage aggregated into daily buckets, broken down by project.

Incremental, Full refreshstart_time—
usage_code_interpreter_sessions

Code interpreter session usage aggregated into daily buckets, broken down by project.

Incremental, Full refreshstart_time—
usage_web_search_calls

Web search tool-call usage aggregated into daily buckets, broken down by project, user, API key, model, and context level.

Incremental, Full refreshstart_time—
usage_file_search_calls

File search tool-call usage aggregated into daily buckets, broken down by project, user, API key, and vector store.

Incremental, Full refreshstart_time—
costs

Daily spend for your OpenAI organization, broken down by project, line item, and API key.

Incremental, Full refreshstart_time—
projects

Projects in your OpenAI organization (includes archived projects).

Full refresh——
users

Members of your OpenAI organization.

Full refresh——
invites

Pending and historical invitations to join your OpenAI organization.

Full refresh——
admin_api_keys

Organization-level Admin API keys.

Full refresh——
project_users

Membership rows mapping users to the projects they belong to, one row per (project, user).

Full refresh——
project_service_accounts

Service accounts (bot users not tied to a person) in each project.

Full refresh——
project_api_keys

API keys provisioned in each project.

Full refresh——
project_rate_limits

Per-model rate limit configuration for each project.

Full refresh——
groups

Groups of users in your OpenAI organization, used to grant access and roles in bulk.

Full refresh——
group_users

Membership of each group: one row per user in a group.

Full refresh——
roles

Roles configured for your OpenAI organization, including predefined roles managed by OpenAI.

Full refresh——
user_roles

Organization roles assigned to each user, directly or through a group.

Full refresh——
audit_logs

User actions and configuration changes within your OpenAI organization.

Incremental, Full refresheffective_at—
models

Models available to the project of the project API key, including fine-tuned models.

Full refresh——
chat_completions

Chat completions created with store set to true in the project of the project API key.

Full refresh——
batches

Batch API jobs in the project of the project API key, with their status and request counts.

Full refresh——
evals

Evaluations defined in the project of the project API key.

Full refresh——
eval_runs

Runs of each evaluation, with their status, result counts, and per-criteria results.

Full refresh——
eval_run_output_items

Per-sample results of each evaluation run, with the grader results for each sample.

Full refresh——

Troubleshooting

  • 401 Unauthorized (Admin API key) – your Admin API key is invalid or has been revoked. Create a new one and reconnect.
  • 403 Forbidden (Admin API key) – your key isn't an Admin API key. Ask an organization owner to create one in the organization settings.
  • 401 Unauthorized (Project API key) – your project API key is invalid or has been revoked. Create a new one in your API keys dashboard.
  • 403 Forbidden (Project API key) – your project API key can't read this table. Give the key read access in your OpenAI project settings, or stop syncing that table.

If your sync is failing or data looks wrong, see the Data warehouse troubleshooting guide. If that doesn't help, contact support – we're happy to help.

Still have questions?

Was this page useful?