Skip to content

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

Python

feat(xai): catalog grok-4.6 as the default — 500k window, dual-band prices

Rach Pradhan7b295697f70b
diff --git a/src/grok_spec_conformance.zig b/src/grok_spec_conformance.zig
index cebf9883..b89a9ab6 100644
--- a/src/grok_spec_conformance.zig
+++ b/src/grok_spec_conformance.zig
@@ -23,7 +23,7 @@ fn xaiAgent(arena: std.mem.Allocator, label: []const u8, kind: @import("provider
.arena = arena,
.io = std.testing.io,
.client = undefined,
- .provider = .{ .id = "xai", .kind = kind, .auth = .bearer, .url = "https://api.x.ai/v1/responses", .api_key = "k", .model = "grok-4.6", .context = 256_000 },
+ .provider = .{ .id = "xai", .kind = kind, .auth = .bearer, .url = "https://api.x.ai/v1/responses", .api_key = "k", .model = "grok-4.6", .context = 500_000 },
.messages = messages,
.sub = !std.mem.eql(u8, label, "main"),
.label = label,
@@ -54,7 +54,7 @@ test "grok spec: Chat Completions x-grok-conv-id is stable for root and isolated
const child_id = http_headers.promptCacheKey(io, "sub", @ptrCast(&child_slot), &cbuf);
try std.testing.expect(!std.mem.eql(u8, root_id, child_id));
- const xai: @import("provider.zig").Provider = .{ .id = "xai", .kind = .openai, .auth = .bearer, .url = "", .api_key = "k", .model = "grok-4.6", .context = 256_000 };
+ const xai: @import("provider.zig").Provider = .{ .id = "xai", .kind = .openai, .auth = .bearer, .url = "", .api_key = "k", .model = "grok-4.6", .context = 500_000 };
var h1: [12]std.http.Header = undefined;
var h2: [12]std.http.Header = undefined;
var h3: [12]std.http.Header = undefined;
diff --git a/src/http_headers.zig b/src/http_headers.zig
index 487cd6bc..22e9014a 100644
--- a/src/http_headers.zig
+++ b/src/http_headers.zig
@@ -241,7 +241,7 @@ test "projectCacheKey is a durable per-project v5 UUID, stable across calls" {
test "x-grok-conv-id with no explicit conv still uses the project root id" {
const io = std.testing.io;
- const xai: Provider = .{ .id = "xai", .kind = .openai, .auth = .bearer, .url = "", .api_key = "k", .model = "grok-4.6", .context = 256_000 };
+ const xai: Provider = .{ .id = "xai", .kind = .openai, .auth = .bearer, .url = "", .api_key = "k", .model = "grok-4.6", .context = 500_000 };
var buf: [12]std.http.Header = undefined;
const headers = providerHeadersWithConv(io, xai, "Bearer k", &buf, null);
try std.testing.expectEqualStrings(projectRootId(io), headerValue(headers, "x-grok-conv-id") orelse return error.MissingGrokConvId);
@@ -276,7 +276,7 @@ test "xAI Chat Completions headers carry a stable per-conversation x-grok-conv-i
const child_id = promptCacheKey(io, "sub", child_agent, &child_buf);
try std.testing.expect(!std.mem.eql(u8, root_id, child_id));
- const xai: Provider = .{ .id = "xai", .kind = .openai, .auth = .bearer, .url = "", .api_key = "k", .model = "grok-4.6", .context = 256_000 };
+ const xai: Provider = .{ .id = "xai", .kind = .openai, .auth = .bearer, .url = "", .api_key = "k", .model = "grok-4.6", .context = 500_000 };
var buf: [12]std.http.Header = undefined;
const first = providerHeadersWithConv(io, xai, "Bearer k", &buf, root_id);
try std.testing.expectEqualStrings(root_id, headerValue(first, "x-grok-conv-id") orelse return error.MissingGrokConvId);
diff --git a/src/oauth.zig b/src/oauth.zig
index 894e7479..f9586ccd 100644
--- a/src/oauth.zig
+++ b/src/oauth.zig
@@ -445,7 +445,7 @@ pub fn xaiLogin(io: Io, gpa: Allocator, arena: Allocator, home: []const u8) !voi
const refresh = strFieldObj(resp, "refresh_token") orelse "";
const expires_in: i64 = if (resp.get("expires_in")) |ei| (if (ei == .integer) ei.integer else 3600) else 3600;
try writeXaiAuth(io, arena, home, a.string, refresh, @divTrunc(unixMs(io), 1000) + expires_in);
- try out.print("✓ logged into Grok (xAI) — wrote {s}. /model xai grok-4.3\n", .{xaiAuthPath(arena, home)});
+ try out.print("✓ logged into Grok (xAI) — wrote {s}. /model xai grok-4.6\n", .{xaiAuthPath(arena, home)});
try out.writeAll(" note: xAI gates its OpenAI-compatible API per account — if a Grok turn 403s, your account isn't allowlisted for api.x.ai yet.\n");
try out.flush();
return;
diff --git a/src/pricing.zig b/src/pricing.zig
index 74f5e446..355170e7 100644
--- a/src/pricing.zig
+++ b/src/pricing.zig
@@ -13,7 +13,7 @@ const util = @import("util.zig");
/// (codex, claude) bill via subscription, not per token — recordCost treats
/// them as $0 ("sub") regardless of this table. Models absent here have no
/// known price and contribute 0 to the running cost (shown as ~).
-pub const ModelPrice = struct { name: []const u8, in: f64, out: f64, cache: f64 };
+pub const ModelPrice = struct { name: []const u8, in: f64, out: f64, cache: f64, high_at: u64 = 0, high_in: f64 = 0, high_out: f64 = 0, high_cache: f64 = 0 };
pub const price_table = [_]ModelPrice{
.{ .name = "deepseek-v4-pro", .in = 1.1, .out = 2.2, .cache = 0.11 },
.{ .name = "deepseek-v4-flash", .in = 0.14, .out = 0.28, .cache = 0.028 },
@@ -43,6 +43,7 @@ pub const price_table = [_]ModelPrice{
.{ .name = "kimi-k2.6", .in = 0.95, .out = 4, .cache = 0.1 },
.{ .name = "kimi-k2-thinking", .in = 0.6, .out = 2.5, .cache = 0.06 },
.{ .name = "kimi-k2.5", .in = 0.6, .out = 3, .cache = 0.06 },
+ .{ .name = "grok-4.6", .in = 2, .out = 6, .cache = 0.5, .high_at = 200_000, .high_in = 4, .high_out = 12, .high_cache = 1 },
.{ .name = "grok-4.3", .in = 1.25, .out = 2.5, .cache = 0.3 },
.{ .name = "grok-build", .in = 1, .out = 2, .cache = 0.1 },
.{ .name = "glm-5.2", .in = 1, .out = 3.2, .cache = 0.1 },
@@ -70,11 +71,16 @@ pub fn priceFor(model: []const u8) ?ModelPrice {
pub const Billing = enum { sub, priced, unpriced };
/// USD for one request at price `p` (negative token counts clamp to 0).
+/// When `high_at` is set and prompt tokens (uncached+cached) meet it, the
+/// whole request uses the high band — grok-4.6's published ≥200k rates.
pub fn usdFor(p: ModelPrice, uncached_in: i64, cache_in: i64, out: i64) f64 {
- const fi: f64 = @floatFromInt(@max(uncached_in, 0));
- const fc: f64 = @floatFromInt(@max(cache_in, 0));
+ const ui = @max(uncached_in, 0);
+ const ci = @max(cache_in, 0);
+ const high = p.high_at > 0 and @as(u64, @intCast(ui)) +| @as(u64, @intCast(ci)) >= p.high_at;
+ const fi: f64 = @floatFromInt(ui);
+ const fc: f64 = @floatFromInt(ci);
const fo: f64 = @floatFromInt(@max(out, 0));
- return (fi * p.in + fc * p.cache + fo * p.out) / 1_000_000.0;
+ return (fi * (if (high) p.high_in else p.in) + fc * (if (high) p.high_cache else p.cache) + fo * (if (high) p.high_out else p.out)) / 1_000_000.0;
}
/// Session-wide usage/cost tally. Every API response lands here — the root
@@ -275,6 +281,7 @@ pub const model_table = [_]ModelInfo{
.{ .provider = "kimi", .name = "kimi-for-coding", .context = 262_144, .protocol = .kimi, .supports_reasoning = true },
.{ .provider = "kimi", .name = "kimi-for-coding-highspeed", .context = 262_144, .protocol = .kimi, .supports_reasoning = true },
.{ .provider = "moonshot", .name = "kimi-latest", .context = 131_072 },
+ .{ .provider = "xai", .name = "grok-4.6", .context = 500_000, .supports_reasoning = true },
.{ .provider = "xai", .name = "grok-4.3", .context = 1_000_000 },
.{ .provider = "xai", .name = "grok-build", .context = 256_000 },
.{ .provider = "zai", .name = "glm-5.2", .context = 204_800 },
@@ -588,13 +595,4 @@ test "refresh overlay augments price/context lookups (codex cap still applies)"
// Seat classification moved to billing.zig with #471 — it needs the provider
// spec and the credential source, not the price sheet. Its tests live there.
-test "usdFor: per-million math and negative clamping" {
- const p = priceFor("gpt-5.5").?; // $5 in / $30 out / $0.5 cache per 1M
- try std.testing.expectApproxEqAbs(@as(f64, 5.0), usdFor(p, 1_000_000, 0, 0), 1e-9);
- try std.testing.expectApproxEqAbs(@as(f64, 0.5), usdFor(p, 0, 1_000_000, 0), 1e-9);
- try std.testing.expectApproxEqAbs(@as(f64, 3.0), usdFor(p, 0, 0, 100_000), 1e-9);
- try std.testing.expectApproxEqAbs(@as(f64, 0.0), usdFor(p, -42, -1, 0), 1e-9); // clamped
-}
-
-// CostTally/modelInTable/priceFor/resolveModelName tests live in
-// pricing_tests.zig (this file sits against the 600-line cap).
+// usdFor + grok-4.6 window/band tests live in pricing_tests.zig (600-line cap).
diff --git a/src/pricing_tests.zig b/src/pricing_tests.zig
index b05ab1da..4b2c34b8 100644
--- a/src/pricing_tests.zig
+++ b/src/pricing_tests.zig
@@ -49,3 +49,51 @@ test "resolveModelName (#377): family-prefixed spelling resolves to the provider
try std.testing.expect(!pricing.familyAliasEquals("kimi", "k3", "kimi-k30"));
try std.testing.expect(!pricing.familyAliasEquals("codex", "k3", "kimi-k3"));
}
+
+test "usdFor: per-million math and negative clamping" {
+ const p = pricing.priceFor("gpt-5.5").?; // $5 in / $30 out / $0.5 cache per 1M
+ try std.testing.expectApproxEqAbs(@as(f64, 5.0), pricing.usdFor(p, 1_000_000, 0, 0), 1e-9);
+ try std.testing.expectApproxEqAbs(@as(f64, 0.5), pricing.usdFor(p, 0, 1_000_000, 0), 1e-9);
+ try std.testing.expectApproxEqAbs(@as(f64, 3.0), pricing.usdFor(p, 0, 0, 100_000), 1e-9);
+ try std.testing.expectApproxEqAbs(@as(f64, 0.0), pricing.usdFor(p, -42, -1, 0), 1e-9); // clamped
+}
+
+test "grok-4.6 window is 500k and compact-at is 80%" {
+ const window = pricing.contextFor("xai", "grok-4.6");
+ try std.testing.expectEqual(@as(u64, 500_000), window);
+ try std.testing.expectEqual(@as(u64, 1_000_000), pricing.contextFor("xai", "grok-4.3")); // other xAI rows unchanged
+ try std.testing.expectEqual(@as(u64, pricing.default_context), pricing.contextFor("nope", "unknown-xyz"));
+ const p = provider_mod.Provider{
+ .id = "xai",
+ .kind = .responses,
+ .auth = .bearer,
+ .url = "",
+ .api_key = "k",
+ .model = "grok-4.6",
+ .context = window,
+ };
+ try std.testing.expectEqual(@as(u64, 400_000), p.compactAt()); // 80% of 500k
+}
+
+test "grok-4.6 prices: low band under 200k, high band for the whole request at ≥200k" {
+ const p = pricing.priceFor("grok-4.6") orelse return error.MissingGrok46Price;
+ try std.testing.expectEqual(@as(u64, 200_000), p.high_at);
+ // 10k uncached + 2k cached + 500 out @ $2 / $0.50 / $6
+ try std.testing.expectApproxEqAbs(@as(f64, 0.024), pricing.usdFor(p, 10_000, 2_000, 500), 1e-12);
+ // 199_999 stays on the low band
+ try std.testing.expectApproxEqAbs(@as(f64, 0.399998), pricing.usdFor(p, 199_999, 0, 0), 1e-12);
+ // 200k uncached + 1k out uses $4 / $12 for every token
+ try std.testing.expectApproxEqAbs(@as(f64, 0.812), pricing.usdFor(p, 200_000, 0, 1_000), 1e-12);
+ // 190k uncached + 20k cached = 210k prompt → high band on all tokens @ $4 / $1 (cached) / $12 out unused here
+ try std.testing.expectApproxEqAbs(@as(f64, 0.78), pricing.usdFor(p, 190_000, 20_000, 0), 1e-12);
+ // grok-4.3 stays flat even above 200k
+ const old = pricing.priceFor("grok-4.3").?;
+ try std.testing.expectEqual(@as(u64, 0), old.high_at);
+ try std.testing.expectApproxEqAbs(@as(f64, 0.25), pricing.usdFor(old, 200_000, 0, 0), 1e-12);
+}
+
+test "xAI default model is grok-4.6 and is catalogued" {
+ const spec = provider_mod.specFor("xai") orelse return error.MissingXaiSpec;
+ try std.testing.expectEqualStrings("grok-4.6", spec.default_model);
+ try std.testing.expect(pricing.providerModelInTable("xai", "grok-4.6"));
+}
diff --git a/src/provider.zig b/src/provider.zig
index 3fa2b2aa..2c55eb80 100644
--- a/src/provider.zig
+++ b/src/provider.zig
@@ -102,7 +102,7 @@ pub const provider_specs = [_]ProviderSpec{
.{ .id = "moonshot", .display_name = "Moonshot", .kind = .openai, .auth = .bearer, .url = "https://api.moonshot.ai/v1/chat/completions", .env_key = "MOONSHOT_API_KEY", .default_model = "kimi-latest" },
// `graff login xai` is a real device-code OAuth flow (oauth.zig), so xAI's
// login is a SuperGrok plan while XAI_API_KEY is metered api.x.ai access.
- .{ .id = "xai", .display_name = "xAI", .kind = .openai, .auth = .bearer, .url = "https://api.x.ai/v1/chat/completions", .env_key = "XAI_API_KEY", .default_model = "grok-4.3", .login = .xai_device, .sub_login = true, .catalog = .openai, .models_url = "https://api.x.ai/v1/models" },
+ .{ .id = "xai", .display_name = "xAI", .kind = .openai, .auth = .bearer, .url = "https://api.x.ai/v1/chat/completions", .env_key = "XAI_API_KEY", .default_model = "grok-4.6", .login = .xai_device, .sub_login = true, .catalog = .openai, .models_url = "https://api.x.ai/v1/models" },
.{ .id = "zai", .display_name = "Z.AI", .kind = .openai, .auth = .bearer, .url = "https://api.z.ai/api/paas/v4/chat/completions", .env_key = "ZAI_API_KEY", .default_model = "glm-5.2" },
.{ .id = "fugu", .display_name = "fugu", .kind = .openai, .auth = .bearer, .url = "https://api.sakana.ai/v1/chat/completions", .env_key = "FUGU_API_KEY", .default_model = "fugu-ultra" },
// Fireworks serves its serverless catalog live (AIP gateway shape: