Can we work on tracking usage metrics like we discussed in the thread?
Can we work on tracking usage metrics like we discussed in the thread?
Picking up the first step from the business-direction thread: per-job usage metering, rolled up per workspace. Today jobs only has started_at/finished_at and the runner reports nothing about tokens.
Plan:
- Schema (
drizzle/0014_job_usage.sql): nullable usage columns onjobs:input_tokens,output_tokens,cache_read_tokens,cache_write_tokens,cost_usd,turns, plus ausagejsonb with the per-model breakdown the Agent SDK gives us. Null = never reported (old jobs). Sandbox time stays derived fromstarted_at/finished_at. - Runner (
lib/agent/runner.mjs): tallymessage.usagefrom every assistant message as it streams (dedup'd by API message id) and ship the running total with each heartbeat, so a run that is stopped, times out or crashes still leaves what it burned. At the end, the SDKresultmessage'smodelUsage/total_cost_usd/num_turnsreplaces the tally and goes with/finish. - Server:
lib/usage.ts(zod shape,recordJobUsage, workspace rollup query); heartbeat + finish routes acceptusage;finishJoband Stop stamp the run's usage onto the run's terminal post (meta.usage). - UI: the terminal post of each run shows a small usage line (tokens, est. cost, wall time, turns);
/workspaces/[id]gets a Usage section for members: this month and all time (runs, tokens, est. cost, sandbox minutes) plus the per-model split.
Cost is the SDK's own estimate (list price), labelled as such, not a bill. No quotas/plans yet; that's the next step once numbers exist.
threadide@4c1fd4dUsage metering is in, pushed as 4c1fd4d on thread/3abc99dc.
What each run now records (jobs columns, drizzle/0014): input / output / cache-read / cache-write tokens, the Agent SDK's cost estimate, turns, and a per-model split in usage jsonb. Sandbox time comes from started_at/finished_at.
How it gets there
runner.mjstallies every assistant message's usage as the run streams (deduped by API message id, since the SDK emits one message per content block) and sends it with each heartbeat. So a run that gets stopped, times out or crashes still shows what it burned.- At the end the SDK result's
modelUsage/total_cost_usd/num_turnsgo with/finishand replace the tally. A tally can never overwrite a final report. finishJoband the Stop button stamp the run's figures onto the post that ended the run (meta.usage).
Where you see it
- Each run's final post gets a mono line:
RUN 1.32M in (90% cached) 18k out ~$2.41 25 min 48 turns. A stopped run showscost n/a(tally only). Exact figures are in the tooltip. /workspaces/[id]has a Usage section for members: month to date and all time (runs, tokens in/out, est. cost, sandbox time), by model, and the costliest threads this month. It also says how many earlier runs were never metered.
Caveats, so nobody mistakes this for billing
- Cost is the SDK's estimate at list price (
costUSD), labelled "est." A model the SDK can't price shows $0. Sandbox minutes aren't priced. - Verified against a sandbox Postgres (migration chain + a script covering tally → final → late tally, post stamping, Stop, and the rollup) and by rendering the components; a real end-to-end run needs the production credentials, so the first live run after merge is the real test. The SDK field names were checked against 0.3.283's typings.
Next steps if we go further on the business side: per-user attribution (jobs link to the trigger post already), a monthly cap per workspace read from these same columns, then plans/Stripe.
I’m also curious, can you tell me how much my usage and spending has been so far?
Short answer first: I can't read it off for you yet. The sandbox has no production database access, and the metering in PR #154 isn't merged, so no run has recorded tokens or cost so far; the runner never reported them before this branch, and that history can't be recovered.
What I can do now, so the "so far" question gets the best possible answer once #154 lands:
- Extend the Usage panel on /workspaces/[id] with a Before metering row: the number of runs and the sandbox time they took (both derivable from
jobs.started_at/finished_atfor every past run), with tokens/cost marked n/a. Right now that history is just a footnote count. - Give you the two places where the real spend already lives (Anthropic Console for the server key; your own key's console if you set one on /settings) and a SQL query you can run against production for the run count and sandbox minutes today.
Starting the panel change; it's small.
threadide@1815c4dThe honest answer: there is no number to give you yet. Three reasons, none fixable from here:
- Before this branch the runner reported nothing about tokens, and the Anthropic API doesn't let us look up past calls per request. So the tokens and cost of every run up to now are gone; metering starts with the first run after PR #154 merges and production redeploys.
- PR #154 is still open, so even the new runs aren't being recorded yet.
- The sandbox has no production database access, so I can't query what is known (run counts, sandbox minutes).
Where your real spend lives today
- Runs on the server key: Anthropic Console → Usage / Cost for the org that owns
ANTHROPIC_API_KEY. Filter by that key; the console shows tokens per model and dollars per day. That is the actual bill, not an estimate. - If you set your own key on /settings (BYOK, PR #79), those runs are on your own console instead. A Claude subscription token (
sk-ant-oat01…) shows up on claude.ai's usage page and isn't billed per token. - Sandbox time is on the Vercel dashboard (Sandbox usage) for the team.
What I changed so "so far" gets the best answer the data allows (1815c4d): the Usage section on /workspaces/[id] now has a Before metering row with the run count and total sandbox time of every past run (jobs.started_at/finished_at exist for all of them), tokens and cost shown as n/a. It was only a footnote count before. tsc clean; no screenshot this time, it's one table row in the layout already shown above.
If you want the run count and sandbox minutes right now, run this against production (Neon/Vercel Postgres console):
select count(*) as runs,
round(sum(extract(epoch from finished_at - started_at))/60) as sandbox_minutes,
min(created_at) as first_run
from jobs
where status <> 'queued';
Add join threads t on t.id = jobs.thread_id where t.workspace_id = '<id>' for one workspace.
Rough sense of scale, not a figure: a run like the one that built this branch is on the order of 1–3M input tokens (mostly cache reads) and 20–40k output on Fable 5.1, which the SDK prices around $1.5–3. Multiply by your run count from the query above for a ballpark; the console has the truth.
Next step on the business side stays as before: once a month of metered runs exists, per-user attribution (jobs already link to the trigger post) and a monthly cap per workspace read from these columns.

![Usage section on /workspaces/[id] (members): month to date, all time, by model, costliest threads (mock data)](https://tyrvppbgsommwt2h.public.blob.vercel-storage.com/threadide/199a9a37-af86f3e3c4ce.png)
