@@ -14,6 +14,7 @@ import { getCostMultiplier, isBillingEnabled } from '@/lib/core/config/feature-f
1414import { generateRequestId } from '@/lib/core/utils/request'
1515import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
1616import { enrichTableSchema } from '@/lib/table/llm/wand'
17+ import { getWorkspaceBilledAccountUserId } from '@/lib/workspaces/utils'
1718import { verifyWorkspaceMembership } from '@/app/api/workflows/utils'
1819import { extractResponseText , parseResponsesUsage } from '@/providers/openai/utils'
1920import { getModelPricing } from '@/providers/utils'
@@ -86,7 +87,8 @@ Use this context to calculate relative dates like "yesterday", "last week", "beg
8687}
8788
8889async function updateUserStatsForWand (
89- userId : string ,
90+ billingUserId : string ,
91+ workspaceId : string | null ,
9092 usage : {
9193 prompt_tokens ?: number
9294 completion_tokens ?: number
@@ -128,7 +130,8 @@ async function updateUserStatsForWand(
128130 }
129131
130132 await recordUsage ( {
131- userId,
133+ userId : billingUserId ,
134+ workspaceId : workspaceId ?? undefined ,
132135 entries : [
133136 {
134137 category : 'model' ,
@@ -143,7 +146,7 @@ async function updateUserStatsForWand(
143146 } ,
144147 } )
145148
146- await checkAndBillOverageThreshold ( userId )
149+ await checkAndBillOverageThreshold ( billingUserId )
147150 } catch ( error ) {
148151 logger . error ( `[${ requestId } ] Failed to update user stats for wand usage` , error )
149152 }
@@ -223,6 +226,21 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
223226 }
224227 }
225228
229+ let billingUserId = session . user . id
230+ if ( workspaceId ) {
231+ const workspaceBilledAccountUserId = await getWorkspaceBilledAccountUserId ( workspaceId )
232+ if ( ! workspaceBilledAccountUserId ) {
233+ logger . error ( `[${ requestId } ] Unable to resolve billed account for workspace` , {
234+ workspaceId,
235+ } )
236+ return NextResponse . json (
237+ { success : false , error : 'Unable to resolve billing account for this workspace' } ,
238+ { status : 500 }
239+ )
240+ }
241+ billingUserId = workspaceBilledAccountUserId
242+ }
243+
226244 let isBYOK = false
227245 let activeOpenAIKey = openaiApiKey
228246
@@ -339,7 +357,13 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
339357 }
340358
341359 usageRecorded = true
342- await updateUserStatsForWand ( session . user . id , finalUsage , requestId , isBYOK )
360+ await updateUserStatsForWand (
361+ billingUserId ,
362+ workspaceId ,
363+ finalUsage ,
364+ requestId ,
365+ isBYOK
366+ )
343367 }
344368
345369 try {
@@ -556,7 +580,8 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
556580 const usage = parseResponsesUsage ( completion . usage )
557581 if ( usage ) {
558582 await updateUserStatsForWand (
559- session . user . id ,
583+ billingUserId ,
584+ workspaceId ,
560585 {
561586 prompt_tokens : usage . promptTokens ,
562587 completion_tokens : usage . completionTokens ,
0 commit comments