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.

Embed
Preview of the card
<a href="https://lucidace.com/help/ai-cost-per-person"><img src="https://lucidace.com/help/_api/public/orgs/af34862a-c837-4478-8335-0505efe40459/spaces/2f4b888b-c4e6-488b-81bd-6e838fb12d2e/pages/f531c950-d40c-43f7-80a4-4246157e05c6/social-card.svg?v=3e7ae33eec8b" alt="AI cost per person" width="600" height="315"></a>

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

  1. 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 the trace option; an id that is not 32 hex digits is dropped.

  2. Connect the Watchtower connector with a read token and stream llm_runs. It reads Watchtower's GET /llm/runs (the trace-filtered read covers the last 30 days; without the trace_id filter it pages by time).

  3. Each run becomes two things:

    • a row in the hidden external_metrics table (dimension llm_run_cost_usd, key <trace id>:<run id>), which feeds cost_usd on the conversations endpoint;

    • one $ai_run_cost event 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.

  4. 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

cost_usd

number

Run cost in USD (Watchtower's cost_micro divided by 1,000,000)

total_tokens

number

Input plus output tokens of the run, as Watchtower reports them

input_tokens, output_tokens

number

The split, once Watchtower's run list returns it (requested; until then only total_tokens is set)

model

string

First model Watchtower lists for the run

service

string

The service that ran it (use it as the feature filter: listing writer, pricing, shopping agent)

outcome

string

ok, or error if any call in the run failed

$ai_trace_id

string

Trace id of the conversation

run_id

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.

Updated