Launch offer: the first 1,000 users get Settl free for a year* Claim your spot
settlbuilding in public

Track what your AI really costs, per call, in PostHog

The short version

If your app talks to more than one AI provider, you get more than one bill and no idea which feature spends the money. The fix is small: every time your server makes an AI call, send PostHog one event with the provider, the model, the token counts and the cost. Then one dashboard shows every provider side by side, every day. Settl does exactly this: over 4,000 AI calls in November so far, one total of $8.35, and the app never sends PostHog a prompt, a reply or a transcript.

This guide is the pattern Settl uses, in the order you would build it.

Piece What it does Where it lives
One capture function Sends one $ai_generation event per model call a single file on your server
The fields provider, model, input tokens, output tokens, total cost, latency, status the event's $ai_* properties
The cost You compute it from the provider's price and send it as $ai_total_cost_usd the same event
The privacy rule Never the prompt, never the reply, never the transcript the shape of the function
One query Groups cost by day and provider a HogQL query, or a PostHog insight

What gets sent on every call

Property What it holds
$ai_provider who ran it (for example cerebras, google, elevenlabs)
$ai_model which model answered
$ai_input_tokens / $ai_output_tokens tokens in and out, when the provider reports them
$ai_total_cost_usd what this one call cost, in US dollars
$ai_latency how long it took, in seconds
$ai_http_status whether it worked
$ai_trace_id joins the calls of one user turn together

Two details that matter. Send the cost yourself (set $ai_cost_passthrough to true) when you already know it, because that is the number you actually pay. And keep the capture in one function so no call site can forget a field or add a prompt by accident.

Copy these prompts

Paste them into Claude Code (or any AI that can see your project).

1. Find every AI call in my app

Search my codebase for every place it calls an AI provider (OpenAI, Google Gemini, Anthropic, ElevenLabs, Cerebras, any gateway, any speech-to-text or image API). Make a table: file, function, provider, model, and whether the call already reports tokens or cost. Do not change anything yet.

2. Add one capture function

Add a single function captureAiGeneration to my server code that sends one PostHog event named $ai_generation per model call, using posthog-node. Properties: $ai_provider, $ai_model, $ai_input_tokens, $ai_output_tokens, $ai_latency (seconds), $ai_http_status, $ai_trace_id, and $ai_total_cost_usd with $ai_cost_passthrough true when the cost is known. The function must NEVER accept or send the prompt, the reply, or any transcript. Flush without delaying the response (waitUntil on serverless).

3. Wire the calls and compute the cost

Use the table from step 1. At every AI call site, call captureAiGeneration after the response comes back. Compute the cost from the provider's published price per token (or per second for speech-to-text) kept in one pricing object, and pass it as the cost. If a provider does not report tokens, send what it does report. Show me the diff, call site by call site.

4. Build the one dashboard

In PostHog, write a HogQL query over the $ai_generation event that returns, for the last 90 days, one row per day and provider with sum of $ai_total_cost_usd and count of calls. Then give me three insights: daily cost stacked by provider, cumulative cost, and this month so far. Do not show cost per call on any chart I would share.

If you are building

Run this on your codebase

Audit my AI cost tracking. List every AI call in the project and tell me which ones do NOT send a $ai_generation event to PostHog. Check that no event can contain a prompt, a reply, a transcript or any user-written text. Tell me which provider is the biggest line this month and which feature calls it. Give me the three fixes that would close the biggest gaps.

Get the next one in your inbox