Skip to content

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

Rust

kimi: track request start time for accurate TTFB metrics

Raine Virtafc8fdf0c2ad5
diff --git a/src/providers/kimi/client.ts b/src/providers/kimi/client.ts
index 860f6ea..6486a68 100644
--- a/src/providers/kimi/client.ts
+++ b/src/providers/kimi/client.ts
@@ -9,6 +9,7 @@ export interface KimiResponse {
body: ReadableStream<Uint8Array>
status: number
headers: Headers
+ requestStartTime: number
}
export class KimiError extends Error {
@@ -29,6 +30,7 @@ export async function postKimi(
): Promise<KimiResponse> {
const log = ctx.childLogger("kimi.client")
let auth = await getAuth()
+ const requestStartTime = Date.now()
let resp = await doFetch(auth.access, body, log, ctx.signal)
if (resp.status === 401) {
@@ -58,9 +60,12 @@ export async function postKimi(
if (!resp.body) throw new KimiError(500, "Upstream returned no body")
- log.debug("upstream response", { status: resp.status })
+ log.debug("upstream response", {
+ status: resp.status,
+ timeToHeadersMs: Date.now() - requestStartTime,
+ })
- return { body: resp.body, status: resp.status, headers: resp.headers }
+ return { body: resp.body, status: resp.status, headers: resp.headers, requestStartTime }
}
async function doFetch(
@@ -77,17 +82,19 @@ async function doFetch(
...fp,
})
+ const bodyJson = JSON.stringify(body)
log.debug("posting to kimi", {
url: `${API_BASE_URL}/chat/completions`,
model: body.model,
messageCount: body.messages.length,
toolCount: body.tools?.length ?? 0,
+ requestBodyBytes: new TextEncoder().encode(bodyJson).length,
})
return fetch(`${API_BASE_URL}/chat/completions`, {
method: "POST",
headers,
- body: JSON.stringify(body),
+ body: bodyJson,
signal,
})
}
diff --git a/src/providers/kimi/index.ts b/src/providers/kimi/index.ts
index 4bb1966..6f2635b 100644
--- a/src/providers/kimi/index.ts
+++ b/src/providers/kimi/index.ts
@@ -114,6 +114,7 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Prom
messageId,
model: body.model,
log: ctx.childLogger("kimi.stream"),
+ requestStartTime: upstream.requestStartTime,
onFinish: (finish) => {
const mappedUsage = finish.usage ? mapUsageToAnthropic(finish.usage) : undefined
log.debug("stream finish", {
diff --git a/src/providers/kimi/translate/stream.ts b/src/providers/kimi/translate/stream.ts
index e4dc228..692e87b 100644
--- a/src/providers/kimi/translate/stream.ts
+++ b/src/providers/kimi/translate/stream.ts
@@ -15,6 +15,7 @@ export function translateStream(
messageId: string
model: string
log: Logger
+ requestStartTime?: number
onFinish?: (finish: {
stopReason: "end_turn" | "tool_use" | "max_tokens"
usage?: Parameters<typeof mapUsageToAnthropic>[0]
@@ -188,10 +189,15 @@ export function translateStream(
})
}
} finally {
+ const now = Date.now()
+ const timeToFirstChunkMs = opts.requestStartTime && firstChunkAt
+ ? firstChunkAt - opts.requestStartTime
+ : undefined
opts.log.debug("stream summary", {
chunkCount: stats.chunkCount,
- firstChunkMs: firstChunkAt ? firstChunkAt - streamStart : undefined,
- totalMs: Date.now() - streamStart,
+ timeToFirstChunkMs,
+ streamDurationMs: firstChunkAt ? now - firstChunkAt : undefined,
+ totalMs: opts.requestStartTime ? now - opts.requestStartTime : now - streamStart,
reasoningChars,
contentChars,
toolCount,