AI cost per person
How LLM cost reaches Lucida from Watchtower, the $ai_run_cost event it becomes, and how to chart cost and tokens per person or cohort.
Lucida records no LLM calls itself. Tokens, latency, models and cost belong to Watchtower. The Watchtower connector copies each agent run's cost and token count into Lucida as an ordinary event on the person who owns the conversation, so the standard insights work on it.
How the cost arrives
Your agent code sends
$ai_trace_id(the 32 hex digit W3C trace id of the agent run, the same id Watchtower records) on$ai_conversation_started. The Lucida SDKs take it as thetraceoption; an id that is not 32 hex digits is dropped.Connect the Watchtower connector with a read token and stream
llm_runs. It reads Watchtower'sGET /llm/runs(the trace-filtered read covers the last 30 days; without thetrace_idfilter it pages by time).Each run becomes two things:
a row in the hidden
external_metricstable (dimensionllm_run_cost_usd, key<trace id>:<run id>), which feedscost_usdon the conversations endpoint;one
$ai_run_costevent on the person whose events carry the same$ai_trace_id(the distinct id of the latest such event). If no person is found for the trace, the event is skipped and the metric row is still written. Runs without a trace id are skipped entirely.
The event id is derived from
<trace id>:<run id>, so a re-sync (runs of the last hour are read again while their cost settles) replaces the event instead of duplicating it. Delivery is at-least-once.
The $ai_run_cost event
$ai_run_cost is a reserved Lucida event: a tracking plan never flags it. It is timestamped at the start of the run.
Property | Type | Meaning |
|---|---|---|
| number | Run cost in USD (Watchtower's |
| number | Input plus output tokens of the run, as Watchtower reports them |
| number | The split, once Watchtower's run list returns it (requested; until then only |
| string | First model Watchtower lists for the run |
| string | The service that ran it (use it as the feature filter: listing writer, pricing, shopping agent) |
| string |
|
| string | Trace id of the conversation |
| string | Watchtower run id |
Filter any of the charts below by model or service with a normal event property filter. Cached or reasoning tokens are not separated.
Math on cost_usd (or a token property): sum, avg, median, p75 to p99 per run, and per person per_user_avg, per_user_median, per_user_p75, per_user_p90, per_user_p95, per_user_p99. For example "math": "per_user_p90" is the 90th percentile of cost per person in each interval.
Chart cost per person
In the app: New insight, Trends, pick the event $ai_run_cost, change the math from Total count to Sum, choose the property cost_usd, and set Breakdown to Person ID. Change the property to total_tokens for tokens.
Via the API, POST /v1/projects/{id}/insights:
{
"name": "AI cost per person (USD)",
"query": {
"kind": "trends",
"date_range": {"from": "-30d", "to": "now"},
"interval": "day",
"series": [{
"source": {"type": "event", "name": "$ai_run_cost"},
"math": "sum",
"math_property": "cost_usd"
}],
"breakdown": [{"key": "meta:person_id", "type": "event"}]
}
}Use "math": "per_user_avg" with the same math_property for the average cost per person. Replace the breakdown with a person property ({"key": "plan", "type": "person"}) to see cost by plan.
Chart cost per cohort
Breakdown by cohort takes cohort ids, comma separated, up to five. Each person lands in the first listed cohort they belong to, and everyone else in a "none" bucket:
"breakdown": [{"key": "<cohort id 1>,<cohort id 2>", "type": "cohort"}]Everything else in the body above stays the same. Filter a series to one cohort with the usual cohort property filter instead if you want a single total.
Other reads
GET /v1/projects/{id}/agent-analytics/conversations still returns cost_usd per conversation (the sum of the external_metrics rows for its trace), with distinct_id and person_id. It is null with no connector, no synced runs yet, or no trace id on the conversation. The Agent analytics page shows the same column. See Agent analytics.
Good to know
Runs the connector already synced before this event existed have a metric row but no event; they get one only if the connector re-reads them (a full resync reads the last 30 days).
A trace whose conversation event has not reached Lucida yet gets no event until a later sync finds it, and a run older than the sync window is not picked up.
Cost per person depends on a person being resolvable from
$ai_trace_id; conversations without a trace id have no cost.
See also Watchtower and Developer guide: AI and agents.