Zoo

← back to the zoo

15. You can see what you spent

Every question through ai is on your ledger, per app.

ai writes every call to a ledger; GET ?api=usage tells an app what the signed-in person spent today and this week, by app.

This is the code serving the zoo right now: the bytes this very program was built from (each file is embedded when the zoo is compiled), at commit 55c71513c5 (live). The zoo's own self-check fetches this page and compares it byte for byte with the file it runs.

e15_spend.odin sha256 8bf7d361ee07 · raw

1package main
2
3// Exhibit 15. ai keeps a ledger of every call. Any app can read it back for the signed-in person:
4//     call_app("ai", "/?api=usage", true)   ->   {user: {id, spent, by_app}, breakdown: {"24h": [...], "7d": [...]}}
5// breakdown rows are {app, user, calls, tokens, in, out}. There are no budgets (PLAN belief 7): this
6// exhibit shows what was spent, not what is left.
7
8import "core:fmt"
9import "ratcloud:rc"
10
11App_Spend :: struct {
12	today: i64 `json:"today"`,
13	week:  i64 `json:"week"`,
14	calls: i64 `json:"calls"`,
15}
16Last_Call :: struct {
17	id:     i64 `json:"id"`,
18	at:     i64 `json:"at"`,
19	tokens: i64 `json:"tokens"`,
20}
21Spend :: struct {
22	today: i64 `json:"today"`,
23	apps:  map[string]App_Spend `json:"apps"`,
24	week:  i64 `json:"week"`,
25	last:  Last_Call `json:"last"`, // the zoo's newest call for this person, on ai's ledger
26}
27
28spend_by_app :: proc() -> (Spend, string) {
29	a := call_app("ai", "/?api=usage", true, timeout_ms = 20_000)
30	r := parse(a.body)
31	if a.status != 200 || !jhas(r, "user", "id") || !jhas(r, "breakdown") do return {}, fmt.tprintf("ai's usage answered %d%s", a.status, cat(": ", a.err) if a.err != "" else "")
32	uid := jint(r, "user", "id")
33	apps := make(map[string]App_Spend)
34	for row in jarr(r, "breakdown", "24h") {
35		if jint(row, "user") != uid do continue // the person's rows only, not the zoo's other askers
36		x := apps[jstr(row, "app")]
37		x.today = jint(row, "tokens")
38		apps[jstr(row, "app")] = x
39	}
40	week: i64 = 0
41	for row in jarr(r, "breakdown", "7d") {
42		if jint(row, "user") != uid do continue
43		x := apps[jstr(row, "app")]
44		x.week, x.calls = jint(row, "tokens"), jint(row, "calls")
45		apps[jstr(row, "app")] = x
46		week += x.week
47	}
48	return {jint(r, "user", "spent"), apps, week, {jint(r, "last", "id"), jint(r, "last", "at"), jint(r, "last", "tokens")}}, ""
49}
50
51e15 :: proc() -> Exhibit {
52	return {
53		n = 15, wing = "Intelligence",
54		title = "You can see what you spent",
55		promise = "Every question through ai is on your ledger, per app.",
56		block = "ai writes every call to a ledger; GET ?api=usage tells an app what the signed-in person spent today and this week, by app.",
57		src = {"e15_spend.odin", #load("e15_spend.odin", string)},
58		script = {"web/e15.js", #load("web/e15.js", string)},
59		show = proc(me: ^rc.User) -> string {
60			if me == nil do return signin_line("to see what you spent.")
61			return `<p>You spent <b id="spent-today">…</b> tokens today and <b id="spent-week">…</b> this week. Ask the model above and watch it rise. <button class="link" id="spent-refresh">refresh</button></p><p class="muted" id="spent-by"></p>`
62		},
63		api = proc(a: ^Api) -> bool {
64			if a.do_ != "usage" do return false
65			if a.me == nil {reply_err(a, 401, "sign in first"); return true}
66			s, err := spend_by_app()
67			if err != "" {reply_err(a, 502, err); return true}
68			reply(a, s)
69			return true
70		},
71		// The robot's spend must go up when it asks. (Exhibit 14 asks too; this does its own ask so the
72		// check never depends on the order of two checks: it calls 14's question, as the page does.)
73		check = proc() -> Check {
74			b, err := robot_at()
75			if err != "" do return fail(err)
76			before, after: Spend
77			r1 := post(b, self_url("/api/15/usage"))
78			if r1.status != 200 || !json_into(r1.body, &before) do return fail(fmt.tprintf("reading the spend failed (%d): %s", r1.status, jstr(r1.j, "error")))
79			b.timeout_ms = 30_000
80			r2 := post(b, self_url("/api/14/ask"), `{"question":"Reply with the single word: ok","max_tokens":20}`)
81			b.timeout_ms = 15_000
82			if r2.status != 200 do return fail(fmt.tprintf("the robot's question failed (%d): %s", r2.status, jstr(r2.j, "error")))
83			r3 := post(b, self_url("/api/15/usage"))
84			if r3.status != 200 || !json_into(r3.body, &after) do return fail(fmt.tprintf("reading the spend again failed (%d)", r3.status))
85			// Not the totals: they are over a rolling 24 h and 7 days, and a call from 24 h ago, as large, can
86			// leave the window while this one lands (the checks run on the same minutes every day, so it
87			// did, and this light was red). The ledger's newest call is the question just asked.
88			ask: Answer
89			json_into(r2.body, &ask)
90			if after.last.id <= before.last.id do return fail(fmt.tprintf("asked, but the robot's newest call by the zoo on ai's ledger is still #%d", before.last.id), after)
91			if after.last.tokens != ask.tokens do return fail(fmt.tprintf("the ledger's newest call (#%d) has %d tokens; the answer said %d", after.last.id, after.last.tokens, ask.tokens), after)
92			was, now_ := before.apps["zoo"].today, after.apps["zoo"].today
93			return pass(fmt.tprintf("one question (%d tokens) is on the robot's ledger by the zoo as call #%d; its spend by the zoo is %d tokens over 24 h (was %d), %d this week", ask.tokens, after.last.id, now_, was, after.week), after)
94		},
95	}
96}

web/e15.js sha256 39f4b837b08c · raw

1(() => {
2  const today = document.getElementById("spent-today");
3  if (!today) return;
4  const n = (x) => x.toLocaleString("en");
5  const read = async () => {
6    const j = await zoo.call("/api/15/usage");
7    if (j.error) { today.textContent = j.error; return; }
8    today.textContent = n(j.today); document.getElementById("spent-week").textContent = n(j.week);
9    const by = Object.entries(j.apps || {}).map(([a, r]) => a + " " + n(r.today) + " today, " + n(r.week) + " this week").join("; ");
10    document.getElementById("spent-by").textContent = by ? "By app: " + by : "Nothing spent this week.";
11  };
12  read();
13  document.getElementById("spent-refresh").addEventListener("click", read);
14  window.addEventListener("zoo-asked", read);   // exhibit 14 just asked: re-read, the number rises
15})();