A pi agent asks Mycel which model to use instead of hard-coding a model id. It reads the catalog, drops models that lack a required capability, and ranks the rest by the estimated dollar cost of one request. Then it runs one tool-calling turn on the chosen mycel:<id> to prove the choice works.
Run it
zsh
pnpm exec tsx examples/02-mycel-model-selection/run.ts # offline, mock Mycel
pnpm exec tsx examples/02-mycel-model-selection/run.ts --smoke # offline, PASS/FAIL lines, exit 0/1
MYCEL_URL=https://mycel.thefocus.ai MYCEL_API_KEY=... \
pnpm exec tsx examples/02-mycel-model-selection/run.ts --live --smoke
Walkthrough
- CATALOG: the run reads
GET /v1/modelsand prints each model's price, capabilities, estimated cost per request, and verdict. - SELECT: offline,
lab/cheap-prompthas the lowest prompt price, butlab/balancedwins once completion tokens count. The chat-only, decisions-only anddefaultalias entries are rejected with a reason. - TURN:
runPiTurnruns onmycel:<chosen id>. The model callslookup_section(7)and quotes the codeword. - Run
--live. Today it picksz-ai/glm-5.3-flash.
Files
run.ts: the three steps and the checks.catalog.ts: readsGET /v1/modelsand selects a model for a task profile.mock-mycel.ts: offline Mycel with a catalog and SSE chat completions with tool calls.presentation.html: the deck (published copy). It also covers caching, which is now Lab 03.PROMPT.md: a prompt to rebuild this folder.FINDINGS.md: what we learned and the latest smoke output.
Prompt
Coding-Agent Prompt · Ready to Paste
Build a standalone lab in examples/02-mycel-model-selection/ of the umwelten repo. It tests one thing:
a pi agent picks the cheapest Mycel model that has the capabilities its task needs, from Mycel's
GET /v1/models catalog, and that model answers through pi.
Read first:
- packages/pi/src/models.ts (resolvePiModel, runPiTurn, tool, ModelSpec, the "mycel" provider)
- packages/mycel/src/buyer/models.ts (the GET /v1/models response shape)
- examples/tsconfig.json
Files to write (do not import from other example folders; duplicate code if needed):
- catalog.ts: fetchCatalog(baseUrl, apiKey) parses GET /v1/models into
{ id, pricing: { prompt, completion } ($ per million tokens), capabilities[], guarantees[] }.
selectModel(catalog, taskProfile) marks each model eligible or not with a reason (alias "default",
missing capability, missing guarantee), estimates $ per request from the task's prompt and
completion token counts, sorts eligible models by that cost, and returns the cheapest.
- mock-mycel.ts: a node:http server on 127.0.0.1:0 with a bearer key. GET /v1/models returns a catalog
where the lowest prompt price is not the cheapest model once completion price counts, plus a
chat-only model, a decisions-only model, and a "default" alias. POST /v1/chat/completions streams
OpenAI-style SSE: a scripted model calls lookup_section for each "section N" in the user message,
then quotes the tool result. Record model, bearer and X-Mycel-End-User for each request.
- run.ts: offline mock by default; --live uses MYCEL_URL (default https://mycel.thefocus.ai) and
MYCEL_API_KEY. Set process.env.MYCEL_API_KEY and MYCEL_END_USER before calling pi. Print a CATALOG
table with verdicts, the SELECT reason, then one runPiTurn on { provider: "mycel", name: <chosen id>,
baseUrl: <url>/v1 } with a lookup_section tool. Wrap globalThis.fetch to record the model of every
chat request.
Checks: chosen model has every required capability and is not an alias; it is the cheapest eligible
one; resolvePiModel resolves it; the turn called lookup_section(7) and the answer quotes its codeword;
every chat request used the chosen model. Offline only: rejects exactly the chat-only, decisions-only
and alias models; chooses the balanced model over the cheapest-prompt one; the mock saw the bearer
and X-Mycel-End-User on every request.
--smoke prints one PASS/FAIL line per check and exits 0 only if all pass.
- README.md (YAML frontmatter: title, track, status, tests, needs, run, smoke, deck), PROMPT.md, FINDINGS.md.
Strict TypeScript, no `any`. Verify with pnpm exec tsc --noEmit -p examples/tsconfig.json,
pnpm exec eslint --no-ignore "examples/02-*/**/*.ts", and run.ts --smoke offline and --live --smoke.
Works in Pi, Claude Code, Codex & Antigravity
Offline mock first →
--live opt-in
What we learned
- Selection needs both prices. Ranking on prompt price alone picks the wrong model. The selector estimates $ per request from the task's prompt and completion token counts. Offline,
lab/cheap-prompthas the lowest prompt price, butlab/balancedis cheaper per request. - Filter before ranking. Chat-only and decisions-only models lack
tool-calling. Thedefaultalias routes to another listed model, so picking it hides the real choice. - Live today it picks
z-ai/glm-5.3-flash. It resolves asmycel:z-ai/glm-5.3-flashthroughresolvePiModeland calls the tool correctly. - Caching does not change the ranking today. Mycel meters prompt tokens itself and ignores
cached_tokens(ADR 0017), so a cache read costs the full prompt price. The selector ranks on price only. A cache-read price in/v1/modelswould let selection account for caching. Lab 03 covers caching. @umwelten/pidefaultsMYCEL_URLtolocalhost:7438. Set it explicitly for hosted Mycel.
Smoke output (2026-11-30)
Offline (--smoke), exit 0:
Visual Target Wireframe
ASCII Layout
PASS chosen model has every required capability and is not an alias PASS chosen model is the cheapest eligible one for the task profile PASS selection rejects chat-only, decisions-only and alias models PASS selection weighs completion price, not prompt price alone PASS mycel:lab/balanced resolves through resolvePiModel PASS turn called lookup_section(7) PASS answer quotes hollow-thistle PASS every chat request asked Mycel for the chosen model PASS mock saw the bearer and X-Mycel-End-User on every request 9/9 passed
Live (MYCEL_URL=https://mycel.thefocus.ai --live --smoke), exit 0:
Visual Target Wireframe
ASCII Layout
PASS chosen model has every required capability and is not an alias PASS chosen model is the cheapest eligible one for the task profile PASS mycel:z-ai/glm-5.3-flash resolves through resolvePiModel PASS turn called lookup_section(7) PASS answer quotes hollow-thistle PASS every chat request asked Mycel for the chosen model 6/6 passed