MCPcopy Index your code
hub / github.com/simstudioai/sim / updateUserStatsForWand

Function updateUserStatsForWand

apps/sim/app/api/wand/route.ts:92–153  ·  view source on GitHub ↗
(
  billingUserId: string,
  workspaceId: string | null,
  usage: {
    prompt_tokens?: number
    completion_tokens?: number
    total_tokens?: number
  },
  requestId: string,
  isBYOK = false
)

Source from the content-addressed store, hash-verified

90}
91
92async function updateUserStatsForWand(
93 billingUserId: string,
94 workspaceId: string | null,
95 usage: {
96 prompt_tokens?: number
97 completion_tokens?: number
98 total_tokens?: number
99 },
100 requestId: string,
101 isBYOK = false
102): Promise<void> {
103 if (!isBillingEnabled) {
104 return
105 }
106
107 if (!usage.total_tokens || usage.total_tokens <= 0) {
108 return
109 }
110
111 try {
112 const promptTokens = usage.prompt_tokens || 0
113 const completionTokens = usage.completion_tokens || 0
114
115 const modelName = useWandAzure ? wandModelName : 'gpt-4o'
116 let costToStore = 0
117
118 if (!isBYOK) {
119 const pricing = getModelPricing(modelName)
120 const costMultiplier = getCostMultiplier()
121 let modelCost = 0
122
123 if (pricing) {
124 const inputCost = (promptTokens / 1000000) * pricing.input
125 const outputCost = (completionTokens / 1000000) * pricing.output
126 modelCost = inputCost + outputCost
127 } else {
128 modelCost = (promptTokens / 1000000) * 0.005 + (completionTokens / 1000000) * 0.015
129 }
130
131 costToStore = modelCost * costMultiplier
132 }
133
134 await recordUsage({
135 userId: billingUserId,
136 workspaceId: workspaceId ?? undefined,
137 entries: [
138 {
139 category: 'model',
140 source: 'wand',
141 description: modelName,
142 cost: costToStore,
143 sourceReference: `wand:${requestId}`,
144 metadata: { inputTokens: promptTokens, outputTokens: completionTokens },
145 },
146 ],
147 })
148
149 await checkAndBillOverageThreshold(billingUserId)

Callers 2

flushUsageFunction · 0.85
route.tsFile · 0.85

Calls 5

getModelPricingFunction · 0.90
getCostMultiplierFunction · 0.90
recordUsageFunction · 0.90
errorMethod · 0.80

Tested by

no test coverage detected