Skip to content

Commit 7963fcc

Browse files
committed
fix: adding more comments and multiply localEstimate by safetyFactor
1 parent 017a1fb commit 7963fcc

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/api/providers/base-provider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export abstract class BaseProvider implements ApiHandler {
8787
return 0
8888
}
8989

90+
// First three requests, we will be using APi-based token counting to have a good start
91+
// and estimate the accuracy by collecting samples
9092
if (this.requestCount < 3) {
9193
this.requestCount++
9294
try {
@@ -97,7 +99,7 @@ export abstract class BaseProvider implements ApiHandler {
9799
return apiCount
98100
} catch (error) {
99101
const localEstimate = await localCountTokens(content, { useWorker: true })
100-
return localEstimate
102+
return Math.ceil(localEstimate * this.tokenComparator.getSafetyFactor())
101103
}
102104
}
103105

@@ -131,6 +133,6 @@ export abstract class BaseProvider implements ApiHandler {
131133
}
132134
}
133135

134-
return localEstimate
136+
return Math.ceil(localEstimate * this.tokenComparator.getSafetyFactor())
135137
}
136138
}

0 commit comments

Comments
 (0)