Artifact 30acdb740b2ad1e924ea211154b6dd50fdcc118866ad8acf7f85ec92a3fe8534:
- File
tools/model-catalog.sh
— part of check-in
[fe63b75bbd]
at
2026-11-19 23:24:39
on branch cost
— Model prices and real costs, as Pi's
The catalog snapshot (regenerated from pi.dev) keeps each model's per-Mtok prices and tiers, as do the pi.dev overlay and models.json. Each reply is priced as Pi's calculateCost does: tiers, 1-hour cache writes, per-API usage normalisation, OpenRouter's billed total. The cost and Pi's normalised token counts are stored with the generation and appear in usage events, the Pi export, pic models --json and, per conversation, pic sessions --json. (user: dmestas, size: 3128) [annotate] [blame] [check-ins using] [more...]
#!/bin/sh # Regenerates src/model_catalog.inc, pic's built-in snapshot of Pi's model # catalog (https://pi.dev/api/models), for the providers and APIs pic has: # one row per chat model with its Pi model object (id, name, api, baseUrl, # reasoning, contextWindow, maxTokens, cost, compat, thinkingLevelMap). At run time pic lays # fresher per-provider data from the same service over it # (~/.pic/models-store.json), as Pi does. Needs curl and sqlite3. # Usage: sh tools/model-catalog.sh [CATALOG_JSON] (fetches when not given) set -eu cd "$(dirname "$0")/.." tmp=$(mktemp -d "${TMPDIR:-/tmp}/pic-catalog.XXXXXX") trap 'rm -rf -- "$tmp"' EXIT HUP INT TERM if [ $# -ge 1 ]; then cp "$1" "$tmp/all.json" else curl -fsS -m 60 -A 'pic-catalog' -o "$tmp/all.json" 'https://pi.dev/api/models?types=chat' fi now=$(($(date +%s) * 1000)) out=src/model_catalog.inc { printf '/* Generated by tools/model-catalog.sh from Pi'"'"'s model catalog (https://pi.dev/api/models)\n' printf ' * on %s. Do not edit: regenerate. */\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" printf '#define PIC_CATALOG_GENERATED_MS %sLL\n' "$now" printf 'static const struct {\n const char *provider, *id, *json;\n} catalog_snapshot[] = {\n' sqlite3 :memory: " WITH d(j) AS (SELECT readfile('$tmp/all.json')), m AS (SELECT p.key AS provider, x.value AS v FROM d, json_each(d.j) p, json_each(p.value) x WHERE p.key IN ('anthropic','openai','openrouter','xai','groq','cerebras','deepseek','together', 'fireworks','huggingface','nvidia','moonshotai','zai') AND json_extract(x.value,'\$.type') = 'chat' AND json_extract(x.value,'\$.api') IN ('anthropic-messages','openai-completions','openai-responses')) SELECT ' {\"' || provider || '\", \"' || replace(json_extract(v,'\$.id'), '\"', '\\\"') || '\", \"' || replace(replace(CASE WHEN json_type(v,'\$.thinkingLevelMap') IS NULL THEN obj ELSE json_insert(obj, '\$.thinkingLevelMap', json(json_extract(v,'\$.thinkingLevelMap'))) END, '\\', '\\\\'), '\"', '\\\"') || '\"},' FROM (SELECT provider, v, json_object('id', json_extract(v,'\$.id'), 'name', json_extract(v,'\$.name'), 'api', json_extract(v,'\$.api'), 'baseUrl', json_extract(v,'\$.baseUrl'), 'reasoning', json(CASE WHEN json_extract(v,'\$.reasoning') THEN 'true' ELSE 'false' END), 'contextWindow', json_extract(v,'\$.contextWindow'), 'maxTokens', json_extract(v,'\$.maxTokens'), 'cost', json(coalesce(json_extract(v,'\$.cost'), '{\"input\":0,\"output\":0,\"cacheRead\":0,\"cacheWrite\":0}')), 'compat', json(coalesce(json_extract(v,'\$.compat'), '{}'))) AS obj FROM m) ORDER BY provider, json_extract(v,'\$.id')" printf '};\n' } > "$tmp/out" n=$(grep -c '^ {"' "$tmp/out") [ "$n" -gt 100 ] || { echo "model-catalog: only $n models; refusing to write $out" >&2; exit 1; } mv "$tmp/out" "$out" printf 'model-catalog: %s models written to %s\n' "$n" "$out"