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