Skip to content

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

Python

kimi: pin prompt_cache_key to session id; send thinking.keep:all like kimi-code

Rach Pradhan88b04bfe54ec
diff --git a/src/agent_request_body.zig b/src/agent_request_body.zig
index 9b9a1c8f..0c61e7dd 100644
--- a/src/agent_request_body.zig
+++ b/src/agent_request_body.zig
@@ -44,12 +44,8 @@ pub fn buildBody(self: *Agent, tools: ?[]const u8, force_tool: bool, stream: boo
if (is_kimi) {
if (pricing.kimiSupportsThinking(self.provider.model)) {
try s.objectField("thinking");
- // #323: k2.6 bills the reasoning we replay every turn and
- // then ignores it unless the request says keep:"all".
- if (pricing.kimiKeepsReasoning(self.provider.model))
- try s.print("{s}", .{"{\"type\":\"enabled\",\"keep\":\"all\"}"})
- else
- try s.print("{s}", .{"{\"type\":\"enabled\"}"});
+ // kimi-code default: keep:"all" on every thinking request (#323; k3 accepts it — verified live).
+ try s.print("{s}", .{"{\"type\":\"enabled\",\"keep\":\"all\"}"});
if (pricing.kimiThinkingEffort(self.provider.model, @tagName(self.reasoning))) |effort| {
try s.objectField("output_config");
try s.beginObject();
@@ -161,17 +157,20 @@ pub fn buildBody(self: *Agent, tools: ?[]const u8, force_tool: bool, stream: boo
try s.beginObject();
try s.objectField("type");
try s.write("enabled");
- // #323: same per-model opt-in as the Anthropic branch above.
- if (pricing.kimiKeepsReasoning(self.provider.model)) {
- try s.objectField("keep");
- try s.write("all");
- }
+ // kimi-code default: keep:"all" on every thinking request (#323).
+ try s.objectField("keep");
+ try s.write("all");
if (pricing.kimiThinkingEffort(self.provider.model, @tagName(self.reasoning))) |effort| {
try s.objectField("effort");
try s.write(effort);
}
try s.endObject();
}
+ // kimi-code pins each request to its session id for prompt-cache affinity (same partition trick as codex below).
+ if (is_kimi) {
+ try s.objectField("prompt_cache_key");
+ try s.write(http_headers.sessionId(self.io));
+ }
// Reasoning-effort hint for OpenAI-compatible providers that
// honor it (codegraff gateway, deepseek). Mirrors the
// Responses `reasoning.effort` set in the branch below.
@@ -289,12 +288,12 @@ test "Kimi request body follows live native or Anthropic protocol metadata" {
const native = try agent.buildBody(openai_tools, false, true, true);
defer std.testing.allocator.free(native);
try std.testing.expect(std.mem.indexOf(u8, native, "\"max_completion_tokens\":16000") != null);
- try std.testing.expect(std.mem.indexOf(u8, native, "\"thinking\":{\"type\":\"enabled\",\"effort\":\"max\"}") != null);
+ try std.testing.expect(std.mem.indexOf(u8, native, "\"thinking\":{\"type\":\"enabled\",\"keep\":\"all\",\"effort\":\"max\"}") != null);
try std.testing.expect(std.mem.indexOf(u8, native, "\"role\":\"system\",\"content\":\"system\"") != null);
try std.testing.expect(std.mem.indexOf(u8, native, "\"reasoning_effort\"") == null);
try std.testing.expect(std.mem.indexOf(u8, native, "\"target\":{\"anyOf\":[{\"const\":\"one\",\"type\":\"string\"},{\"const\":\"two\",\"type\":\"string\"}]}") != null);
try std.testing.expect(std.mem.indexOf(u8, native, "\"oneOf\":[{\"required\":[\"target\"],\"type\":\"object\"},{\"required\":[\"other\"],\"type\":\"object\"}]") != null);
- try std.testing.expect(std.mem.indexOf(u8, native, "\"keep\"") == null); // #323: k3 has no thinking.keep
+ try std.testing.expect(std.mem.indexOf(u8, native, "\"prompt_cache_key\":\"") != null); // kimi-code: session-id cache affinity on every request
agent.provider.kind = .anthropic;
agent.provider.auth = .x_api_key;
@@ -302,13 +301,13 @@ test "Kimi request body follows live native or Anthropic protocol metadata" {
const anthropic_tools = "[{\"name\":\"ping\",\"description\":\"\",\"input_schema\":{\"type\":\"object\"}}]";
const anthropic = try agent.buildBody(anthropic_tools, false, true, true);
defer std.testing.allocator.free(anthropic);
- try std.testing.expect(std.mem.indexOf(u8, anthropic, "\"thinking\":{\"type\":\"enabled\"}") != null);
+ try std.testing.expect(std.mem.indexOf(u8, anthropic, "\"thinking\":{\"type\":\"enabled\",\"keep\":\"all\"}") != null);
try std.testing.expect(std.mem.indexOf(u8, anthropic, "\"output_config\":{\"effort\":\"max\"}") != null);
try std.testing.expect(std.mem.indexOf(u8, anthropic, "\"type\":\"adaptive\"") == null);
try std.testing.expect(std.mem.indexOf(u8, anthropic, "\"system\":[{\"type\":\"text\",\"text\":\"system\",\"cache_control\":{\"type\":\"ephemeral\"}}]") != null);
try std.testing.expect(std.mem.indexOf(u8, anthropic, "\"content\":[{\"type\":\"text\",\"text\":\"hello\",\"cache_control\":{\"type\":\"ephemeral\"}}]") != null);
try std.testing.expect(std.mem.indexOf(u8, anthropic, "\"input_schema\":{\"type\":\"object\"},\"cache_control\":{\"type\":\"ephemeral\"}") != null);
- try std.testing.expect(std.mem.indexOf(u8, anthropic, "\"keep\"") == null); // #323: k3 has no thinking.keep
+ try std.testing.expect(std.mem.indexOf(u8, anthropic, "\"prompt_cache_key\"") == null); // native-transport field only
}
// ── Retained-reasoning wire-format regressions ──────────────────────────────
diff --git a/src/pricing.zig b/src/pricing.zig
index 1dc6d9d5..b5005871 100644
--- a/src/pricing.zig
+++ b/src/pricing.zig
@@ -321,14 +321,6 @@ pub fn kimiThinkingEffort(model: []const u8, requested: []const u8) ?[]const u8
return info.support_efforts[info.support_efforts.len / 2];
}
-/// Kimi k2.6 defaults `thinking.keep` to null: the server bills the reasoning
-/// we replay each turn and then ignores it (#323). k2.7-code always preserves
-/// and k3 rejects the parameter; the live catalog says nothing about keep, so
-/// this is a seeded id check (a point release is spelled `k2.6` or `k2p6`).
-pub fn kimiKeepsReasoning(model: []const u8) bool {
- return kimiSupportsThinking(model) and (std.mem.indexOf(u8, model, "k2.6") != null or std.mem.indexOf(u8, model, "k2p6") != null);
-}
-
fn pureKimiGeneration(name: []const u8) ?u32 {
if (name.len < 2 or name[0] != 'k') return null;
for (name[1..]) |c| if (!std.ascii.isDigit(c)) return null;