From 11429e525e6eb20cfc34726366fd800c5b2ebf86 Mon Sep 17 00:00:00 2001 From: Mark IJbema Date: Tue, 19 May 2026 16:07:16 +0200 Subject: [PATCH] feat(gateway): capture x-kilo-request and x-kilo-session headers Extract the kilocode client's per-message id (`x-kilo-request`) into `MicrodollarUsageContext.clientRequestId` and accept `x-kilo-session` as a fallback session id when `x-kilocode-taskid` is absent. The new `clientRequestId` field has no current consumer; it lands here as plumbing for the model-experiments attribution write (PR #3325) so later changes don't have to touch the dozens of `MicrodollarUsageContext` construction sites in routes, tests, and dev helpers. `session_id` already feeds existing usage telemetry, so the `x-kilo-session` fallback is immediately useful for non-kilocode clients that supply a session header but not a kilocode task id. --- apps/web/src/app/api/openrouter/[...path]/route.ts | 12 ++++++++++-- apps/web/src/lib/ai-gateway/processUsage.types.ts | 7 +++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/apps/web/src/app/api/openrouter/[...path]/route.ts b/apps/web/src/app/api/openrouter/[...path]/route.ts index b063c4bb7f2..1738fad9da8 100644 --- a/apps/web/src/app/api/openrouter/[...path]/route.ts +++ b/apps/web/src/app/api/openrouter/[...path]/route.ts @@ -223,6 +223,13 @@ export async function POST(request: NextRequest): Promise 0, botId, tokenSource, feature, - session_id: taskId ?? null, + session_id: taskId ?? sessionHeader ?? null, mode: modeHeader, auto_model: autoModel, ttfb_ms: null, + clientRequestId, }; setTag('ui.ai_model', requestBodyParsed.body.model); diff --git a/apps/web/src/lib/ai-gateway/processUsage.types.ts b/apps/web/src/lib/ai-gateway/processUsage.types.ts index f88ea58ec62..e7bbba77db3 100644 --- a/apps/web/src/lib/ai-gateway/processUsage.types.ts +++ b/apps/web/src/lib/ai-gateway/processUsage.types.ts @@ -144,6 +144,13 @@ export type MicrodollarUsageContext = { auto_model: string | null; /** Time to first byte from the upstream provider, in milliseconds. Set after the upstream request returns. */ ttfb_ms: number | null; + /** + * Client-supplied per-message id from the `x-kilo-request` header. + * Joinable to PostHog `Feedback Submitted.parentMessageID`. Optional + * because pre-existing construction sites (routes, tests, dev helpers) + * do not need to know about it. + */ + clientRequestId?: string | null; }; export type CoreUsageWithMetaData = {