graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
feat(pricing): hydrate price/context overlays from the LiteLLM price database
Rach Pradhan26aac5b10c66
diff --git a/src/commands_misc.zig b/src/commands_misc.zigindex 35595ec9..c00c308c 100644--- a/src/commands_misc.zig+++ b/src/commands_misc.zig@@ -41,6 +41,7 @@ const pricing = @import("pricing.zig");const obs = @import("obs.zig");const default_context = pricing.default_context;const g_cost = &pricing.g_cost;+const pricing_db = @import("pricing_db.zig"); // #557: price-DB age for the /models footerconst models_cache = @import("models_cache.zig");const kimi_catalog = @import("kimi_catalog.zig");const providers = @import("providers.zig");@@ -390,6 +391,7 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,}try out.print("(codex catalog: {s})\n", .{models_cache.codex_catalog_source});try out.print("(kimi catalog: {s})\n", .{kimi_catalog.catalog_source});+ if (pricing_db.source.len != 0) try out.print("(price db: {s})\n", .{pricing_db.source});try out.print("(unknown models: {d}k ctx; claude* → anthropic, else → codegraff)\n", .{default_context / 1000});// Live LM Studio models: query the local server so loaded models show up// in /models without hand-typing their ids. Best-effort and silent if thediff --git a/src/models_cache.zig b/src/models_cache.zigindex 10fd65b9..f9d23045 100644--- a/src/models_cache.zig+++ b/src/models_cache.zig@@ -12,6 +12,7 @@ const Value = std.json.Value;const Allocator = std.mem.Allocator;const pricing = @import("pricing.zig");+const pricing_db = @import("pricing_db.zig"); // #557: the LiteLLM price sheet refreshes on the same beat as models.devconst util = @import("util.zig");const strFieldObj = util.strFieldObj;const provider = @import("provider.zig"); // g_codex_url_override: keep /models discovery on the same origin as the overridden responses endpoint@@ -514,6 +515,8 @@ pub fn refresh(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, out:try out.print("✓ refreshed {d} models from models.dev → {s}\n", .{ metas.items.len, path });reportNew(doc.object, out);loadOverlay(io, arena, home); // apply to this process too+ pricing_db.ensure(io, gpa, arena, home, true); // strictly after loadOverlay: it appends, models.dev assigns+ if (pricing_db.source.len != 0) try out.print("✓ price database: {s}\n", .{pricing_db.source});try out.flush();}@@ -560,6 +563,7 @@ pub fn command(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, sub_areturn;}loadOverlay(io, arena, home); // reflect a prior refresh in the listing+ pricing_db.ensure(io, gpa, arena, home, false); // cached price sheet only; `refresh` is the online armtry list(out);}diff --git a/src/pricing_db.zig b/src/pricing_db.zignew file mode 100644index 00000000..131b5ddc--- /dev/null+++ b/src/pricing_db.zig@@ -0,0 +1,527 @@+//! #557 — hydrate graff's price/context overlay from the community LiteLLM+//! price database (`model_prices_and_context_window.json`).+//!+//! Layering, outermost first. Every layer is best-effort; a missing or broken+//! one simply falls through to the next, so the baked table in pricing.zig is+//! always a working floor:+//!+//! 1. `graff models refresh` (models.dev) — models_cache.loadOverlay installs+//! its rows FIRST, and priceFor/contextFor return the first match, so an+//! explicit refresh keeps winning over this automatic layer.+//! 2. this database — appended AFTER those rows, covering every catalog model+//! models.dev did not.+//! 3. pricing.price_table / pricing.model_table — the baked offline fallback.+//!+//! Inside one model the layering is per FIELD: a value the database omits keeps+//! whatever answered before (`pricing.priceFor`), never 0. Dropping a rate to+//! zero would silently make cached input free, and dropping grok-4.6's ≥200k+//! band would under-bill every long request — a stale number beats a wrong one.+//!+//! Cached tokens: `CostTally` counts cache READS only, so only+//! `cache_read_input_token_cost` is plumbed. Anthropic's cache-WRITE premium+//! (`cache_creation_input_token_cost`) is deliberately ignored: there is no+//! cache-write counter to bill it against, and folding it into the read rate+//! would over-bill every provider that has no write premium at all.+//!+//! Context: `max_input_tokens` ONLY. LiteLLM's `max_tokens` mirrors+//! `max_output_tokens` for chat rows (2402 of 3040 rows at time of writing), so+//! using it as a fallback would report a 64k window for a 200k model and fire+//! auto-compaction against a phantom wall.++const std = @import("std");+const Io = std.Io;+const Allocator = std.mem.Allocator;++const pricing = @import("pricing.zig");+const util = @import("util.zig");+const credential_store = @import("credential_store.zig");++pub const db_url = "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json";++/// A price sheet a day old is fine; the upstream file moves on release cadence.+pub const ttl_ms: i64 = 24 * 60 * 60 * 1000;+/// Upstream is ~1.8 MB. Anything past this is not the price database.+pub const max_bytes: usize = 8 * 1024 * 1024;++/// `GRAFF_PRICES_PATH` — pinned at startup, since the modules that trigger+/// hydration sit several calls below the one that holds the environment.+pub var path_override: []const u8 = "";++/// One dim line for the `/models` footer; empty until something hydrates.+pub var source: []const u8 = "";++var cache_loaded = false;+var live_loaded = false;+var layered = false;+var base_prices: []const pricing.ModelPrice = &.{};+var base_contexts: []const pricing.ModelInfo = &.{};++pub fn initOverride(path: ?[]const u8) void {+ const p = path orelse return;+ if (p.len != 0) path_override = p;+}++/// Forget the per-process latch and the captured overlay base. Tests use it;+/// so would a future in-session "re-read my price sheet" surface.+pub fn invalidate() void {+ cache_loaded = false;+ live_loaded = false;+ layered = false;+ base_prices = &.{};+ base_contexts = &.{};+ source = "";+}++// ── the upstream row ────────────────────────────────────────────────────────++/// Sentinel for "the database did not say", kept distinct from a real 0 so a+/// genuinely free model is not confused with a missing field.+const absent: f64 = -1;++/// The seven numbers we read out of one upstream model object. Costs are USD+/// per TOKEN here (upstream units); conversion to graff's per-1M happens in+/// `priceOf`, once, so the on-disk cache stays comparable with upstream.+const Fields = struct {+ in: f64 = absent,+ out: f64 = absent,+ cache: f64 = absent,+ high_in: f64 = absent,+ high_out: f64 = absent,+ high_cache: f64 = absent,+ context: f64 = absent,++ fn slot(self: *Fields, key: []const u8) ?*f64 {+ const eq = std.mem.eql;+ if (eq(u8, key, "input_cost_per_token")) return &self.in;+ if (eq(u8, key, "output_cost_per_token")) return &self.out;+ if (eq(u8, key, "cache_read_input_token_cost")) return &self.cache;+ if (eq(u8, key, "input_cost_per_token_above_200k_tokens")) return &self.high_in;+ if (eq(u8, key, "output_cost_per_token_above_200k_tokens")) return &self.high_out;+ if (eq(u8, key, "cache_read_input_token_cost_above_200k_tokens")) return &self.high_cache;+ if (eq(u8, key, "max_input_tokens")) return &self.context;+ return null;+ }++ fn usable(self: Fields) bool {+ return self.in >= 0 or self.out >= 0 or self.context > 0;+ }+};++pub const Row = struct { name: []const u8, fields: Fields };++/// Upstream publishes a `sample_spec` row whose values are prose ("max input+/// tokens, if the provider specifies it"). Reading numbers only — and requiring+/// at least one — drops it without a name-based special case.+fn readFields(scanner: *std.json.Scanner, sa: Allocator) !Fields {+ if (try scanner.next() != .object_begin) return error.NotAnObject;+ var f: Fields = .{};+ while (true) {+ switch (try scanner.peekNextTokenType()) {+ .object_end => {+ _ = try scanner.next();+ return f;+ },+ .string => {},+ else => return error.NotAnObject,+ }+ const key = switch (try scanner.nextAlloc(sa, .alloc_if_needed)) {+ .string, .allocated_string => |s| s,+ else => return error.NotAnObject,+ };+ const slot = f.slot(key) orelse {+ try scanner.skipValue();+ continue;+ };+ if (try scanner.peekNextTokenType() != .number) {+ try scanner.skipValue();+ continue;+ }+ const text = switch (try scanner.nextAlloc(sa, .alloc_if_needed)) {+ .number, .allocated_number => |s| s,+ else => continue,+ };+ const value = std.fmt.parseFloat(f64, text) catch continue;+ // `absent` is -1, and a cost cannot be negative or infinite: anything+ // else would read back as "the database did not say" or bill nonsense.+ if (std.math.isFinite(value) and value >= 0) slot.* = value;+ }+}++// ── matching upstream keys to graff's catalog ───────────────────────────────++/// LiteLLM keys are `[<billing vendor>/]<model id>`, and the LEADING segment is+/// who bills you: `openrouter/anthropic/claude-sonnet-4.6` is OpenRouter's+/// markup of an Anthropic model, not Anthropic's own row. These are the vendors+/// graff itself routes to, in LiteLLM's spellings — same intent as+/// models_cache's `canonical_providers`, read off the key so a row never has to+/// be parsed before it can be ranked.+const canonical_vendors = [_][]const u8{+ "openai", "anthropic", "deepseek", "xai", "zai",+ "z-ai", "zhipuai", "minimax", "moonshot", "moonshotai",+ "mistral", "xiaomi", "groq", "google", "gemini",+ "fireworks", "fireworks_ai", "fireworks-ai",+};++/// A reseller quotes the same weights at its own margin. Trusting it for a+/// CONTEXT window is safe — the window is a property of the model. Trusting it+/// for a PRICE is not: it would replace graff's hand-pinned direct-seat rate+/// with a middleman's markup (upstream today has only `openrouter/xiaomi/+/// mimo-v2.5-pro`, at 2.3x Xiaomi's own price). Below this rank a row+/// contributes its window and nothing else.+const trusted_price_rank: u8 = 2;++/// A graff catalog name plus its alias-normalized form, computed once instead+/// of once per (key, name) pair — upstream ships ~3000 keys against ~65 names.+const Target = struct { name: []const u8, norm: []const u8 };++const KeyForm = struct {+ raw: []const u8,+ norm: []const u8,+ tail_norm: []const u8,+ namespaced: bool,+ canonical: bool,+};++fn keyForm(key: []const u8, norm_buf: *[128]u8, tail_buf: *[128]u8) KeyForm {+ const first = std.mem.indexOfScalar(u8, key, '/') orelse return .{+ .raw = key,+ .norm = pricing.normalizeModelAlias(norm_buf, key),+ .tail_norm = "",+ .namespaced = false,+ .canonical = false,+ };+ var canonical = false;+ for (canonical_vendors) |v| if (std.ascii.eqlIgnoreCase(v, key[0..first])) {+ canonical = true;+ };+ const last = std.mem.lastIndexOfScalar(u8, key, '/').?;+ return .{+ .raw = key,+ .norm = pricing.normalizeModelAlias(norm_buf, key),+ .tail_norm = pricing.normalizeModelAlias(tail_buf, key[last + 1 ..]),+ .namespaced = true,+ .canonical = canonical,+ };+}++/// How well an upstream key names a graff model; higher wins, 0 is no match.+/// A bare key is the vendor's own row and outranks every namespaced spelling of+/// the same id, which is what keeps `xai/grok-4.3` ahead of `azure_ai/grok-4.3`+/// and `zai/glm-5` ahead of `baseten/zai-org/GLM-5`.+fn rankFor(k: KeyForm, name: []const u8, name_norm: []const u8) u8 {+ if (std.mem.eql(u8, k.raw, name)) return 6;+ if (!k.namespaced) return if (std.mem.eql(u8, k.norm, name_norm)) 4 else 0;+ if (std.mem.eql(u8, k.norm, name_norm)) return if (k.canonical) 3 else 1;+ if (k.tail_norm.len != 0 and std.mem.eql(u8, k.tail_norm, name_norm)) return if (k.canonical) 2 else 1;+ return 0;+}++const Hit = struct { index: usize, rank: u8 };++fn bestMatch(k: KeyForm, targets: []const Target, ranks: []const u8) ?Hit {+ var best: ?Hit = null;+ for (targets, 0..) |t, i| {+ const rank = rankFor(k, t.name, t.norm);+ if (rank == 0 or rank <= ranks[i]) continue;+ if (best) |b| if (rank <= b.rank) continue;+ best = .{ .index = i, .rank = rank };+ }+ return best;+}++/// Deduped graff catalog names, newest activation included (the codex/kimi/+/// router slices are already swapped in by the time hydration runs).+fn targetsOf(sa: Allocator) ![]const Target {+ var out: std.ArrayList(Target) = .empty;+ for (pricing.models()) |m| {+ var dup = false;+ for (out.items) |t| if (std.mem.eql(u8, t.name, m.name)) {+ dup = true;+ };+ if (dup) continue;+ const buf = try sa.create([128]u8);+ try out.append(sa, .{ .name = m.name, .norm = pricing.normalizeModelAlias(buf, m.name) });+ }+ return out.toOwnedSlice(sa);+}++/// One streaming pass over the database: every value whose key cannot beat the+/// current match for some graff model is skipped without being materialized, so+/// peak memory is the ~40 rows we keep, not the ~3000 rows upstream ships.+pub fn scan(gpa: Allocator, arena: Allocator, data: []const u8) ?[]const Row {+ if (data.len == 0 or data.len > max_bytes) return null;+ var scratch = std.heap.ArenaAllocator.init(gpa);+ defer scratch.deinit();+ const sa = scratch.allocator();++ const targets = targetsOf(sa) catch return null;+ const ranks = sa.alloc(u8, targets.len) catch return null;+ const found = sa.alloc(Fields, targets.len) catch return null;+ @memset(ranks, 0);++ var scanner: std.json.Scanner = .initCompleteInput(sa, data);+ defer scanner.deinit();+ if ((scanner.next() catch return null) != .object_begin) return null;+ var norm_buf: [128]u8 = undefined;+ var tail_buf: [128]u8 = undefined;+ while (true) {+ switch (scanner.peekNextTokenType() catch return null) {+ .object_end => break,+ .string => {},+ else => return null,+ }+ const key = switch (scanner.nextAlloc(sa, .alloc_if_needed) catch return null) {+ .string, .allocated_string => |s| s,+ else => return null,+ };+ const hit = bestMatch(keyForm(key, &norm_buf, &tail_buf), targets, ranks) orelse {+ scanner.skipValue() catch return null;+ continue;+ };+ // A matched key whose value is not an object is upstream's problem, not+ // a reason to throw away 3000 good rows — skip it and keep scanning.+ if ((scanner.peekNextTokenType() catch return null) != .object_begin) {+ scanner.skipValue() catch return null;+ continue;+ }+ const fields = readFields(&scanner, sa) catch return null;+ if (!fields.usable()) continue;+ ranks[hit.index] = hit.rank;+ found[hit.index] = fields;+ }++ var rows: std.ArrayList(Row) = .empty;+ for (targets, 0..) |t, i| {+ if (ranks[i] == 0) continue;+ var fields = found[i];+ // Strip an untrusted price HERE, not at apply time, so the trimmed+ // cache we write never carries a markup that would reload as a+ // vendor-grade row (its keys are graff names, i.e. exact matches).+ if (ranks[i] < trusted_price_rank) fields = .{ .context = fields.context };+ if (!fields.usable()) continue;+ const owned = arena.dupe(u8, t.name) catch continue;+ rows.append(arena, .{ .name = owned, .fields = fields }) catch continue;+ }+ if (rows.items.len == 0) return null;+ return rows.toOwnedSlice(arena) catch null;+}++// ── overlay assembly ────────────────────────────────────────────────────────++/// Per-token USD to graff's per-1M USD. Rounded to nano-dollars because the+/// multiply is not exact in binary: zai's 3.2e-6 comes back as+/// 3.1999999999999997, which `graff models` would print verbatim.+fn perMillion(v: f64, fallback: f64) f64 {+ if (v < 0) return fallback;+ return @round(v * 1_000_000.0 * 1e9) / 1e9;+}++/// Per-field layering (see the module header): upstream when it says, the+/// currently effective price when it doesn't, 0 only when nothing knows.+fn priceOf(row: Row) pricing.ModelPrice {+ const prev = pricing.priceFor(row.name) orelse pricing.ModelPrice{ .name = row.name, .in = 0, .out = 0, .cache = 0 };+ const f = row.fields;+ const in = perMillion(f.in, prev.in);+ const out = perMillion(f.out, prev.out);+ const cache = perMillion(f.cache, prev.cache);+ // A ≥200k band needs BOTH of its dominant rates. Half a tier is no tier:+ // taking `cache_read_..._above_200k_tokens` alone would switch usdFor into+ // a band whose input rate is whatever the flat baked row had — often 0, i.e.+ // free input on exactly the largest requests. Keep the baked band instead.+ if (!(f.high_in >= 0 and f.high_out >= 0)) return .{+ .name = row.name,+ .in = in,+ .out = out,+ .cache = cache,+ .high_at = prev.high_at,+ .high_in = prev.high_in,+ .high_out = prev.high_out,+ .high_cache = prev.high_cache,+ };+ return .{+ .name = row.name,+ .in = in,+ .out = out,+ .cache = cache,+ .high_at = 200_000,+ .high_in = perMillion(f.high_in, 0),+ .high_out = perMillion(f.high_out, 0),+ // Upstream tiers input/output far more often than cache reads; the base+ // cache rate is the honest floor for the band, never 0.+ .high_cache = perMillion(f.high_cache, if (prev.high_cache > 0) prev.high_cache else cache),+ };+}++/// Append our rows to whatever the models.dev refresh already installed. The+/// context rows carry `provider = ""` on purpose: contextFor consults a baked+/// provider-specific row first (so the gateway's narrower gpt-5.5 window still+/// wins) and clamps EVERY codex resolution to codex_context_window, so a wide+/// upstream number can never lift the codex cap.+pub fn apply(arena: Allocator, rows: []const Row) void {+ if (!layered) {+ base_prices = pricing.price_overlay;+ base_contexts = pricing.context_overlay;+ layered = true;+ }+ var prices: std.ArrayList(pricing.ModelPrice) = .empty;+ var contexts: std.ArrayList(pricing.ModelInfo) = .empty;+ prices.appendSlice(arena, base_prices) catch return;+ contexts.appendSlice(arena, base_contexts) catch return;+ for (rows) |row| {+ if (row.fields.in >= 0 or row.fields.out >= 0) prices.append(arena, priceOf(row)) catch {};+ // Range-checked before the cast: @intFromFloat past u64 is undefined+ // behaviour, and a hand-edited prices.json is an untrusted input.+ if (row.fields.context >= 1 and row.fields.context < 1e15)+ contexts.append(arena, .{ .provider = "", .name = row.name, .context = @intFromFloat(row.fields.context) }) catch {};+ }+ pricing.price_overlay = prices.toOwnedSlice(arena) catch pricing.price_overlay;+ pricing.context_overlay = contexts.toOwnedSlice(arena) catch pricing.context_overlay;+}++// ── on-disk cache ───────────────────────────────────────────────────────────++/// Re-emit the matched rows in the SAME shape as upstream, so one parser reads+/// both and `GRAFF_PRICES_PATH` accepts either a vendored copy of the database+/// or one of these. Keyed by graff's model name, which reloads as an exact+/// match. `{e}` round-trips an f64 through parseFloat; `{d}` would not.+pub fn serialize(arena: Allocator, rows: []const Row) ?[]const u8 {+ var aw: Io.Writer.Allocating = .init(arena);+ aw.writer.writeByte('{') catch return null;+ for (rows, 0..) |row, i| {+ if (i > 0) aw.writer.writeByte(',') catch return null;+ var name: std.json.Stringify = .{ .writer = &aw.writer };+ name.write(row.name) catch return null;+ aw.writer.writeAll(":{") catch return null;+ var wrote: usize = 0;+ inline for (.{+ .{ "input_cost_per_token", "in" },+ .{ "output_cost_per_token", "out" },+ .{ "cache_read_input_token_cost", "cache" },+ .{ "input_cost_per_token_above_200k_tokens", "high_in" },+ .{ "output_cost_per_token_above_200k_tokens", "high_out" },+ .{ "cache_read_input_token_cost_above_200k_tokens", "high_cache" },+ .{ "max_input_tokens", "context" },+ }) |pair| {+ const value = @field(row.fields, pair[1]);+ if (value >= 0) {+ if (wrote > 0) aw.writer.writeByte(',') catch return null;+ aw.writer.print("\"{s}\":{e}", .{ pair[0], value }) catch return null;+ wrote += 1;+ }+ }+ aw.writer.writeByte('}') catch return null;+ }+ aw.writer.writeAll("}\n") catch return null;+ return aw.writer.buffered();+}++/// Age of `path` from its mtime, or null when it is not there. Path-based on+/// purpose: `stat` on a WRITE-ONLY handle is ACCESS_DENIED on Windows (#462),+/// and this runs on every platform graff ships to.+fn ageMs(io: Io, dir: Io.Dir, path: []const u8) ?i64 {+ if (path.len == 0) return null;+ const st = dir.statFile(io, path, .{}) catch return null;+ return util.unixMs(io) - @as(i64, @intCast(@divTrunc(st.mtime.nanoseconds, std.time.ns_per_ms)));+}++fn readCapped(io: Io, arena: Allocator, dir: Io.Dir, path: []const u8) ?[]const u8 {+ if (path.len == 0) return null;+ return dir.readFileAlloc(io, path, arena, .limited(max_bytes)) catch null;+}++fn fetch(io: Io, gpa: Allocator, arena: Allocator) ?[]const u8 {+ var client: std.http.Client = .{ .allocator = gpa, .io = io };+ defer client.deinit();+ var aw: Io.Writer.Allocating = .init(arena);+ const extra = [_]std.http.Header{+ .{ .name = "Accept", .value = "application/json" },+ .{ .name = "User-Agent", .value = "graff-prices/1.0" },+ };+ const res = client.fetch(.{+ .location = .{ .url = db_url },+ .method = .GET,+ .response_writer = &aw.writer,+ .extra_headers = &extra,+ }) catch return null;+ if (@intFromEnum(res.status) != 200) return null;+ const body = aw.writer.buffered();+ return if (body.len == 0 or body.len > max_bytes) null else body;+}++// ── hydration ───────────────────────────────────────────────────────────────++pub const Paths = struct { override: []const u8 = "", cache: []const u8 = "", flat: []const u8 = "" };++fn cachePaths(arena: Allocator, home: []const u8) Paths {+ if (home.len == 0) return .{ .override = path_override };+ return .{+ .override = path_override,+ .cache = std.fmt.allocPrint(arena, "{s}/.codegraff/prices.json", .{home}) catch "",+ .flat = std.fmt.allocPrint(arena, "{s}/.codegraff-prices.json", .{home}) catch "",+ };+}++fn note(arena: Allocator, comptime fmt: []const u8, args: anytype) []const u8 {+ return std.fmt.allocPrint(arena, fmt, args) catch "litellm";+}++/// The whole policy, in one place and testable against a `tmpDir`:+/// `GRAFF_PRICES_PATH` beats everything, then a cache younger than the TTL,+/// then the network, then a stale cache (offline beats nothing). Every arm is+/// silent — a failure just leaves the baked table authoritative.+pub fn hydrateAt(io: Io, gpa: Allocator, arena: Allocator, dir: Io.Dir, paths: Paths, allow_network: bool) void {+ if (readCapped(io, arena, dir, paths.override)) |data| {+ if (scan(gpa, arena, data)) |rows| {+ apply(arena, rows);+ source = note(arena, "GRAFF_PRICES_PATH · {d} model(s)", .{rows.len});+ }+ return; // an explicit override is the answer, fresh or not+ }+ var path: []const u8 = "";+ var age: i64 = std.math.maxInt(i64);+ for ([_][]const u8{ paths.cache, paths.flat }) |candidate| {+ if (ageMs(io, dir, candidate)) |a| {+ path = candidate;+ age = a;+ break;+ }+ }+ if (path.len != 0 and age >= 0 and age < ttl_ms) {+ if (readCapped(io, arena, dir, path)) |data| if (scan(gpa, arena, data)) |rows| {+ apply(arena, rows);+ source = note(arena, "litellm cache · {d}h old · {d} model(s)", .{ @divTrunc(age, 60 * 60 * 1000), rows.len });+ return;+ };+ }+ if (allow_network) {+ if (fetch(io, gpa, arena)) |body| if (scan(gpa, arena, body)) |rows| {+ apply(arena, rows);+ source = note(arena, "litellm live · {d} model(s)", .{rows.len});+ if (serialize(arena, rows)) |bytes| for ([_][]const u8{ paths.cache, paths.flat }) |p| {+ if (p.len == 0) continue;+ credential_store.replaceFile(io, dir, p, bytes, .default_file) catch continue;+ break;+ };+ return;+ };+ }+ // Offline with only a stale sheet: yesterday's prices beat last release's.+ if (path.len != 0) if (readCapped(io, arena, dir, path)) |data| if (scan(gpa, arena, data)) |rows| {+ apply(arena, rows);+ source = note(arena, "litellm cache (stale) · {d} model(s)", .{rows.len});+ };+}++/// Hydrate at most once per process per mode. `allow_network = false` is the+/// catalog-ensure path (cache/override only, so boot never waits on a 2 MB+/// GET); `allow_network = true` is the explicit `/models` freshness moment.+pub fn ensure(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, allow_network: bool) void {+ if (if (allow_network) live_loaded else cache_loaded) return;+ cache_loaded = true;+ if (allow_network) live_loaded = true;+ hydrateAt(io, gpa, arena, Io.Dir.cwd(), cachePaths(arena, home), allow_network);+}++test {+ _ = @import("pricing_db_tests.zig");+}diff --git a/src/pricing_db_tests.zig b/src/pricing_db_tests.zignew file mode 100644index 00000000..4f4323d5--- /dev/null+++ b/src/pricing_db_tests.zig@@ -0,0 +1,301 @@+//! #557 tests for the LiteLLM price-database overlay. Split out of+//! pricing_db.zig to keep that file inside the 600-line cap; reached from the+//! test root through its `test { _ = @import(...) }` block.+//!+//! The overlay pointers in pricing.zig are process globals. Zig's test runner+//! executes tests sequentially in one process, so save-and-`defer`-restore is+//! sound here — but the restore must be unconditional, or one failing test+//! leaks a fake price sheet into every test that runs after it.++const std = @import("std");+const builtin = @import("builtin");+const Io = std.Io;++const pricing = @import("pricing.zig");+const db = @import("pricing_db.zig");++/// Hand-written in the upstream shape. Exercises, in order: the prose-valued+/// `sample_spec` row upstream really ships; a bare vendor row; a reseller row+/// that must LOSE to the bare key for the same model; that bare key; a row with+/// no cache rate (the per-field fallback); a namespaced key whose vendor IS the+/// model's vendor (trusted, banded); a reseller-ONLY model (window yes, price+/// no); a HALF band (one tier rate, which must not become a band); a matched+/// key whose value is not an object; and a model graff has never heard of.+const fixture =+ \\{+ \\ "sample_spec": {"input_cost_per_token": "0.0", "max_input_tokens": "max input tokens, if the provider specifies it"},+ \\ "grok-4.3": {"litellm_provider": "xai", "mode": "chat", "input_cost_per_token": 0.000003, "output_cost_per_token": 0.000015, "cache_read_input_token_cost": 0.0000003, "max_input_tokens": 300000, "supports_vision": true},+ \\ "openrouter/deepseek/deepseek-v4-flash": {"input_cost_per_token": 0.00000999, "output_cost_per_token": 0.00000999, "max_input_tokens": 111111},+ \\ "deepseek-v4-flash": {"input_cost_per_token": 0.0000005, "output_cost_per_token": 0.000001, "cache_read_input_token_cost": 0.0000001, "max_input_tokens": 700000},+ \\ "gpt-5.5": {"input_cost_per_token": 0.000002, "output_cost_per_token": 0.00001, "max_input_tokens": 900000},+ \\ "xai/grok-4.6": {"input_cost_per_token": 0.000002, "output_cost_per_token": 0.000006, "input_cost_per_token_above_200k_tokens": 0.000009, "output_cost_per_token_above_200k_tokens": 0.00002},+ \\ "openrouter/xiaomi/mimo-v2.5-pro": {"input_cost_per_token": 0.000001, "output_cost_per_token": 0.000003, "max_input_tokens": 1048576},+ \\ "glm-5.2": {"input_cost_per_token": 0.0000012, "output_cost_per_token": 0.000004, "cache_read_input_token_cost_above_200k_tokens": 0.0000005},+ \\ "kimi-k2.7": ["upstream typo: not an object"],+ \\ "some-model-graff-has-never-heard-of": {"input_cost_per_token": 0.001, "max_input_tokens": 5}+ \\}+;++const override_fixture =+ \\{"grok-4.3": {"input_cost_per_token": 0.000004, "output_cost_per_token": 0.00002, "max_input_tokens": 123000}}+;++/// Restores every global these tests touch, pass or fail.+const Guard = struct {+ prices: []const pricing.ModelPrice,+ contexts: []const pricing.ModelInfo,++ fn take() Guard {+ db.invalidate();+ return .{ .prices = pricing.price_overlay, .contexts = pricing.context_overlay };+ }+ fn release(self: Guard) void {+ pricing.price_overlay = self.prices;+ pricing.context_overlay = self.contexts;+ db.invalidate();+ }+};++fn hydrate(arena: std.mem.Allocator, dir: Io.Dir, paths: db.Paths) void {+ db.hydrateAt(std.testing.io, std.testing.allocator, arena, dir, paths, false);+}++test "price db: per-token costs convert exactly to graff's per-1M units" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const guard = Guard.take();+ defer guard.release();++ const rows = db.scan(std.testing.allocator, arena, fixture) orelse return error.NoRows;+ db.apply(arena, rows);++ const p = pricing.priceFor("grok-4.3") orelse return error.MissingOverlayRow;+ // 0.000003 USD/token is exactly 3.0 USD per 1M tokens, and both endpoints+ // are representable, so this is an equality — not an approx — on purpose.+ try std.testing.expectEqual(@as(f64, 3.0), p.in);+ try std.testing.expectEqual(@as(f64, 15.0), p.out);+ try std.testing.expectEqual(@as(f64, 0.3), p.cache);+ // usdFor reads the same row: 1M uncached in + 1M out at those rates.+ try std.testing.expectEqual(@as(f64, 18.0), pricing.usdFor(p, 1_000_000, 0, 1_000_000));+}++test "price db: the overlay wins, and the baked table survives where it is silent" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const guard = Guard.take();+ defer guard.release();++ // Baked, before anything is applied.+ try std.testing.expectEqual(@as(f64, 1.25), (pricing.priceFor("grok-4.3") orelse return error.MissingBakedRow).in);++ const rows = db.scan(std.testing.allocator, arena, fixture) orelse return error.NoRows;+ db.apply(arena, rows);++ // Covered by the database → the fresh number answers.+ try std.testing.expectEqual(@as(f64, 3.0), (pricing.priceFor("grok-4.3") orelse return error.MissingOverlayRow).in);+ // kimi-k2.7's fixture value is an ARRAY. One malformed row must not cost+ // the other six their prices, so it is skipped and the baked row answers.+ const kimi = pricing.priceFor("kimi-k2.7") orelse return error.MissingBakedRow;+ try std.testing.expectEqual(@as(f64, 0.95), kimi.in);+ try std.testing.expectEqual(@as(f64, 4.0), kimi.out);+ // A model graff does not carry is never added: the overlay is a price sheet+ // for OUR catalog, not a copy of a 3000-row database.+ try std.testing.expect(pricing.priceFor("some-model-graff-has-never-heard-of") == null);+}++test "price db: a field the database omits keeps the baked value, never 0" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const guard = Guard.take();+ defer guard.release();++ const rows = db.scan(std.testing.allocator, arena, fixture) orelse return error.NoRows;+ db.apply(arena, rows);++ // gpt-5.5's fixture row has no cache_read rate. Zeroing it would bill every+ // cached prompt token at $0 — the baked 0.5 stands in instead.+ const p = pricing.priceFor("gpt-5.5") orelse return error.MissingOverlayRow;+ try std.testing.expectEqual(@as(f64, 2.0), p.in);+ try std.testing.expectEqual(@as(f64, 10.0), p.out);+ try std.testing.expectEqual(@as(f64, 0.5), p.cache);++ // glm-5.2's fixture row tiers ONLY the cache read. Adopting a band from+ // that would leave high_in at the flat row's 0 and make input free past+ // 200k, so the band is refused while the fresh base rates are taken.+ const half = pricing.priceFor("glm-5.2") orelse return error.MissingOverlayRow;+ try std.testing.expectEqual(@as(u64, 0), half.high_at);+ try std.testing.expectEqual(@as(f64, 1.2), half.in);+ try std.testing.expectEqual(@as(f64, 4.0), half.out);++ // grok-4.6 IS banded upstream, so the band comes across whole.+ const banded = pricing.priceFor("grok-4.6") orelse return error.MissingOverlayRow;+ try std.testing.expectEqual(@as(u64, 200_000), banded.high_at);+ try std.testing.expectEqual(@as(f64, 9.0), banded.high_in);+ try std.testing.expectEqual(@as(f64, 20.0), banded.high_out);+ // A 200k prompt bills at the high band, exactly as the baked row used to.+ try std.testing.expectEqual(@as(f64, 1.8), pricing.usdFor(banded, 200_000, 0, 0));+}++test "price db: a reseller may set the window but never the price" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const guard = Guard.take();+ defer guard.release();++ const rows = db.scan(std.testing.allocator, arena, fixture) orelse return error.NoRows;+ db.apply(arena, rows);++ // mimo-v2.5-pro exists upstream ONLY as `openrouter/xiaomi/...`, at 2.3x+ // Xiaomi's own rate. The window is a property of the weights and is taken;+ // the markup is not, so graff's direct-seat price stands.+ const p = pricing.priceFor("mimo-v2.5-pro") orelse return error.MissingBakedRow;+ try std.testing.expectEqual(@as(f64, 0.435), p.in);+ try std.testing.expectEqual(@as(f64, 0.87), p.out);+ try std.testing.expectEqual(@as(u64, 1_048_576), pricing.contextFor("nope", "mimo-v2.5-pro"));++ // A namespaced key whose leading segment IS the vendor is trusted in full.+ const grok = pricing.priceFor("grok-4.6") orelse return error.MissingOverlayRow;+ try std.testing.expectEqual(@as(f64, 2.0), grok.in);+ try std.testing.expectEqual(@as(f64, 6.0), grok.out);+}++test "price db: a vendor's own key beats a reseller's namespaced copy" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const guard = Guard.take();+ defer guard.release();++ const rows = db.scan(std.testing.allocator, arena, fixture) orelse return error.NoRows;+ db.apply(arena, rows);++ // The reseller row sits FIRST in the fixture and quotes 20x the price.+ const p = pricing.priceFor("deepseek-v4-flash") orelse return error.MissingOverlayRow;+ try std.testing.expectEqual(@as(f64, 0.5), p.in);+ try std.testing.expectEqual(@as(u64, 700_000), pricing.contextFor("nope", "deepseek-v4-flash"));+}++test "price db: context comes from max_input_tokens, and the codex cap still applies" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const guard = Guard.take();+ defer guard.release();++ const rows = db.scan(std.testing.allocator, arena, fixture) orelse return error.NoRows;+ db.apply(arena, rows);++ // Name-only overlay row, for a provider with no baked row of its own.+ try std.testing.expectEqual(@as(u64, 300_000), pricing.contextFor("nope", "grok-4.3"));+ // A baked PROVIDER-specific row still wins: the gateway's 1M gpt-5.5 window+ // is deliberate configuration, not a stale number the database may correct.+ try std.testing.expectEqual(@as(u64, 1_050_000), pricing.contextFor("openai", "gpt-5.5"));+ // And no upstream number can lift the codex cap — 300k clamps to 270k.+ try std.testing.expectEqual(pricing.codex_context_window, pricing.contextFor("codex", "grok-4.3"));+}++test "price db: malformed or oversized input falls back silently" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const guard = Guard.take();+ defer guard.release();++ const before = pricing.price_overlay;+ for ([_][]const u8{ "", "{not json", "[]", "\"a string\"", "{\"grok-4.3\": 7}" }) |bad|+ try std.testing.expect(db.scan(std.testing.allocator, arena, bad) == null);+ // Nothing was applied, so the effective price is still the baked one.+ try std.testing.expectEqual(before.len, pricing.price_overlay.len);+ try std.testing.expectEqual(@as(f64, 1.25), (pricing.priceFor("grok-4.3") orelse return error.MissingBakedRow).in);+}++test "price db: GRAFF_PRICES_PATH wins over a fresh on-disk cache" {+ if (builtin.os.tag == .windows) return;+ const io = std.testing.io;+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const guard = Guard.take();+ defer guard.release();++ try tmp.dir.writeFile(io, .{ .sub_path = "prices.json", .data = fixture });+ try tmp.dir.writeFile(io, .{ .sub_path = "override.json", .data = override_fixture });+ hydrate(arena, tmp.dir, .{ .override = "override.json", .cache = "prices.json" });++ try std.testing.expectEqual(@as(f64, 4.0), (pricing.priceFor("grok-4.3") orelse return error.MissingOverlayRow).in);+ try std.testing.expectEqual(@as(u64, 123_000), pricing.contextFor("nope", "grok-4.3"));+ try std.testing.expect(std.mem.startsWith(u8, db.source, "GRAFF_PRICES_PATH"));+}++test "price db: a cache younger than the TTL is used; a stale one is labelled" {+ if (builtin.os.tag == .windows) return;+ const io = std.testing.io;+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const guard = Guard.take();+ defer guard.release();++ try tmp.dir.writeFile(io, .{ .sub_path = "prices.json", .data = fixture });+ hydrate(arena, tmp.dir, .{ .cache = "prices.json" });+ try std.testing.expectEqual(@as(f64, 3.0), (pricing.priceFor("grok-4.3") orelse return error.MissingOverlayRow).in);+ try std.testing.expect(std.mem.startsWith(u8, db.source, "litellm cache ·"));++ // Backdate the file past the TTL. Offline, a stale sheet is still better+ // than a release-old baked table — but it says so.+ db.invalidate();+ pricing.price_overlay = guard.prices;+ pricing.context_overlay = guard.contexts;+ const stale_ns = Io.Timestamp.now(io, .real).nanoseconds - @as(i96, db.ttl_ms + 60_000) * std.time.ns_per_ms;+ try tmp.dir.setTimestamps(io, "prices.json", .{ .modify_timestamp = .{ .new = .{ .nanoseconds = stale_ns } } });+ hydrate(arena, tmp.dir, .{ .cache = "prices.json" });+ try std.testing.expectEqual(@as(f64, 3.0), (pricing.priceFor("grok-4.3") orelse return error.MissingOverlayRow).in);+ try std.testing.expect(std.mem.startsWith(u8, db.source, "litellm cache (stale)"));+}++test "price db: no cache and no network leaves the baked table untouched" {+ if (builtin.os.tag == .windows) return;+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const guard = Guard.take();+ defer guard.release();++ const before = pricing.price_overlay;+ hydrate(arena, tmp.dir, .{ .cache = "prices.json", .flat = "flat-prices.json" });+ try std.testing.expectEqual(before.len, pricing.price_overlay.len);+ try std.testing.expectEqualStrings("", db.source);+ try std.testing.expectEqual(@as(f64, 1.25), (pricing.priceFor("grok-4.3") orelse return error.MissingBakedRow).in);+}++test "price db: the trimmed cache we write reloads to the same numbers" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const guard = Guard.take();+ defer guard.release();++ const rows = db.scan(std.testing.allocator, arena, fixture) orelse return error.NoRows;+ const bytes = db.serialize(arena, rows) orelse return error.NotSerialized;+ // A round trip through our own writer must be lossless: the cache is what+ // every later session reads instead of the 1.8 MB upstream file.+ const reloaded = db.scan(std.testing.allocator, arena, bytes) orelse return error.NoRows;+ try std.testing.expectEqual(rows.len, reloaded.len);+ db.apply(arena, reloaded);+ const p = pricing.priceFor("grok-4.3") orelse return error.MissingOverlayRow;+ try std.testing.expectEqual(@as(f64, 3.0), p.in);+ try std.testing.expectEqual(@as(f64, 0.3), p.cache);+ try std.testing.expectEqual(@as(u64, 300_000), pricing.contextFor("nope", "grok-4.3"));+ // The trimmed sheet is a rounding error next to upstream's ~1.8 MB.+ try std.testing.expect(bytes.len < 2048);+}diff --git a/src/router_catalog.zig b/src/router_catalog.zigindex ba2d4db6..9fa99c99 100644--- a/src/router_catalog.zig+++ b/src/router_catalog.zig@@ -23,6 +23,7 @@ const catalog_selection = @import("catalog_selection.zig");const credential_store = @import("credential_store.zig");const kimi_catalog = @import("kimi_catalog.zig");const pricing = @import("pricing.zig");+const pricing_db = @import("pricing_db.zig"); // #557: LiteLLM price/context overlay, hydrated on the same beats as these catalogsconst provider = @import("provider.zig");const serde = @import("serde.zig");const util = @import("util.zig");@@ -485,6 +486,7 @@ pub fn ensureForStartup(io: Io, gpa: Allocator, arena: Allocator, home: []const/// remains the offline fallback, and each live list is re-cached for the/// next boot. Returns how many providers were pinged.pub fn refreshForListing(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, keys: provider.Keys) usize {+ pricing_db.ensure(io, gpa, arena, home, true); // the same freshness moment covers the price sheet, network and allvar futures: [provider.provider_specs.len + 1]?Io.Future(FetchOutcome) = @splat(null);var pinged: usize = 0;for (provider.provider_specs, 0..) |spec, index| {@@ -510,6 +512,9 @@ pub fn refreshForListing(io: Io, gpa: Allocator, arena: Allocator, home: []const}pub fn ensureForQuery(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, keys: provider.Keys, query: []const u8) void {+ // Cache/override only: this runs at boot too, and no model surface is worth+ // a 2 MB GET on the hot path. refreshForListing is the online arm.+ pricing_db.ensure(io, gpa, arena, home, false);for (provider.provider_specs, 0..) |spec, index| {if (!dynamic(spec) or !catalog_selection.queryMayUse(spec.id, query)) continue;ensureAt(io, gpa, arena, home, keys, index);diff --git a/src/startup.zig b/src/startup.zigindex b577bca9..f0a0fda5 100644--- a/src/startup.zig+++ b/src/startup.zig@@ -32,6 +32,7 @@ const provider_mod = @import("provider.zig");const keys_cli = @import("keys_cli.zig");const oauth = @import("oauth.zig");const models_cache = @import("models_cache.zig");+const pricing_db = @import("pricing_db.zig");const kimi_catalog = @import("kimi_catalog.zig");const router_config = @import("router_config.zig");const router_catalog = @import("router_catalog.zig");@@ -199,6 +200,10 @@ pub fn runSubcommand(io: Io, gpa: Allocator, arena: Allocator, init: std.process// agents (the pre-compaction note, title, reflect) resolve the ONE file// the login flow writes instead of "/.kimi/...".oauth.initHome(keys_cli.homeEnv(init.environ_map) orelse "");+ // #557: same pin for GRAFF_PRICES_PATH, before `graff models` is dispatched+ // below — the hydration points (router_catalog, models_cache) all sit well+ // under the last call that still holds the environment.+ pricing_db.initOverride(init.environ_map.get("GRAFF_PRICES_PATH"));// `harness key set <provider> <key>` / `harness key list`: safe key store// (macOS Keychain, else a 0600 file). Exits after.