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.
| The Spend page says what a workspace spent, where it went and the budgets that hold it, in Workspace under Money: spent at price, agents' spend and the spend limit; by day; by agent, person, channel, model, kind of work and product; budgets from the workspace down to each person, agent and task, with what happens at 100%; the costliest tasks, each with a receipt of every session at the provider's price and what was charged; and how it's priced. Everyone sees what agents spent for them, and owners and billing managers the whole workspace; each person can have a monthly budget agents keep to, and the spend pill in the top bar shows your month or the workspace's against its limit. The spend guide says how. | 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; | |
| 3 | ||
| 4 | import type { AgentSession, PriceBook } from "@g1t/contracts"; | |
| 5 | ||
| 6 | import { daySeries, markupLabel, percentLabel, pricingOf, readPeriod, readScope, receiptOf, spanFor, tokenCount } from "./spend.ts"; | |
| 7 | ||
| 8 | const OCT_8 = new Date("2026-10-08T15:00:00Z"); | |
| 9 | ||
| 10 | test("a period's days match the agents service's, both ends included", () => { | |
| 11 | assert.deepEqual(spanFor("month", OCT_8), { from: "2026-10-01", until: "2026-10-08" }); | |
| 12 | assert.deepEqual(spanFor("last_month", OCT_8), { from: "2026-09-01", until: "2026-09-30" }); | |
| 13 | assert.deepEqual(spanFor("last_month", new Date("2026-01-15T00:00:00Z")), { from: "2025-12-01", until: "2025-12-31" }); | |
| 14 | assert.deepEqual(spanFor("7d", OCT_8), { from: "2026-10-02", until: "2026-10-08" }); | |
| 15 | assert.deepEqual(spanFor("30d", OCT_8), { from: "2026-09-09", until: "2026-10-08" }); | |
| 16 | }); | |
| 17 | ||
| 18 | test("only someone who may see the workspace's spend gets it; anything else is their own", () => { | |
| 19 | assert.equal(readScope(null, true), "workspace"); | |
| 20 | assert.equal(readScope("me", true), "me"); | |
| 21 | assert.equal(readScope("workspace", false), "me"); | |
| 22 | assert.equal(readPeriod("7d"), "7d"); | |
| 23 | assert.equal(readPeriod("year"), "month"); | |
| 24 | }); | |
| 25 | ||
| 26 | test("a day series has every day, products added together, zeros where nothing was spent", () => { | |
| 27 | const days = daySeries("2026-10-01", "2026-10-03", [ | |
| 28 | { day: "2026-10-01", micros: 5 }, | |
| 29 | { day: "2026-10-01", micros: 7 }, | |
| 30 | { day: "2026-10-03", micros: 1 }, | |
| 31 | ]); | |
| 32 | assert.deepEqual(days, [ | |
| 33 | { day: "2026-10-01", micros: 12 }, | |
| 34 | { day: "2026-10-02", micros: 0 }, | |
| 35 | { day: "2026-10-03", micros: 1 }, | |
| 36 | ]); | |
| 37 | }); | |
| 38 | ||
| 39 | test("pricing comes from the price book: the model markup, the run markup, the agent rate", () => { | |
| 40 | const book = { | |
| 41 | modelMarginPercent: 0, | |
| 42 | changes: [], | |
| 43 | prices: [ | |
| 44 | { meter: "sandbox_second", costMicros: 10, markupPercent: 20, priceMicros: 12 }, | |
| 45 | { meter: "build_second", costMicros: 10, markupPercent: 20, priceMicros: 12 }, | |
| 46 | { meter: "agent_tokens", costMicros: 0, markupPercent: 0, priceMicros: 250_000 }, | |
| 47 | { meter: "agent_tokens_own", costMicros: 0, markupPercent: 0, priceMicros: 250_000 }, | |
| 48 | ], | |
| 49 | } as unknown as PriceBook; | |
| 50 | const pricing = pricingOf(book); | |
| 51 | assert.equal(pricing.modelMarkupPercent, 0); | |
| 52 | assert.deepEqual(pricing.markup, { min: 20, max: 20 }); | |
| 53 | assert.equal(markupLabel(pricing.markup!), "20%"); | |
| 54 | assert.equal(markupLabel({ min: 15, max: 20 }), "15–20%"); | |
| 55 | assert.equal(pricing.agentRateMicros, 250_000); | |
| 56 | }); | |
| 57 | ||
| 58 | const session = (over: Partial<AgentSession>): AgentSession => | |
| 59 | ({ id: "s", parent_id: null, root_id: "r", charged_micros: 0, cost_micros: 0, input_tokens: 0, output_tokens: 0, steps: 0, tool_calls: 0, created_at: "2026-10-08T00:00:00Z", ...over }) as AgentSession; | |
| 60 | ||
| 61 | test("a receipt gives the root its own share, children under their parents, and the tree's totals", () => { | |
| 62 | const tree = [ | |
| 63 | session({ id: "r", charged_micros: 1_840_000, cost_micros: 1_000_000, input_tokens: 400_000, output_tokens: 30_000, steps: 6, tool_calls: 20 }), | |
| 64 | session({ id: "c1", parent_id: "r", charged_micros: 300_000, cost_micros: 200_000, input_tokens: 10_000, steps: 2, created_at: "2026-10-08T00:01:00Z" }), | |
| 65 | session({ id: "g1", parent_id: "c1", charged_micros: 40_000, cost_micros: 30_000, steps: 1, created_at: "2026-10-08T00:02:00Z" }), | |
| 66 | ]; | |
| 67 | const receipt = receiptOf(tree, "r")!; | |
| 68 | assert.deepEqual( | |
| 69 | receipt.lines.map((l) => [l.session.id, l.depth, l.ownMicros]), | |
| 70 | [ | |
| 71 | ["r", 0, 1_500_000], | |
| 72 | ["c1", 1, 300_000], | |
| 73 | ["g1", 2, 40_000], | |
| 74 | ], | |
| 75 | ); | |
| 76 | assert.equal(receipt.chargedMicros, 1_840_000); | |
| 77 | assert.equal(receipt.providerMicros, 1_230_000); | |
| 78 | assert.equal(receipt.steps, 9); | |
| 79 | assert.equal(receiptOf([], "r"), null); | |
| 80 | }); | |
| 81 | ||
| 82 | test("token counts and percents read short", () => { | |
| 83 | assert.equal(tokenCount(830), "830"); | |
| 84 | assert.equal(tokenCount(1_200), "1.2K"); | |
| 85 | assert.equal(tokenCount(412_000), "412K"); | |
| 86 | assert.equal(tokenCount(1_250_000), "1.3M"); | |
| 87 | assert.equal(percentLabel(62, 100), "62%"); | |
| 88 | assert.equal(percentLabel(62, null), ""); | |
| 89 | }); |