link the usage with the session to display together

This commit is contained in:
Owen
2026-08-11 16:14:46 -04:00
parent c42df737b0
commit f7aca85417
9 changed files with 129 additions and 8 deletions
+2 -2
View File
@@ -1,4 +1,3 @@
import { randomUUID } from "crypto";
import { logsDb, db, orgs, aiSessionLog, type AiProvider } from "@server/db";
import type { InferInsertModel } from "drizzle-orm";
import logger from "@server/logger";
@@ -169,6 +168,7 @@ function truncateBody(value: string): { value: string; truncated: boolean } {
}
export function logAiSession(data: {
sessionId: string;
capability: AiCapability;
provider: AiProvider;
requestedModel: string | undefined;
@@ -230,7 +230,7 @@ export function logAiSession(data: {
}
sessionLogBuffer.push({
sessionId: randomUUID(),
sessionId: data.sessionId,
orgId: sanitizeString(data.orgId),
providerId: data.provider.providerId,
capability: data.capability,
+10 -1
View File
@@ -1,3 +1,4 @@
import { randomUUID } from "crypto";
import { Request, Response } from "express";
import { and, eq, inArray } from "drizzle-orm";
import {
@@ -594,6 +595,12 @@ export function recordAiGatewayCompletion(args: {
const pricing = getModelPricing(provider.type as AiProviderType, model);
const cost = calculateAiCost(pricing, usage);
// Shared by the usage record and the session log so the two can be
// joined later to show token/cost usage alongside the transcript -
// generated up front since neither buffered insert's row id is known
// until its next batch flush.
const sessionId = randomUUID();
logger.info("AI gateway request usage", {
capability,
providerId: provider.providerId,
@@ -618,7 +625,8 @@ export function recordAiGatewayCompletion(args: {
userId: requestUserId,
requestedModel: model ?? "unknown",
usage,
costUsd: cost?.totalCost ?? null
costUsd: cost?.totalCost ?? null,
sessionId
});
if (budgets.length > 0) {
@@ -635,6 +643,7 @@ export function recordAiGatewayCompletion(args: {
}
logAiSession({
sessionId,
capability,
provider,
requestedModel,