@@ -924,8 +924,7 @@ export async function processDocumentAsync(
924924 . where ( eq ( document . id , documentId ) )
925925 return
926926 }
927- let totalEmbeddingTokens = 0
928- let embeddingIsBYOK = false
927+ let billableEmbeddingTokens = 0
929928 let embeddingModelName = kbEmbeddingModel
930929 let embeddingPricingId = kbEmbeddingModel
931930
@@ -992,17 +991,15 @@ export async function processDocumentAsync(
992991 logger . info ( `[${ documentId } ] Processing embedding batch ${ batchNum } /${ totalBatches } ` )
993992 const {
994993 embeddings : batchEmbeddings ,
995- totalTokens : batchTokens ,
996- isBYOK,
994+ billableTokens : batchBillableTokens ,
997995 modelName,
998996 pricingId,
999997 } = await generateEmbeddings ( batch , kbEmbeddingModel , ctx . workspaceId )
1000998 for ( const emb of batchEmbeddings ) {
1001999 embeddings . push ( emb )
10021000 }
1003- totalEmbeddingTokens += batchTokens
1001+ billableEmbeddingTokens += batchBillableTokens
10041002 if ( i === 0 ) {
1005- embeddingIsBYOK = isBYOK
10061003 embeddingModelName = modelName
10071004 embeddingPricingId = pricingId
10081005 }
@@ -1136,12 +1133,12 @@ export async function processDocumentAsync(
11361133 const processingTime = Date . now ( ) - startTime
11371134 logger . info ( `[${ documentId } ] Successfully processed document in ${ processingTime } ms` )
11381135
1139- if ( ! embeddingIsBYOK && totalEmbeddingTokens > 0 ) {
1136+ if ( billableEmbeddingTokens > 0 ) {
11401137 try {
11411138 const costMultiplier = getCostMultiplier ( )
11421139 const { total : cost } = calculateCost (
11431140 embeddingPricingId ,
1144- totalEmbeddingTokens ,
1141+ billableEmbeddingTokens ,
11451142 0 ,
11461143 false ,
11471144 costMultiplier
@@ -1158,7 +1155,7 @@ export async function processDocumentAsync(
11581155 description : embeddingModelName ,
11591156 cost,
11601157 sourceReference : `knowledge-document:${ documentId } :${ startTime } ` ,
1161- metadata : { inputTokens : totalEmbeddingTokens , outputTokens : 0 } ,
1158+ metadata : { inputTokens : billableEmbeddingTokens , outputTokens : 0 } ,
11621159 } ,
11631160 ] ,
11641161 } )
@@ -1170,7 +1167,7 @@ export async function processDocumentAsync(
11701167 } else {
11711168 logger . warn (
11721169 `[${ documentId } ] Embedding model "${ embeddingModelName } " has no pricing entry — billing skipped` ,
1173- { totalEmbeddingTokens , embeddingModelName }
1170+ { billableEmbeddingTokens , embeddingModelName }
11741171 )
11751172 }
11761173 } catch ( billingError ) {
0 commit comments