Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

kimi: round-trip reasoning content as anthropic thinking blocks

Raine Virta977c97ac84dc
diff --git a/src/anthropic/schema.ts b/src/anthropic/schema.ts
index 9743c13..bda93bf 100644
--- a/src/anthropic/schema.ts
+++ b/src/anthropic/schema.ts
@@ -25,11 +25,18 @@ export interface AnthropicToolResultBlock {
is_error?: boolean
}
+export interface AnthropicThinkingBlock {
+ type: "thinking"
+ thinking: string
+ signature?: string
+}
+
export type AnthropicContentBlock =
| AnthropicTextBlock
| AnthropicImageBlock
| AnthropicToolUseBlock
| AnthropicToolResultBlock
+ | AnthropicThinkingBlock
export interface AnthropicMessage {
role: "user" | "assistant"
diff --git a/src/providers/kimi/count-tokens.ts b/src/providers/kimi/count-tokens.ts
index 8819f91..5698740 100644
--- a/src/providers/kimi/count-tokens.ts
+++ b/src/providers/kimi/count-tokens.ts
@@ -24,6 +24,8 @@ export function countTokens(req: AnthropicRequest): number {
total += encode(JSON.stringify(block.input ?? {})).length
} else if (block.type === "tool_result") {
total += encode(toolResultToString(block.content)).length
+ } else if (block.type === "thinking") {
+ total += encode(block.thinking).length
}
}
}
@@ -53,6 +55,7 @@ export function countTranslatedTokens(req: KimiChatRequest): number {
}
} else if (m.role === "assistant") {
if (typeof m.content === "string") total += encode(m.content).length
+ if (m.reasoning_content) total += encode(m.reasoning_content).length
for (const tc of m.tool_calls ?? []) {
total += encode(tc.function.name).length
total += encode(tc.function.arguments).length
diff --git a/src/providers/kimi/index.ts b/src/providers/kimi/index.ts
index d334f51..51c9497 100644
--- a/src/providers/kimi/index.ts
+++ b/src/providers/kimi/index.ts
@@ -28,7 +28,7 @@ function jsonError(status: number, type: string, message: string): Response {
async function handleCountTokens(body: AnthropicRequest, ctx: RequestContext): Promise<Response> {
const resolvedModel = resolveModel(body.model)
const translated = translateRequest({ ...body, model: resolvedModel })
- const tokens = countTranslatedTokens(translated)
+ const tokens = countTranslatedTokens(translated.body)
log.debug("count_tokens", { reqId: ctx.reqId, tokens })
return new Response(JSON.stringify({ input_tokens: tokens }), {
headers: { "content-type": "application/json" },
@@ -71,25 +71,34 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Prom
{ sessionId: ctx.sessionId },
)
const localInputTokens = VERBOSE ? countTokens(body) : undefined
- const translatedInputTokens = VERBOSE ? countTranslatedTokens(translated) : undefined
+ const translatedInputTokens = VERBOSE ? countTranslatedTokens(translated.body) : undefined
+ if (translated.thinking.degraded) {
+ log.warn("thinking degraded", {
+ reqId: ctx.reqId,
+ sessionId: ctx.sessionId,
+ reason: "prior assistant tool_call turn has no reasoning_content",
+ })
+ }
log.debug("translated request", {
reqId: ctx.reqId,
requestedModel: body.model,
resolvedModel,
- messageCount: translated.messages.length,
- toolCount: translated.tools?.length ?? 0,
+ messageCount: translated.body.messages.length,
+ toolCount: translated.body.tools?.length ?? 0,
localInputTokens,
translatedInputTokens,
- promptCacheKey: translated.prompt_cache_key,
- reasoningEffort: translated.reasoning_effort,
- thinking: translated.thinking?.type,
- maxTokens: translated.max_tokens,
+ promptCacheKey: translated.body.prompt_cache_key,
+ reasoningEffort: translated.body.reasoning_effort,
+ thinkingRequested: translated.thinking.requested,
+ thinkingEffective: translated.thinking.effective,
+ thinkingDegraded: translated.thinking.degraded,
+ maxTokens: translated.body.max_tokens,
})
- if (VERBOSE) log.debug("translated request body", { reqId: ctx.reqId, body: translated })
+ if (VERBOSE) log.debug("translated request body", { reqId: ctx.reqId, body: translated.body })
let upstream
try {
- upstream = await postKimi(translated, { sessionId: ctx.sessionId, signal: ctx.signal })
+ upstream = await postKimi(translated.body, { sessionId: ctx.sessionId, signal: ctx.signal })
} catch (err) {
if (err instanceof KimiError) {
log.warn("kimi error", { reqId: ctx.reqId, status: err.status, detail: err.detail })
diff --git a/src/providers/kimi/translate/accumulate.ts b/src/providers/kimi/translate/accumulate.ts
index 083cb47..b9e8447 100644
--- a/src/providers/kimi/translate/accumulate.ts
+++ b/src/providers/kimi/translate/accumulate.ts
@@ -1,4 +1,5 @@
import { mapUsageToAnthropic, reduceUpstream, type KimiUsage } from "./reducer.ts"
+import { makeThinkingSignature } from "./signature.ts"
export { UpstreamStreamError } from "./reducer.ts"
@@ -9,7 +10,7 @@ export interface AnthropicNonStreamResponse {
model: string
content: Array<
| { type: "text"; text: string }
- | { type: "thinking"; thinking: string }
+ | { type: "thinking"; thinking: string; signature: string }
| { type: "tool_use"; id: string; name: string; input: unknown }
>
stop_reason: "end_turn" | "tool_use" | "max_tokens" | null
@@ -88,7 +89,12 @@ export async function accumulateResponse(
const b = blocks.get(i)
if (!b) continue
if (b.kind === "thinking") {
- if (b.text) content.push({ type: "thinking", thinking: b.text })
+ if (b.text)
+ content.push({
+ type: "thinking",
+ thinking: b.text,
+ signature: makeThinkingSignature(opts.messageId, i),
+ })
} else if (b.kind === "text") {
if (b.text) content.push({ type: "text", text: b.text })
} else {
diff --git a/src/providers/kimi/translate/request.ts b/src/providers/kimi/translate/request.ts
index 8ca9178..008049a 100644
--- a/src/providers/kimi/translate/request.ts
+++ b/src/providers/kimi/translate/request.ts
@@ -29,14 +29,17 @@ export type KimiToolChoice =
| "required"
| { type: "function"; function: { name: string } }
+export type KimiAssistantMessage = {
+ role: "assistant"
+ content?: string | null
+ reasoning_content?: string
+ tool_calls?: KimiAssistantToolCall[]
+}
+
export type KimiMessage =
| { role: "system"; content: string }
| { role: "user"; content: string | KimiUserContentPart[] }
- | {
- role: "assistant"
- content?: string | null
- tool_calls?: KimiAssistantToolCall[]
- }
+ | KimiAssistantMessage
| { role: "tool"; tool_call_id: string; content: string | KimiToolResultPart[] }
export type KimiUserContentPart =
@@ -62,17 +65,31 @@ export interface TranslateOptions {
sessionId?: string
}
+export interface TranslateResult {
+ body: KimiChatRequest
+ thinking: {
+ requested: boolean
+ effective: boolean
+ degraded: boolean
+ }
+}
+
const DEFAULT_MAX_TOKENS = 32000
export function translateRequest(
req: AnthropicRequest,
opts: TranslateOptions = {},
-): KimiChatRequest {
- const messages = buildMessages(req)
- const tools = req.tools?.map(toKimiTool)
+): TranslateResult {
+ const requested = req.thinking?.type === "enabled"
+ // Kimi rejects requests where any prior assistant-tool-call turn lacks
+ // reasoning_content while thinking is enabled. If the history doesn't
+ // carry reasoning we must degrade this turn to non-thinking rather than
+ // 400 the user.
+ const degraded = requested && hasToolCallTurnWithoutThinking(req.messages)
+ const effective = requested && !degraded
- const thinkingDisabled = req.thinking?.type === "disabled"
- const effort = req.output_config?.effort ?? "high"
+ const messages = buildMessages(req, { includeReasoning: effective })
+ const tools = req.tools?.map(toKimiTool)
const out: KimiChatRequest = {
model: req.model,
@@ -81,15 +98,30 @@ export function translateRequest(
stream_options: { include_usage: true },
max_tokens: clampMaxTokens(req.max_tokens),
}
- if (!thinkingDisabled) {
- out.reasoning_effort = effort
+ if (effective) {
+ out.reasoning_effort = req.output_config?.effort ?? "medium"
out.thinking = { type: "enabled" }
}
if (tools && tools.length) out.tools = tools
const tool_choice = mapToolChoice(req.tool_choice)
if (tool_choice !== "auto") out.tool_choice = tool_choice
if (opts.sessionId) out.prompt_cache_key = opts.sessionId
- return out
+ return {
+ body: out,
+ thinking: { requested, effective, degraded },
+ }
+}
+
+function hasToolCallTurnWithoutThinking(messages: AnthropicMessage[]): boolean {
+ for (const msg of messages) {
+ if (msg.role !== "assistant") continue
+ const blocks = normalizeContent(msg.content)
+ const hasToolCall = blocks.some((b) => b.type === "tool_use")
+ if (!hasToolCall) continue
+ const hasThinking = blocks.some((b) => b.type === "thinking" && (b as { thinking?: string }).thinking)
+ if (!hasThinking) return true
+ }
+ return false
}
function clampMaxTokens(requested: number | undefined): number {
@@ -125,7 +157,10 @@ export function buildSystemMessage(system: AnthropicRequest["system"]): string |
return texts.join("\n\n")
}
-function buildMessages(req: AnthropicRequest): KimiMessage[] {
+function buildMessages(
+ req: AnthropicRequest,
+ opts: { includeReasoning: boolean },
+): KimiMessage[] {
const out: KimiMessage[] = []
const system = buildSystemMessage(req.system)
if (system) out.push({ role: "system", content: system })
@@ -135,7 +170,7 @@ function buildMessages(req: AnthropicRequest): KimiMessage[] {
if (msg.role === "user") {
pushUserMessages(out, blocks)
} else {
- pushAssistantMessage(out, blocks)
+ pushAssistantMessage(out, blocks, opts.includeReasoning)
}
}
return out
@@ -180,12 +215,19 @@ function pushUserMessages(out: KimiMessage[], blocks: AnthropicContentBlock[]):
flushBuffer()
}
-function pushAssistantMessage(out: KimiMessage[], blocks: AnthropicContentBlock[]): void {
+function pushAssistantMessage(
+ out: KimiMessage[],
+ blocks: AnthropicContentBlock[],
+ includeReasoning: boolean,
+): void {
const textParts: string[] = []
+ const thinkingParts: string[] = []
const toolCalls: KimiAssistantToolCall[] = []
for (const block of blocks) {
if (block.type === "text") {
if (block.text) textParts.push(block.text)
+ } else if (block.type === "thinking") {
+ if (includeReasoning && block.thinking) thinkingParts.push(block.thinking)
} else if (block.type === "tool_use") {
toolCalls.push({
id: block.id,
@@ -196,15 +238,18 @@ function pushAssistantMessage(out: KimiMessage[], blocks: AnthropicContentBlock[
},
})
}
- // thinking / image blocks from the assistant are dropped — Kimi's
- // assistant schema does not express them.
+ // image blocks from the assistant are dropped — Kimi's assistant
+ // schema does not express them.
}
- if (!textParts.length && !toolCalls.length) return
+ if (!textParts.length && !toolCalls.length && !thinkingParts.length) return
- const msg: Extract<KimiMessage, { role: "assistant" }> = { role: "assistant" }
- if (textParts.length) msg.content = textParts.join("")
- else msg.content = ""
+ const msg: KimiAssistantMessage = { role: "assistant" }
+ msg.content = textParts.length ? textParts.join("") : ""
+ // Kimi accepts one reasoning_content string per assistant turn. If a turn
+ // has multiple interleaved thinking blocks we concatenate in order —
+ // exact block/tool_use pairing can't be preserved over the wire.
+ if (thinkingParts.length) msg.reasoning_content = thinkingParts.join("\n\n")
if (toolCalls.length) msg.tool_calls = toolCalls
out.push(msg)
}
diff --git a/src/providers/kimi/translate/signature.ts b/src/providers/kimi/translate/signature.ts
new file mode 100644
index 0000000..9475a9b
--- /dev/null
+++ b/src/providers/kimi/translate/signature.ts
@@ -0,0 +1,7 @@
+// Opaque per-block signature for Anthropic thinking blocks. Claude Code
+// treats this as a passthrough string, so the only requirements are:
+// stable per (messageId, blockIndex) and identical between the streaming
+// and non-streaming paths.
+export function makeThinkingSignature(messageId: string, index: number): string {
+ return Buffer.from(`ccp:kimi:v1:${messageId}:${index}`).toString("base64url")
+}
diff --git a/src/providers/kimi/translate/stream.ts b/src/providers/kimi/translate/stream.ts
index 232380d..c0f792d 100644
--- a/src/providers/kimi/translate/stream.ts
+++ b/src/providers/kimi/translate/stream.ts
@@ -1,6 +1,7 @@
import { encodeSseEvent } from "../../../sse.ts"
import { createLogger } from "../../../log.ts"
import { mapUsageToAnthropic, reduceUpstream, UpstreamStreamError } from "./reducer.ts"
+import { makeThinkingSignature } from "./signature.ts"
const log = createLogger("kimi.stream")
@@ -74,6 +75,18 @@ export function translateStream(
})
break
case "thinking-stop":
+ // Emit the signature as a separate signature_delta right
+ // before content_block_stop — matches Anthropic's native
+ // wire format and keeps Claude Code's thinking-block parser
+ // happy on round-trip.
+ emit("content_block_delta", {
+ type: "content_block_delta",
+ index: e.index,
+ delta: {
+ type: "signature_delta",
+ signature: makeThinkingSignature(opts.messageId, e.index),
+ },
+ })
emit("content_block_stop", { type: "content_block_stop", index: e.index })
break
case "text-start":