Skip to content
156 linesCodeBlameRaw
1import { ArrowLeft, CornerDownRight } from "lucide-react";
2import type { CSSProperties } from "react";
3import { Link, data } from "react-router";
4
5import type { Route } from "./+types/spend-receipt";
6import { AgentAvatar } from "../../components/agent-avatar";
7import { KindBadge, PrivateTitle, StatusChip, sessionHref } from "../../components/agents/parts";
8import { whereLabel } from "../../components/agents/format";
9import { TimeAgo } from "../../components/ui";
10import { effortLabel } from "../../lib/effort";
11import { page } from "../../lib/meta";
12import { requireUser, roleIn } from "../../lib/session.server";
13import { loadPricing } from "../../lib/spend.server";
14import { receiptOf, tokenCount } from "../../lib/spend";
15import { workspaceAgents } from "../../lib/services.server";
16import { money } from "../../lib/usage";
17
18export function meta({ params, ...args }: Route.MetaArgs) {
19 return page(args, { title: `Receipt · ${params.owner} · g1t` });
20}
21
22/** A task's receipt: its session tree from the agents service, and the price book's terms. */
23export async function loader({ params, context, request }: Route.LoaderArgs) {
24 const viewer = requireUser(context, request);
25 const slug = params.owner.toLowerCase();
26 if (!roleIn(viewer, slug)) throw data(null, { status: 404 });
27 const [detail, pricing] = await Promise.all([workspaceAgents.session(slug, params.id, viewer).catch(() => null), loadPricing()]);
28 if (detail && !detail.ok && detail.error.code === "not_found") throw data(null, { status: 404 });
29 const receipt = detail?.ok ? receiptOf(detail.value.tree, detail.value.session.root_id) : null;
30 return { slug, receipt, pricing };
31}
32
33/**
34 * One task, itemized: each session of its tree (the agent's own, and every
35 * helper and subagent it brought in) with its tokens, the model at the
36 * provider's price and what it was charged; then the totals. A session in
37 * a conversation you aren't in shows what it cost, not what it was about.
38 */
39export default function Receipt({ loaderData }: Route.ComponentProps) {
40 const { slug, receipt, pricing } = loaderData;
41 const back = (
42 <Link to={`/${slug}/-/spend`} className="inline-flex items-center gap-1.5 text-sm text-muted hover:text-fg">
43 <ArrowLeft size={14} /> Spend
44 </Link>
45 );
46 if (!receipt) {
47 return (
48 <div className="mx-auto w-full max-w-215 space-y-6 px-4 py-5 md:px-10 md:py-8">
49 {back}
50 <p className="rounded-xl border border-line bg-surface px-4 py-8 text-center text-sm text-muted">This receipt couldn&apos;t be read right now. Reload in a moment.</p>
51 </div>
52 );
53 }
54 const { root } = receipt;
55 const g1tPart = Math.max(0, receipt.chargedMicros - receipt.providerMicros);
56 return (
57 <div className="mx-auto w-full max-w-215 space-y-6 px-4 py-5 md:px-10 md:py-8">
58 {back}
59 <header className="flex items-start gap-3">
60 <AgentAvatar agent={{ handle: root.agent_handle, avatar_seed: root.agent_avatar_seed }} size={36} />
61 <div className="min-w-0 grow">
62 <p className="text-xs text-muted">Receipt</p>
63 <h1 className="mt-0.5 text-xl leading-snug font-semibold tracking-tight">{root.visible ? root.title || "Untitled session" : <PrivateTitle />}</h1>
64 <p className="mt-1 flex flex-wrap items-center gap-x-1.5 gap-y-1 text-xs text-faint">
65 <span className="text-muted">{root.agent_name}</span>
66 {root.visible && root.asked_by_username && <span>· for @{root.asked_by_username}</span>}
67 {root.visible && <span>· {whereLabel(root)}</span>}
68 <span>·</span>
69 <TimeAgo at={root.created_at} />
70 <StatusChip status={root.status} className="ml-1" />
71 </p>
72 </div>
73 <div className="shrink-0 text-right">
74 <p className="text-xs text-muted">Total</p>
75 <p className="text-2xl font-semibold tracking-tight tabular-nums">{money(receipt.chargedMicros)}</p>
76 </div>
77 </header>
78
79 <section aria-label="Sessions" className="overflow-hidden rounded-xl border border-line bg-surface">
80 <div className="hidden grid-cols-[minmax(0,1fr)_9rem_6.5rem_5.5rem] gap-3 border-b border-line px-4 py-2 text-[0.6875rem] font-medium tracking-wide text-faint uppercase sm:grid">
81 <span>Session</span>
82 <span className="text-right">Tokens</span>
83 <span className="text-right">Provider price</span>
84 <span className="text-right">Charged</span>
85 </div>
86 <ul className="divide-y divide-line/60">
87 {receipt.lines.map(({ session, depth, ownMicros }) => (
88 <li key={session.id} className="grid grid-cols-[minmax(0,1fr)_auto] gap-x-3 gap-y-1 px-4 py-3 sm:grid-cols-[minmax(0,1fr)_9rem_6.5rem_5.5rem] sm:items-center">
89 <div className="flex min-w-0 items-start gap-2" style={{ paddingLeft: `${depth * 1.25}rem` }}>
90 {depth > 0 && <CornerDownRight size={13} className="mt-0.5 shrink-0 text-faint" aria-hidden="true" />}
91 <div className="min-w-0">
92 <p className="flex min-w-0 flex-wrap items-center gap-1.5 text-sm">
93 {session.visible ? (
94 <Link to={sessionHref(slug, session)} className="min-w-0 truncate hover:underline">
95 {session.title || "Untitled session"}
96 </Link>
97 ) : (
98 <PrivateTitle />
99 )}
100 <KindBadge kind={session.kind} subagent={session.subagent} />
101 </p>
102 <p className="mt-0.5 truncate text-xs text-faint">
103 {session.agent_name}
104 {session.model ? ` · ${session.model}` : ""}
105 {session.effort ? ` · ${effortLabel(session.effort)} effort` : ""} · {session.steps} {session.steps === 1 ? "step" : "steps"} · {session.tool_calls} {session.tool_calls === 1 ? "tool" : "tools"}
106 </p>
107 </div>
108 </div>
109 <span className="text-right text-sm tabular-nums sm:hidden">{money(ownMicros)}</span>
110 <span className="col-span-2 text-xs text-faint tabular-nums sm:col-span-1 sm:text-right sm:text-sm sm:text-muted max-sm:pl-(--indent)" style={{ "--indent": `${depth * 1.25 + (depth ? 1.3 : 0)}rem` } as CSSProperties}>
111 <span className="sm:hidden">Tokens </span>
112 {tokenCount(session.input_tokens)} in · {tokenCount(session.output_tokens)} out
113 </span>
114 <span className="hidden text-right text-sm text-muted tabular-nums sm:block">{money(session.cost_micros ?? 0)}</span>
115 <span className="hidden text-right text-sm tabular-nums sm:block">{money(ownMicros)}</span>
116 </li>
117 ))}
118 </ul>
119 </section>
120
121 <section aria-label="Totals" className="rounded-xl border border-line bg-surface p-4">
122 <dl className="space-y-2 text-sm">
123 <Row
124 label={receipt.chargedMicros < receipt.providerMicros ? "Models, billed by your own provider, not here" : "Models, at the provider's price"}
125 value={money(receipt.providerMicros)}
126 />
127 <Row
128 label={
129 pricing?.agentRateMicros != null
130 ? `g1t's agent rate, ${money(pricing.agentRateMicros)} per million tokens${pricing.modelMarkupPercent > 0 ? `, and the ${pricing.modelMarkupPercent}% model markup` : ""}`
131 : "g1t's part"
132 }
133 value={money(g1tPart)}
134 />
135 <div className="border-t border-line pt-2">
136 <Row label="Total, as budgets count it" value={money(receipt.chargedMicros)} strong />
137 </div>
138 </dl>
139 <p className="mt-3 text-xs text-faint">
140 {tokenCount(receipt.inputTokens)} tokens in and {tokenCount(receipt.outputTokens)} out over {receipt.steps} steps and {receipt.toolCalls} tool calls. On your
141 own model key, your provider bills the model and only the agent rate is charged here; time on your own runners is $0. What the workspace is charged after
142 included usage and credit is on Billing.
143 </p>
144 </section>
145 </div>
146 );
147}
148
149function Row({ label, value, strong = false }: { label: string; value: string; strong?: boolean }) {
150 return (
151 <div className="flex items-baseline justify-between gap-4">
152 <dt className={strong ? "font-medium" : "text-muted"}>{label}</dt>
153 <dd className={`shrink-0 tabular-nums ${strong ? "font-semibold" : ""}`}>{value}</dd>
154 </div>
155 );
156}