Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Mission control shows model usage, yours and the workspace's: tokens, cost, active days, cache share, each day, and the mix | 1 | /** |
| 2 | * What the proxy tells billing about one answer: its tokens, under the | |
| 3 | * run's session and the person it is for, for usage views. Billing still | |
| 4 | * prices runs from AI Gateway, not from these. | |
| 5 | */ | |
| 6 | import type { ModelUpstream } from "@g1t/contracts"; | |
| 7 | ||
| 8 | import type { Tokens } from "./usage"; | |
| 9 | ||
| 10 | export type TokenReport = { | |
| 11 | workspace: string; | |
| 12 | session: string; | |
| 13 | person: string | null; | |
| 14 | model: string; | |
| Merge branch 'model-routing' | 15 | tier: "small" | "large" | "frontier" | null; |
| Mission control shows model usage, yours and the workspace's: tokens, cost, active days, cache share, each day, and the mix | 16 | input: number; |
| 17 | output: number; | |
| 18 | cacheRead: number; | |
| 19 | cacheWrite: number; | |
| 20 | }; | |
| 21 | ||
| 22 | /** | |
| 23 | * Whether a request's answer is a model's answer, and so used tokens. | |
| 24 | * Counting tokens is a question about a request, not an answer. | |
| 25 | */ | |
| 26 | export function isAnswer(path: string): boolean { | |
| 27 | return /^\/v1\/messages\/?(\?|$)/.test(path); | |
| 28 | } | |
| 29 | ||
| 30 | /** | |
| Merge platform pause and the hourly usage watcher: staff can pause compute, schedules, indexing or renders for everyone, the watcher emails on a breach and is never blind quietly, and the models proxy holds each run to its cap (billing 0051, integrations 0006) | 31 | * Whether a run's token may make a request: a model's answer, counting a |
| 32 | * request's tokens, or listing models. Nothing else, so no other route | |
| 33 | * (a batch of messages, say) spends past the run's cap uncounted. | |
| 34 | */ | |
| 35 | export function runMayCall(path: string, method: string): boolean { | |
| 36 | const route = path.split("?")[0]; | |
| 37 | if (isAnswer(route)) return true; | |
| 38 | if (/^\/v1\/messages\/count_tokens\/?$/.test(route)) return true; | |
| 39 | return (method === "GET" || method === "HEAD") && /^\/v1\/models(\/[^/]+)?\/?$/.test(route); | |
| 40 | } | |
| 41 | ||
| 42 | /** | |
| Mission control shows model usage, yours and the workspace's: tokens, cost, active days, cache share, each day, and the mix | 43 | * The report for one answer, or null when it used nothing or its session |
| 44 | * has no id to count it under. The model is the run's when its route names | |
| 45 | * one, else the one that answered. | |
| 46 | */ | |
| 47 | export function tokenReport(upstream: ModelUpstream, answeredBy: string | null, tokens: Tokens): TokenReport | null { | |
| 48 | const used = tokens.input + tokens.output + tokens.cacheRead + tokens.cacheWrite; | |
| 49 | if (used === 0 || !upstream.session) return null; | |
| 50 | return { | |
| 51 | workspace: upstream.workspace, | |
| 52 | session: upstream.session, | |
| 53 | person: upstream.requestedBy ?? null, | |
| 54 | model: upstream.model ?? answeredBy ?? "unknown", | |
| 55 | tier: upstream.tier ?? null, | |
| 56 | input: tokens.input, | |
| 57 | output: tokens.output, | |
| 58 | cacheRead: tokens.cacheRead, | |
| 59 | cacheWrite: tokens.cacheWrite, | |
| 60 | }; | |
| 61 | } |
This file's history is long; its oldest lines are credited to the oldest commit read.