Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export async function POST(
has_tools: false,
machine_id: null,
feature: null,
session_id: null,
};

await insertUsageRecord(coreUsageFields, metadataFields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ const createMicrodollarUsageColumns = (): TableColumn<UsageForTableDisplay>[] =>
},
{ key: 'id', title: 'ID', render: row => row.id, visible: false },
{ key: 'message_id', title: 'Message ID', render: row => row.message_id ?? '', visible: false },
{
key: 'session_id',
title: 'Session',
render: row => row.session_id || 'N/A',
visible: false,
},
{
key: 'upstream_id',
title: 'Upstream ID',
Expand Down
1 change: 1 addition & 0 deletions src/app/api/dev/consume-credits/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export async function POST(request: NextRequest): Promise<NextResponse> {
user_byok: false,
has_tools: false,
feature: null,
session_id: null,
};

// Use the existing countAndStoreUsage function
Expand Down
2 changes: 2 additions & 0 deletions src/app/api/fim/completions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export async function POST(request: NextRequest) {

// Use new shared helper for fraud & project headers
const { fraudHeaders, projectId } = extractFraudAndProjectHeaders(request);
const taskId = extractHeaderAndLimitLength(request, 'x-kilocode-taskid') ?? undefined;

// Extract properties for usage context
const tokenEstimates = estimateFimTokens(requestBody);
Expand Down Expand Up @@ -149,6 +150,7 @@ export async function POST(request: NextRequest) {
user_byok: !!userByok,
has_tools: false,
feature: validateFeatureHeader(request.headers.get(FEATURE_HEADER)),
session_id: taskId ?? null,
};

setTag('ui.ai_model', fimModel_withOpenRouterStyleProviderPrefix);
Expand Down
3 changes: 2 additions & 1 deletion src/app/api/openrouter/[...path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export async function POST(request: NextRequest): Promise<NextResponseType<unkno

// Use new shared helper for fraud & project headers
const { fraudHeaders, projectId } = extractFraudAndProjectHeaders(request);
const taskId = request.headers.get('X-KiloCode-TaskId') ?? undefined;
const taskId = extractHeaderAndLimitLength(request, 'x-kilocode-taskid') ?? undefined;
const { provider, userByok, customLlm } = await getProvider(
originalModelIdLowerCased,
requestBodyParsed,
Expand Down Expand Up @@ -322,6 +322,7 @@ export async function POST(request: NextRequest): Promise<NextResponseType<unkno
has_tools: (requestBodyParsed.tools?.length ?? 0) > 0,
botId,
feature: validateFeatureHeader(request.headers.get(FEATURE_HEADER)),
session_id: taskId ?? null,
};

setTag('ui.ai_model', requestBodyParsed.model);
Expand Down
61 changes: 61 additions & 0 deletions src/db/migrations/0021_orange_maggott.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
DROP VIEW "public"."microdollar_usage_view";--> statement-breakpoint
ALTER TABLE "microdollar_usage_metadata" ADD COLUMN IF NOT EXISTS "session_id" text;--> statement-breakpoint
CREATE VIEW "public"."microdollar_usage_view" AS (
SELECT
mu.id,
mu.kilo_user_id,
meta.message_id,
mu.cost,
mu.input_tokens,
mu.output_tokens,
mu.cache_write_tokens,
mu.cache_hit_tokens,
mu.created_at,
ip.http_ip AS http_x_forwarded_for,
city.vercel_ip_city AS http_x_vercel_ip_city,
country.vercel_ip_country AS http_x_vercel_ip_country,
meta.vercel_ip_latitude AS http_x_vercel_ip_latitude,
meta.vercel_ip_longitude AS http_x_vercel_ip_longitude,
ja4.ja4_digest AS http_x_vercel_ja4_digest,
mu.provider,
mu.model,
mu.requested_model,
meta.user_prompt_prefix,
spp.system_prompt_prefix,
meta.system_prompt_length,
ua.http_user_agent,
mu.cache_discount,
meta.max_tokens,
meta.has_middle_out_transform,
mu.has_error,
mu.abuse_classification,
mu.organization_id,
mu.inference_provider,
mu.project_id,
meta.status_code,
meta.upstream_id,
frfr.finish_reason,
meta.latency,
meta.moderation_latency,
meta.generation_time,
meta.is_byok,
meta.is_user_byok,
meta.streamed,
meta.cancelled,
edit.editor_name,
meta.has_tools,
meta.machine_id,
feat.feature,
meta.session_id
FROM "microdollar_usage" mu
LEFT JOIN "microdollar_usage_metadata" meta ON mu.id = meta.id
LEFT JOIN "http_ip" ip ON meta.http_ip_id = ip.http_ip_id
LEFT JOIN "vercel_ip_city" city ON meta.vercel_ip_city_id = city.vercel_ip_city_id
LEFT JOIN "vercel_ip_country" country ON meta.vercel_ip_country_id = country.vercel_ip_country_id
LEFT JOIN "ja4_digest" ja4 ON meta.ja4_digest_id = ja4.ja4_digest_id
LEFT JOIN "system_prompt_prefix" spp ON meta.system_prompt_prefix_id = spp.system_prompt_prefix_id
LEFT JOIN "http_user_agent" ua ON meta.http_user_agent_id = ua.http_user_agent_id
LEFT JOIN "finish_reason" frfr ON meta.finish_reason_id = frfr.finish_reason_id
LEFT JOIN "editor_name" edit ON meta.editor_name_id = edit.editor_name_id
LEFT JOIN "feature" feat ON meta.feature_id = feat.feature_id
);
Loading