Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

Price hosted-gateway models from the gateway's own catalog

justrach1c4cd541a7fd
diff --git a/src/args.zig b/src/args.zig
index 1d02a056..33fc9386 100644
--- a/src/args.zig
+++ b/src/args.zig
@@ -167,7 +167,7 @@ pub fn parse(init: std.process.Init) !Flags {
} else if (std.mem.eql(u8, arg, "--json")) {
main_mod.json_mode = true;
} else if (std.mem.eql(u8, arg, "--max-tool-calls")) {
- const mv = it.next() orelse std.process.fatal("--max-tool-calls needs a non-negative integer — harness --help", .{});
+ const mv = it.next() orelse std.process.fatal("--max-tool-calls needs a non-negative integer — graff --help", .{});
main_mod.max_tool_calls = std.fmt.parseInt(u64, mv, 10) catch std.process.fatal("--max-tool-calls needs a non-negative integer, got '{s}'", .{mv});
} else if (std.mem.eql(u8, arg, "--compact-at")) {
const mv = it.next() orelse std.process.fatal("--compact-at needs a percent of the context window (10-95)", .{});
@@ -211,48 +211,48 @@ pub fn parse(init: std.process.Init) !Flags {
} else if (std.mem.eql(u8, arg, "--beta")) {
flags.update_beta = true;
} else if (std.mem.eql(u8, arg, "--resume")) {
- const rv = it.next() orelse std.process.fatal("--resume needs a session name — harness --help", .{});
+ const rv = it.next() orelse std.process.fatal("--resume needs a session name — graff --help", .{});
flags.resume_flag = try arena.dupe(u8, rv);
} else if (std.mem.eql(u8, arg, "--branch")) {
- const bv = it.next() orelse std.process.fatal("--branch needs a destination session name — harness --help", .{});
+ const bv = it.next() orelse std.process.fatal("--branch needs a destination session name — graff --help", .{});
flags.branch_flag = try arena.dupe(u8, bv);
} else if (std.mem.eql(u8, arg, "--no-resume")) {
flags.no_resume_flag = true;
} else if (std.mem.eql(u8, arg, "--new")) {
flags.new_session_flag = true;
} else if (std.mem.eql(u8, arg, "--model")) {
- const mv = it.next() orelse std.process.fatal("--model needs a value — harness --help", .{});
+ const mv = it.next() orelse std.process.fatal("--model needs a value — graff --help", .{});
flags.model_flag = try arena.dupe(u8, mv);
} else if (std.mem.eql(u8, arg, "--subagent-provider")) {
- const pv = it.next() orelse std.process.fatal("--subagent-provider needs a value — harness --help", .{});
+ const pv = it.next() orelse std.process.fatal("--subagent-provider needs a value — graff --help", .{});
flags.subagent_provider_flag = try arena.dupe(u8, pv);
} else if (std.mem.eql(u8, arg, "--subagent-model")) {
- const mv = it.next() orelse std.process.fatal("--subagent-model needs a value — harness --help", .{});
+ const mv = it.next() orelse std.process.fatal("--subagent-model needs a value — graff --help", .{});
flags.subagent_model_flag = try arena.dupe(u8, mv);
} else if (std.mem.eql(u8, arg, "--allow-cross-provider-subagents")) {
flags.allow_cross_provider_subagents_flag = true;
} else if (std.mem.eql(u8, arg, "--no-subagent-tier")) {
flags.no_subagent_tier_flag = true;
} else if (std.mem.eql(u8, arg, "--system-prompt")) {
- const sv = it.next() orelse std.process.fatal("--system-prompt needs a value — harness --help", .{});
+ const sv = it.next() orelse std.process.fatal("--system-prompt needs a value — graff --help", .{});
flags.system_prompt_flag = try arena.dupe(u8, sv);
} else if (std.mem.eql(u8, arg, "--append-system-prompt")) {
- const av = it.next() orelse std.process.fatal("--append-system-prompt needs a value — harness --help", .{});
+ const av = it.next() orelse std.process.fatal("--append-system-prompt needs a value — graff --help", .{});
flags.append_system_flag = try arena.dupe(u8, av);
} else if (std.mem.eql(u8, arg, "--host")) {
- const hv = it.next() orelse std.process.fatal("--host needs a value — harness --help", .{});
+ const hv = it.next() orelse std.process.fatal("--host needs a value — graff --help", .{});
flags.host_flag = try arena.dupe(u8, hv);
} else if (std.mem.eql(u8, arg, "--port")) {
- const pv = it.next() orelse std.process.fatal("--port needs a value — harness --help", .{});
+ const pv = it.next() orelse std.process.fatal("--port needs a value — graff --help", .{});
flags.port_flag = std.fmt.parseInt(u16, pv, 10) catch std.process.fatal("--port needs a number 1-65535, got '{s}'", .{pv});
} else if (std.mem.eql(u8, arg, "--token")) {
- const tv = it.next() orelse std.process.fatal("--token needs a value — harness --help", .{});
+ const tv = it.next() orelse std.process.fatal("--token needs a value — graff --help", .{});
flags.token_flag = try arena.dupe(u8, tv);
} else if (std.mem.eql(u8, arg, "--name")) {
- const nv = it.next() orelse std.process.fatal("--name needs a value — harness --help", .{});
+ const nv = it.next() orelse std.process.fatal("--name needs a value — graff --help", .{});
flags.name_flag = try arena.dupe(u8, nv);
} else {
- std.process.fatal("unknown flag '{s}' — harness --help lists them", .{arg});
+ std.process.fatal("unknown flag '{s}' — graff --help lists them", .{arg});
}
} else {
try flags.positionals.append(arena, try arena.dupe(u8, arg));
diff --git a/src/catalog_price.zig b/src/catalog_price.zig
new file mode 100644
index 00000000..38e70db4
--- /dev/null
+++ b/src/catalog_price.zig
@@ -0,0 +1,141 @@
+//! Prices a router publishes in its own /models list.
+//!
+//! The hosted gateway lists each model with OpenRouter's `pricing` object (USD
+//! per token as decimal strings) plus an optional `long_context` tier. Reading
+//! them from the catalog means a model the gateway adds, or a price it changes,
+//! is billed correctly without a graff release; pricing_gateway.zig stays the
+//! offline fallback. The catalog cache stores the same object, so a cached
+//! catalog keeps its prices.
+
+const std = @import("std");
+const Io = std.Io;
+const Value = std.json.Value;
+const pricing = @import("pricing.zig");
+
+/// USD per 1M tokens: the catalog's per-token rate × 1e6. Null when either the
+/// prompt or completion rate is missing or not a finite non-negative number.
+pub fn parse(obj: std.json.ObjectMap) ?pricing.CatalogPrice {
+ const p = obj.get("pricing") orelse return null;
+ if (p != .object) return null;
+ const in = perMillion(p.object.get("prompt")) orelse return null;
+ const out = perMillion(p.object.get("completion")) orelse return null;
+ var price: pricing.CatalogPrice = .{ .in = in, .out = out, .cache = perMillion(p.object.get("input_cache_read")) orelse in };
+ if (p.object.get("long_context")) |lc| if (lc == .object) {
+ const at = lc.object.get("threshold") orelse return price;
+ if (at != .integer or at.integer <= 0) return price;
+ const lin = perMillion(lc.object.get("prompt")) orelse return price;
+ const lout = perMillion(lc.object.get("completion")) orelse return price;
+ price.high_at = @intCast(at.integer);
+ price.high_in = lin;
+ price.high_out = lout;
+ price.high_cache = perMillion(lc.object.get("input_cache_read")) orelse lin;
+ };
+ return price;
+}
+
+fn perMillion(v: ?Value) ?f64 {
+ const rate: f64 = switch (v orelse return null) {
+ .string => |s| std.fmt.parseFloat(f64, s) catch return null,
+ .float => |f| f,
+ .integer => |i| @floatFromInt(i),
+ else => return null,
+ };
+ if (!std.math.isFinite(rate) or rate < 0) return null;
+ // Per-token decimals don't survive ×1e6 exactly (0.000000825 → 0.8250000000000001); keep 6 places.
+ return @round(rate * 1_000_000 * 1_000_000) / 1_000_000;
+}
+
+/// The rest of one cached model object after its name: context, reasoning flag,
+/// the price when known, and the closing brace.
+pub fn writeCacheTail(w: *Io.Writer, m: pricing.ModelInfo) !void {
+ try w.print(",\"context\":{d},\"supports_reasoning\":{}", .{ m.context, m.supports_reasoning });
+ if (m.price) |p| {
+ try w.print(",\"pricing\":{{\"prompt\":{d},\"completion\":{d},\"input_cache_read\":{d}", .{ p.in / 1_000_000, p.out / 1_000_000, p.cache / 1_000_000 });
+ if (p.high_at > 0) try w.print(",\"long_context\":{{\"threshold\":{d},\"prompt\":{d},\"completion\":{d},\"input_cache_read\":{d}}}", .{ p.high_at, p.high_in / 1_000_000, p.high_out / 1_000_000, p.high_cache / 1_000_000 });
+ try w.writeByte('}');
+ }
+ try w.writeByte('}');
+}
+
+/// A catalog price as the session meter's row.
+pub fn asModelPrice(name: []const u8, p: pricing.CatalogPrice) pricing.ModelPrice {
+ return .{ .name = name, .in = p.in, .out = p.out, .cache = p.cache, .cache_write_multiplier = 1, .high_at = p.high_at, .high_in = p.high_in, .high_out = p.high_out, .high_cache = p.high_cache };
+}
+
+fn approx(a: f64, b: f64) bool {
+ return @abs(a - b) < 1e-9;
+}
+
+test "parse reads OpenRouter-style per-token strings as USD per 1M" {
+ const a = std.testing.allocator;
+ const parsed = try std.json.parseFromSlice(Value, a,
+ \\{"id":"gemini-4-argon","pricing":{"prompt":"0.0000022","completion":"0.000011","input_cache_read":"0.00000011","free_credit":false}}
+ , .{});
+ defer parsed.deinit();
+ const p = parse(parsed.value.object).?;
+ try std.testing.expect(approx(p.in, 2.2) and approx(p.out, 11) and approx(p.cache, 0.11));
+ try std.testing.expectEqual(@as(u64, 0), p.high_at);
+ // Rounded to 6 places, so listings print 0.825, not 0.8250000000000001.
+ const flash = try std.json.parseFromSlice(Value, a,
+ \\{"pricing":{"prompt":"0.000000825","completion":"0.000004125"}}
+ , .{});
+ defer flash.deinit();
+ try std.testing.expectEqual(@as(f64, 0.825), parse(flash.value.object).?.in);
+}
+
+test "parse keeps a long-context tier, and rejects a row without both rates" {
+ const a = std.testing.allocator;
+ const tiered = try std.json.parseFromSlice(Value, a,
+ \\{"pricing":{"prompt":4e-6,"completion":"0.00002","long_context":{"threshold":272000,"prompt":"0.000008","completion":"0.00003"}}}
+ , .{});
+ defer tiered.deinit();
+ const p = parse(tiered.value.object).?;
+ try std.testing.expect(approx(p.in, 4) and approx(p.cache, 4));
+ try std.testing.expectEqual(@as(u64, 272000), p.high_at);
+ try std.testing.expect(approx(p.high_in, 8) and approx(p.high_out, 30) and approx(p.high_cache, 8));
+
+ for ([_][]const u8{
+ \\{"pricing":{"prompt":"0.000001"}}
+ ,
+ \\{"pricing":{"prompt":"-1","completion":"0.000001"}}
+ ,
+ \\{"pricing":{"prompt":"free","completion":"0.000001"}}
+ ,
+ \\{"context_length":1000}
+ }) |doc| {
+ const v = try std.json.parseFromSlice(Value, a, doc, .{});
+ defer v.deinit();
+ try std.testing.expect(parse(v.value.object) == null);
+ }
+}
+
+test "a cached price reads back the same" {
+ const a = std.testing.allocator;
+ var aw: Io.Writer.Allocating = .init(a);
+ defer aw.deinit();
+ const m: pricing.ModelInfo = .{ .provider = "codegraff", .name = "x", .context = 1000, .price = .{ .in = 2.2, .out = 11, .cache = 0.11, .high_at = 200001, .high_in = 4.4, .high_out = 22, .high_cache = 0.22 } };
+ try aw.writer.writeAll("{\"name\":\"x\"");
+ try writeCacheTail(&aw.writer, m);
+ const v = try std.json.parseFromSlice(Value, a, aw.written(), .{});
+ defer v.deinit();
+ const p = parse(v.value.object).?;
+ try std.testing.expect(approx(p.in, 2.2) and approx(p.out, 11) and approx(p.cache, 0.11));
+ try std.testing.expectEqual(@as(u64, 200001), p.high_at);
+ try std.testing.expect(approx(p.high_out, 22));
+}
+
+test "a gateway catalog price wins over the baked snapshot; other providers are untouched" {
+ const saved = pricing.active_model_table;
+ defer pricing.active_model_table = saved;
+ pricing.active_model_table = &.{
+ .{ .provider = "codegraff", .name = "gemini-3.8-flash", .context = 1_048_576, .price = .{ .in = 1, .out = 2, .cache = 0.1 } },
+ .{ .provider = "codegraff", .name = "gpt-5.5", .context = 1_050_000 },
+ };
+ const live = pricing.priceForProvider("codegraff", "gemini-3.8-flash").?;
+ try std.testing.expect(approx(live.in, 1) and approx(live.out, 2));
+ // No catalog price: the baked gateway row still answers.
+ const baked = pricing.priceForProvider("codegraff", "gpt-5.5").?;
+ try std.testing.expect(approx(baked.in, 4));
+ // A direct provider never reads the gateway's catalog.
+ try std.testing.expect(pricing.priceForProvider("google", "gemini-3.8-flash") == null or !approx(pricing.priceForProvider("google", "gemini-3.8-flash").?.in, 1));
+}
diff --git a/src/main.zig b/src/main.zig
index 31486ea2..ab47b7b2 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -565,7 +565,7 @@ test { // ── Unit tests (`zig build test`): pull in tests from imported modu
_ = .{ @import("session_catalog.zig"), @import("session_prompt.zig"), @import("session_discovery.zig"), @import("session_workspaces.zig"), @import("commands_resume.zig"), @import("peer_live.zig"), @import("presence_tests.zig"), @import("mcp_rpc_tests.zig") };
_ = .{ @import("prompt_astra.zig"), @import("prompt_guidance.zig"), @import("oauth_chatgpt.zig"), @import("chatgpt_reauth.zig") }; // per-model guidance (Astra, GPT-5.6); ChatGPT plan sign-in (ADR 0253)
_ = .{ @import("agent_empty_completion.zig"), @import("agent_model_loop.zig"), @import("publication_policy_tests.zig"), @import("jobs_completion_tests.zig") };
- _ = .{ @import("provider_routing_tests.zig"), @import("codegraff_messages_tests.zig"), @import("tool_result_empty_tests.zig"), @import("history_wire.zig"), @import("readline_paste_number_tests.zig"), @import("test_hooks.zig"), @import("peer_wake_loop.zig") };
+ _ = .{ @import("provider_routing_tests.zig"), @import("codegraff_messages_tests.zig"), @import("tool_result_empty_tests.zig"), @import("history_wire.zig"), @import("catalog_price.zig"), @import("readline_paste_number_tests.zig"), @import("test_hooks.zig"), @import("peer_wake_loop.zig") };
_ = .{ @import("list_dir_nearmiss.zig"), @import("tool_preview_tests.zig") };
_ = @import("repo_map.zig");
_ = @import("agent_overflow_tests.zig"); // #414: and, through it, agent_overflow.zig's table tests
diff --git a/src/models_cache.zig b/src/models_cache.zig
index f2f3f26d..8e3cceca 100644
--- a/src/models_cache.zig
+++ b/src/models_cache.zig
@@ -552,7 +552,7 @@ pub fn list(out: *Io.Writer) !void {
const ctx = pricing.contextFor(m.provider, m.name);
if (std.mem.eql(u8, m.provider, "codex")) {
try out.print(" {s:<34}{d:>10} ctx (subscription)\n", .{ m.name, ctx });
- } else if (pricing.priceFor(m.name)) |p| {
+ } else if (pricing.priceForProvider(m.provider, m.name)) |p| {
try out.print(" {s:<34}{d:>10} ctx ${d}/{d} per 1M\n", .{ m.name, ctx, p.in, p.out });
} else {
try out.print(" {s:<34}{d:>10} ctx (unpriced)\n", .{ m.name, ctx });
diff --git a/src/pricing.zig b/src/pricing.zig
index 9cb8ba08..52ef12f9 100644
--- a/src/pricing.zig
+++ b/src/pricing.zig
@@ -29,8 +29,13 @@ pub fn priceFor(model: []const u8) ?ModelPrice {
}
/// Gateway aliases have their own prices; vendor overlays must not replace them.
+/// The gateway's live catalog wins over the baked snapshot, so a model or price
+/// it adds bills correctly without a graff release.
pub fn priceForProvider(provider: []const u8, model: []const u8) ?ModelPrice {
- if (std.mem.eql(u8, provider, "codegraff")) return @import("pricing_gateway.zig").find(model);
+ if (std.mem.eql(u8, provider, "codegraff")) {
+ if (modelInfoFor(provider, model)) |info| if (info.price) |p| return @import("catalog_price.zig").asModelPrice(info.name, p);
+ return @import("pricing_gateway.zig").find(model);
+ }
return priceFor(model);
}
@@ -162,8 +167,13 @@ pub const ModelInfo = struct {
/// Codex catalog `default_verbosity` (ADR 0226): the `text.verbosity` the
/// route's own client sends for this model.
default_verbosity: ?[]const u8 = null,
+ /// What the route charges, when its own catalog publishes it (catalog_price.zig).
+ price: ?CatalogPrice = null,
};
+/// USD per 1M tokens, with the optional whole-request long-context tier.
+pub const CatalogPrice = struct { in: f64, out: f64, cache: f64, high_at: u64 = 0, high_in: f64 = 0, high_out: f64 = 0, high_cache: f64 = 0 };
+
pub const ModelProtocol = enum { provider_default, kimi, anthropic };
pub const ThinkingSupport = enum { unknown, no, both, only };
diff --git a/src/router_catalog.zig b/src/router_catalog.zig
index a8ca909c..4858eec0 100644
--- a/src/router_catalog.zig
+++ b/src/router_catalog.zig
@@ -195,9 +195,8 @@ pub fn parseModels(arena: Allocator, provider_id: []const u8, data: []const u8)
if (context == 0) context = positiveInt(item.object, "contextLength");
// Vercel AI Gateway (and some OpenAI-compat hosts) use context_window.
if (context == 0) context = positiveInt(item.object, "context_window");
- // Anthropic declares the input window as max_input_tokens. Its sibling
- // max_tokens is the OUTPUT cap (128k on Opus 5) — never read it as a
- // window, or a 1M-context model would compact at ~102k.
+ // Anthropic's input window is max_input_tokens; its sibling max_tokens is the
+ // OUTPUT cap (128k on Opus 5), never a window, or 1M would compact at ~102k.
if (context == 0) context = positiveInt(item.object, "max_input_tokens");
// Google's window. Its sibling outputTokenLimit is the OUTPUT cap, never this.
if (context == 0) context = positiveInt(item.object, "inputTokenLimit");
@@ -207,6 +206,7 @@ pub fn parseModels(arena: Allocator, provider_id: []const u8, data: []const u8)
.name = arena.dupe(u8, id) catch continue,
.context = context,
.supports_reasoning = listsReasoning(item.object) or boolField(item.object, "supports_reasoning"),
+ .price = @import("catalog_price.zig").parse(item.object),
}) catch continue;
}
const owned = rows.toOwnedSlice(arena) catch return null;
@@ -317,7 +317,7 @@ pub fn cacheDocument(io: Io, arena: Allocator, spec: provider.ProviderSpec, mode
aw.writer.writeAll("{\"name\":") catch return null;
var name_stringify: std.json.Stringify = .{ .writer = &aw.writer };
name_stringify.write(model.name) catch return null;
- aw.writer.print(",\"context\":{d},\"supports_reasoning\":{}}}", .{ model.context, model.supports_reasoning }) catch return null;
+ @import("catalog_price.zig").writeCacheTail(&aw.writer, model) catch return null;
}
aw.writer.writeAll("]}\n") catch return null;
return aw.writer.buffered();