← All help articles

AI observability

Track LLM calls from the Vercel AI SDK and OpenAI: traces and token costs

Send your app's LLM calls to AI observability to see each trace, who made it and what its tokens cost.

Wrap your LLM client with PostHog's @posthog/ai package. AI observability then records each LLM call, groups the calls into traces and works out what they cost from the tokens used.

Before you start

You need:

  • your PostHog project token, which starts with phc_;

  • your PostHog host, the address of your PostHog instance.

Both are in your PostHog project settings.

The code below is from PostHog's docs as of 31 October 2026. If a line fails, check PostHog's current docs.

Vercel AI SDK

These steps assume your app already uses the Vercel AI SDK with its OpenAI provider.

  1. Install the PostHog Node SDK and @posthog/ai. This command also installs the OpenAI Node SDK.

    ``bash npm install --save posthog-node @posthog/ai openai ``

  2. Create a PostHog client. Replace the placeholders with your project token and host.

    ```javascript import { PostHog } from 'posthog-node'

    const phClient = new PostHog( '<ph_project_token>', { host: '<ph_client_api_host>' } ) ```

  3. Wrap your model with withTracing.

    ```javascript // For Vercel AI SDK import { withTracing } from '@posthog/ai' import { generateText } from 'ai'

    const model = withTracing( openaiClient('gpt-5'), phClient, { posthogProperties: { $ai_span_name: "Generate Product Description", product_category: "electronics", target_length: "short" } } ) ```

    openaiClient stands for your Vercel AI SDK OpenAI provider. $ai_span_name is the name your trace shows in PostHog. The other two properties are examples: use your own. The docs also show a posthogDistinctId: '<your_distinct_id>' option next to posthogProperties, marked optional, to link the calls to a user.

  4. Call the wrapped model.

    ``javascript const { text } = await generateText({ model: model, prompt: Write a product description for: ${productName} }) ``

  5. Send the queued events before your code stops. In a serverless function, await phClient.flush() before you return. Call shutdown() only when you're done with the client or before your program exits:

    ``node await client.shutdown() ``

    Here client is your PostHog client, phClient above.

Keep one shared PostHog client outside your handler, so every call reuses it. The help article "Server-side and serverless: don't lose events (flush and shutdown)" explains flush and shutdown in detail.

OpenAI Node SDK

  1. Install the PostHog Node SDK, @posthog/ai and the OpenAI Node SDK. Skip this if you already ran it above.

    ``bash npm install --save posthog-node @posthog/ai openai ``

  2. Create a PostHog client and an OpenAI client that reports to it. Use the OpenAI client from @posthog/ai, not the one from the plain openai package.

    ```javascript import { OpenAI } from '@posthog/ai' import { PostHog } from 'posthog-node'

    const phClient = new PostHog( '<ph_project_token>', { host: '<ph_client_api_host>' } )

    const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, posthog: phClient }) ```

  3. Call OpenAI.

    ``ts const completion = await openai.responses.create({ model: "gpt-5-mini", input: [{ role: "user", content: "Tell me a fun fact about hedgehogs" }], posthogDistinctId: "user_123", // optional posthogTraceId: "trace_123", // optional posthogProperties: { custom_property: "value" }, // optional posthogGroups: { company: "company_id_in_your_db" }, // optional posthogPrivacyMode: false // optional }); ``

    posthogDistinctId links the call to a user. posthogTraceId groups calls into one trace. To give it a name in PostHog, add $ai_span_name inside posthogProperties. The docs pass these options to the OpenAI client imported from @posthog/ai, not the one from the openai package.

  4. Send the queued events before your code stops, as in the Vercel AI SDK section: await phClient.flush() before a serverless function returns, and phClient.shutdown() before your program exits.

Check it in PostHog

  1. Click Apps in the left navigation, then AI observability.

    The Apps panel open from the left navigation, with AI observability highlighted
  2. Click the Traces tab.

    The AI observability page with the Traces tab highlighted
  3. Find your call by its Trace Name, the $ai_span_name you set, and read its Cost. The date picker starts at Last hour, so pick a longer range for older calls.

    The traces table from the Trace Name column to the Cost column, with Cost highlighted
  4. Click the trace's row to open it. The chip with the receipt icon, next to the token usage, shows what the whole trace cost. Hover it to see the cost split into Input, Output and Total.

    A trace page with the receipt chip highlighted and its tooltip showing the Input, Output and Total cost

Why is my cost missing?

PostHog works out each call's cost, $ai_total_cost_usd, from its input and output tokens, its cache reads and writes, and any per-request or web search costs. A trace's cost is the sum of the costs of its generations and embeddings.

To find the model's prices, PostHog matches both $ai_provider and $ai_model. The cost can stay empty, and the table shows "–", when:

  • the call has no $ai_model, so PostHog can't match a model;

  • PostHog doesn't know the model;

  • the call reports no token counts, and no per-request, web search or pre-calculated cost.

Without $ai_model, or for a model PostHog doesn't know, set your own prices instead. Add $ai_input_token_price and $ai_output_token_price to posthogProperties. You need both, and each is the price of a single token, not of a million tokens.

Common problems

Nothing shows up in Traces. Widen the date range first: it starts at the last hour. In a serverless function, await phClient.flush() before you return, or events can be lost when the function stops. Then check that the token is this project's project token (phc_…), not a personal API key, and that the host matches your PostHog region.

A trace has no name ("–"). Set $ai_span_name in posthogProperties.