graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Pin Vercel AI Gateway sessions and price them from its catalog
Rach Pradhan7a5b80c8573f
diff --git a/src/catalog_price.zig b/src/catalog_price.zigindex c9c3f0ed..4fa9fbaa 100644--- a/src/catalog_price.zig+++ b/src/catalog_price.zig@@ -17,8 +17,9 @@ const pricing = @import("pricing.zig");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;+ // Vercel AI Gateway names the same per-token rates input/output.+ const in = perMillion(p.object.get("prompt") orelse p.object.get("input")) orelse return null;+ const out = perMillion(p.object.get("completion") orelse p.object.get("output")) 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;@@ -83,6 +84,16 @@ test "parse reads OpenRouter-style per-token strings as USD per 1M" {try std.testing.expectEqual(@as(f64, 0.825), parse(flash.value.object).?.in);}+test "parse reads Vercel AI Gateway input/output rates" {+ const a = std.testing.allocator;+ const parsed = try std.json.parseFromSlice(Value, a,+ \\{"id":"moonshotai/kimi-k3.1-pro","pricing":{"input":"0.000002","output":"0.00001","input_cache_read":"0.0000002","input_cache_write":"0.000002"}}+ , .{});+ defer parsed.deinit();+ const p = parse(parsed.value.object).?;+ try std.testing.expect(approx(p.in, 2) and approx(p.out, 10) and approx(p.cache, 0.2));+}+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,@@ -130,7 +141,10 @@ test "a gateway catalog price wins over the baked snapshot; other providers arepricing.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-6-sol", .context = 1_050_000 },+ .{ .provider = "vercel", .name = "moonshotai/kimi-k3.1-pro", .context = 1_048_576, .price = .{ .in = 2, .out = 10, .cache = 0.2 } },};+ const vercel = pricing.priceForProvider("vercel", "moonshotai/kimi-k3.1-pro").?;+ try std.testing.expect(approx(vercel.in, 2) and approx(vercel.cache, 0.2));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.diff --git a/src/http_headers.zig b/src/http_headers.zigindex 02083f69..e8252dac 100644--- a/src/http_headers.zig+++ b/src/http_headers.zig@@ -259,6 +259,14 @@ pub fn providerHeadersWithConv(io: Io, provider: Provider, bearer: []const u8, bbuf[count] = .{ .name = "x-title", .value = "graff" };count += 1;}+ // Vercel pins a session to the provider credential that holds its cache+ // and forwards this header upstream. `prompt_cache_key` alone is treated+ // as a shared label that pins the provider but not the credential+ // (vercel.com/docs/ai-gateway/models-and-providers/session-affinity).+ if (std.mem.eql(u8, provider.id, "vercel")) {+ buf[count] = .{ .name = "x-session-affinity", .value = conv_id orelse projectRootId(io) };+ count += 1;+ }// OpenRouter marketplace categories. Max 2 per request; only recognized// slugs count — unrecognized values are dropped server-side, silently.if (std.mem.eql(u8, provider.id, "openrouter")) {@@ -343,56 +351,6 @@ test "Z.AI chat sends Accept-Language and no grok conv headers" {try std.testing.expect(saw_lang);}-test "Vercel chat sends app attribution and no grok conv headers" {- const io = std.testing.io;- var buf: [12]std.http.Header = undefined;- const p: Provider = .{ .id = "vercel", .kind = .openai, .auth = .bearer, .url = "https://ai-gateway.vercel.sh/coding-agent/v1/chat/completions", .api_key = "k", .model = "alibaba/qwen3.8-27b", .context = 1_000_000 };- const headers = providerHeaders(io, p, "Bearer k", &buf);- var saw_ref = false;- var saw_title = false;- for (headers) |h| {- try std.testing.expect(!std.mem.eql(u8, h.name, "x-grok-conv-id"));- if (std.mem.eql(u8, h.name, "http-referer")) {- saw_ref = true;- try std.testing.expectEqualStrings("https://codegraff.com", h.value);- }- if (std.mem.eql(u8, h.name, "x-title")) {- saw_title = true;- try std.testing.expectEqualStrings("graff", h.value);- }- }- try std.testing.expect(saw_ref);- try std.testing.expect(saw_title);-}--test "OpenRouter chat sends app attribution and no grok conv headers" {- const io = std.testing.io;- var buf: [12]std.http.Header = undefined;- const p: Provider = .{ .id = "openrouter", .kind = .openai, .auth = .bearer, .url = "https://openrouter.ai/api/v1/chat/completions", .api_key = "k", .model = "anthropic/claude-sonnet-4.6", .context = 1_000_000 };- const headers = providerHeaders(io, p, "Bearer k", &buf);- var saw_ref = false;- var saw_title = false;- var saw_categories = false;- for (headers) |h| {- try std.testing.expect(!std.mem.eql(u8, h.name, "x-grok-conv-id"));- if (std.mem.eql(u8, h.name, "http-referer")) {- saw_ref = true;- try std.testing.expectEqualStrings("https://codegraff.com", h.value);- }- if (std.mem.eql(u8, h.name, "x-title")) {- saw_title = true;- try std.testing.expectEqualStrings("graff", h.value);- }- if (std.mem.eql(u8, h.name, "x-openrouter-categories")) {- saw_categories = true;- try std.testing.expectEqualStrings("cli-agent,programming-app", h.value);- }- }- try std.testing.expect(saw_ref);- try std.testing.expect(saw_title);- try std.testing.expect(saw_categories);-}-test "projectRootId is minted from the affinity seed, not a second hash" {// Locks the wire: a future change that hashes cwd again inside// projectRootId would still pass the seed-string tests indiff --git a/src/pricing.zig b/src/pricing.zigindex c46cd7dc..41ae97ab 100644--- a/src/pricing.zig+++ b/src/pricing.zig@@ -32,6 +32,11 @@ pub fn priceFor(model: []const u8) ?ModelPrice {/// 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 {+ // Vercel AI Gateway publishes its own per-route price in /models too.+ if (std.mem.eql(u8, provider, "vercel")) {+ if (modelInfoFor(provider, model)) |info| if (info.price) |p| return @import("catalog_price.zig").asModelPrice(info.name, p);+ return priceFor(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);diff --git a/src/router_headers_tests.zig b/src/router_headers_tests.zignew file mode 100644index 00000000..c53a8d32--- /dev/null+++ b/src/router_headers_tests.zig@@ -0,0 +1,82 @@+//! Router-gateway headers (Vercel AI Gateway, OpenRouter), moved off+//! http_headers.zig for the 600-line cap: app attribution and Vercel's+//! session affinity.++const std = @import("std");+const http_headers = @import("http_headers.zig");+const Provider = @import("provider.zig").Provider;++test "Vercel chat sends app attribution and no grok conv headers" {+ const io = std.testing.io;+ var buf: [12]std.http.Header = undefined;+ const p: Provider = .{ .id = "vercel", .kind = .openai, .auth = .bearer, .url = "https://ai-gateway.vercel.sh/coding-agent/v1/chat/completions", .api_key = "k", .model = "alibaba/qwen3.8-27b", .context = 1_000_000 };+ const headers = http_headers.providerHeaders(io, p, "Bearer k", &buf);+ var saw_ref = false;+ var saw_title = false;+ for (headers) |h| {+ try std.testing.expect(!std.mem.eql(u8, h.name, "x-grok-conv-id"));+ if (std.mem.eql(u8, h.name, "http-referer")) {+ saw_ref = true;+ try std.testing.expectEqualStrings("https://codegraff.com", h.value);+ }+ if (std.mem.eql(u8, h.name, "x-title")) {+ saw_title = true;+ try std.testing.expectEqualStrings("graff", h.value);+ }+ }+ try std.testing.expect(saw_ref);+ try std.testing.expect(saw_title);+}++test "Vercel chat pins session affinity to the request cache key" {+ const io = std.testing.io;+ var buf: [12]std.http.Header = undefined;+ const p: Provider = .{ .id = "vercel", .kind = .openai, .auth = .bearer, .url = "https://ai-gateway.vercel.sh/coding-agent/v1/chat/completions", .api_key = "k", .model = "moonshotai/kimi-k3.1-pro", .context = 1_000_000 };+ var kbuf: [96]u8 = undefined;+ var dummy: u8 = 0;+ const key = http_headers.requestCacheKey(io, "main", &dummy, p, &kbuf);+ var affinity: ?[]const u8 = null;+ for (http_headers.providerHeadersWithConv(io, p, "Bearer k", &buf, key)) |h| {+ if (std.mem.eql(u8, h.name, "x-session-affinity")) affinity = h.value;+ }+ try std.testing.expectEqualStrings(key, affinity.?);+ // One-off posts (title, compact) fall back to the durable project id.+ var buf2: [12]std.http.Header = undefined;+ affinity = null;+ for (http_headers.providerHeaders(io, p, "Bearer k", &buf2)) |h| {+ if (std.mem.eql(u8, h.name, "x-session-affinity")) affinity = h.value;+ }+ try std.testing.expectEqualStrings(http_headers.projectRootId(io), affinity.?);+ // Other providers do not get the header.+ var buf3: [12]std.http.Header = undefined;+ const z: Provider = .{ .id = "zai", .kind = .openai, .auth = .bearer, .url = "https://api.z.ai/api/paas/v4/chat/completions", .api_key = "k", .model = "glm-5.3", .context = 1_000_000 };+ for (http_headers.providerHeadersWithConv(io, z, "Bearer k", &buf3, key)) |h| try std.testing.expect(!std.mem.eql(u8, h.name, "x-session-affinity"));+}++test "OpenRouter chat sends app attribution and no grok conv headers" {+ const io = std.testing.io;+ var buf: [12]std.http.Header = undefined;+ const p: Provider = .{ .id = "openrouter", .kind = .openai, .auth = .bearer, .url = "https://openrouter.ai/api/v1/chat/completions", .api_key = "k", .model = "anthropic/claude-sonnet-4.6", .context = 1_000_000 };+ const headers = http_headers.providerHeaders(io, p, "Bearer k", &buf);+ var saw_ref = false;+ var saw_title = false;+ var saw_categories = false;+ for (headers) |h| {+ try std.testing.expect(!std.mem.eql(u8, h.name, "x-grok-conv-id"));+ if (std.mem.eql(u8, h.name, "http-referer")) {+ saw_ref = true;+ try std.testing.expectEqualStrings("https://codegraff.com", h.value);+ }+ if (std.mem.eql(u8, h.name, "x-title")) {+ saw_title = true;+ try std.testing.expectEqualStrings("graff", h.value);+ }+ if (std.mem.eql(u8, h.name, "x-openrouter-categories")) {+ saw_categories = true;+ try std.testing.expectEqualStrings("cli-agent,programming-app", h.value);+ }+ }+ try std.testing.expect(saw_ref);+ try std.testing.expect(saw_title);+ try std.testing.expect(saw_categories);+}diff --git a/src/test_hooks.zig b/src/test_hooks.zigindex ce1e6b44..c89c1ba9 100644--- a/src/test_hooks.zig+++ b/src/test_hooks.zig@@ -340,6 +340,7 @@ test {_ = @import("mcp_connect_notice.zig"); // ADR 0230_ = @import("oauth_callback.zig");_ = @import("cache_key_tests.zig"); // ADR 0223: moved off http_headers.zig (600-line cap)+ _ = @import("router_headers_tests.zig"); // moved off http_headers.zig (600-line cap)_ = @import("mcp_shapes_tests.zig"); // ADR 0225: moved off mcp_shapes.zig (600-line cap)_ = json_controls;_ = effort_route;