← All Labs Lab 04 / 28 Mycel Exchange end to end
Lab 04 · Run models · Learned

Mycel Exchange end to end

A buyer's turn through a local Mycel Exchange is billed from the Exchange's own token count, at the catalogue's retail price, in whole micro-dollars, to the right End User or Application balance.

Learned Needs: chat, streaming Smoke: pnpm exec tsx examples/04-mycel-exchange/run.ts --smoke Run: pnpm exec tsx examples/04-mycel-exchange/run.ts Source: examples/04-mycel-exchange
sh
pnpm exec tsx examples/04-mycel-exchange/run.ts --smoke

Starts the real Exchange on loopback with an in-memory store and a mock supplier (a stand-in for a DGX on the office network). A pi turn (runPiTurn with provider mycel, which imports nothing from @umwelten/mycel) goes through it as End User user-1 of an Application that requires the on-premise guarantee. Then a second caller uses a static Application credential instead of a JWT. Nine checks:

CheckWhat it shows
dispatchThe turn went to the on-premise supplier, attributed to the right Application and End User
own-countThe supplier claims 137 prompt tokens; the Exchange bills its own count
own-completion-countThe supplier claims 9 completion tokens; the Exchange bills its own count of the text it relayed (at least characters ÷ 4)
exact-chargeCharge equals the catalogue's retail price, computed in this script, each part rounded to the nearest micro-dollar
integer-amountsCharge, cost and balance are whole micro-dollars
balance-debitedThe End User's balance drops by exactly the charge
static-credentialA static credential plus X-Mycel-End-User is served and attributed
pool-debitedAn End User nobody funded draws from the Application's pool
no-end-user-refusedWithout X-Mycel-End-User the request gets 401, with no supplier call and no record

Change how priceRequest in packages/mycel/src/metering/counter.ts rounds (round the sum instead of each part) and exact-charge fails. Make the handler record the supplier's completion count instead of its own and own-completion-count fails; before that check existed, every check passed with it.

Parameter pass-through (the supplier, not the catalogue, decides what a request may carry) is its own lab now: Lab 22.

Prompt

Coding-Agent Prompt · Ready to Paste
Build a self-contained end-to-end script that boots @umwelten/mycel's Exchange on localhost, registers an 'on-premise' Supplier publishing gemma-4-26b, funds an End User with 10,000,000 micro-dollars ($10), streams a turn from @umwelten/pi's runPiTurn({ provider: 'mycel' }) without importing @umwelten/mycel on the buyer side, verifies exact prompt/completion micro-dollar charges, and proves requests missing X-Mycel-End-User are rejected with HTTP 401.
Works in Pi, Claude Code, Codex & Antigravity Offline mock first → --live opt-in

What we learned

The answer

Metering holds. The mock supplier reports 137 prompt and 9 completion tokens, and the Exchange ignores both and bills its own counts of 16 and 14. At the catalogue's $0.10/M prompt and $0.40/M completion, 16 + 14 tokens cost 8 micro-dollars, and the End User's balance drops from 10,000,000 to 9,999,992. A static-credential caller whose End User was never funded draws from the Application's pool. A request without X-Mycel-End-User gets 401 before any supplier call or ledger record.

What going through it found

  • The lab checked nothing. run.ts printed the record and always exited 0, so a wrong charge would have looked like success. It now has eight PASS/FAIL checks and exits 1 on any failure. Changing the Exchange's rounding to round the sum makes exact-charge fail (7 against 8), so the check catches pricing bugs.
  • Its smoke ran the other script. The frontmatter's smoke: pointed at pass-through.ts, which tests parameter pass-through, not metering. It now runs run.ts --smoke.
  • The lab tested two things. pass-through.ts (the supplier decides parameter support) was a separate question; it moved to Lab 22.
  • The README described a retired concept ("Interaction → Mycel"). It now describes the checks.
  • Completion tokens weren't checked. own-count covered prompt tokens only, and exact-charge prices whatever count the record holds, so an Exchange billing the supplier's completion count passed all eight checks. own-completion-count now checks the Exchange's count against the supplier's claim and the text the buyer received; that bug fails it.

Gotchas

  • Streamed completions are counted per chunk. Each relayed chunk rounds up to whole tokens on its own, so the 43-character reply streamed in 9 chunks bills 14 tokens, while the same text unstreamed would bill 11. Short chunks pay up to almost a token each.
  • Each part of the price rounds on its own. round(1.6) + round(5.6) = 8, while rounding the total, 7.2, would give 7. On tiny requests that is up to a micro-dollar either way per part; any price shown to users has to round the same way.
  • The Exchange's token count is an estimate (about four characters per token), not the model's tokenizer. It is the billing count by design (ADR 0017), but it won't match what the supplier reports.
  • A priced request costs at least 1 micro-dollar, so a short prompt on a cheap model is never free unless both retail rates are explicitly zero.

Not tested

  • Guarantee-filtered dispatch against a cheaper supplier without on-premise. The lab has only one supplier, so it can't show a cheaper offer being skipped.
  • Running out of balance mid-stream.
  • A supplier that dials in over WSS /suppliers/connect; this lab registers the supplier by URL. Lab 01 covers the dial-in.
  • Hosted Mycel. The lab needs the Exchange's ledger, so it runs only locally.

Smoke run

Visual Target Wireframe ASCII Layout
  PASS  dispatch: served by office-spark for hairstyle-app/user-1
  PASS  own-count: supplier reported 137 prompt tokens; the Exchange billed its own count, 16
  PASS  own-completion-count: supplier reported 9 completion tokens; the Exchange billed 14 for 43 characters received (at least 11)
  PASS  exact-charge: 16 × $0.1/M + 14 × $0.4/M → expected 8, charged 8
  PASS  integer-amounts: charge 8, cost 0, balance 9999992
  PASS  balance-debited: 10000000 − 8 = 9999992
  PASS  static-credential: HTTP 200, 1 record(s), subject operator
  PASS  pool-debited: unfunded subject drew from the Application pool: 5000000 → 4999996
  PASS  no-end-user-refused: HTTP 401, 0 supplier call(s), 0 new record(s)

  9/9 passed