Skip to content

Commit bba8f55

Browse files
committed
fix: improving token counting strategy - start with 3 API-based counting at the beginning + do API-based counting at 90% of effective threshold + minor factor/parameters tuning
1 parent 0a87061 commit bba8f55

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/api/providers/base-provider.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { countTokens as localCountTokens } from "../../utils/countTokens"
1313
*/
1414
class TokenCountComparator {
1515
private static readonly MAX_SAMPLES = 20
16-
private static readonly DEFAULT_SAFETY_FACTOR = 1.2
17-
private static readonly ADDITIONAL_SAFETY_FACTOR = 1.0
16+
private static readonly DEFAULT_SAFETY_FACTOR = 1.1
17+
private static readonly ADDITIONAL_SAFETY_FACTOR = 1.05
1818

1919
private samples: Array<{ local: number; api: number }> = []
2020
private safetyFactor = TokenCountComparator.DEFAULT_SAFETY_FACTOR
@@ -41,7 +41,7 @@ class TokenCountComparator {
4141

4242
const totalRatio = this.samples.reduce((sum, sample) => sum + sample.api / sample.local, 0)
4343
const averageRatio = totalRatio / this.samples.length
44-
this.safetyFactor = Math.max(1, averageRatio) * TokenCountComparator.ADDITIONAL_SAFETY_FACTOR
44+
this.safetyFactor = averageRatio * TokenCountComparator.ADDITIONAL_SAFETY_FACTOR
4545
}
4646

4747
public getSampleCount(): number {
@@ -59,7 +59,7 @@ class TokenCountComparator {
5959
* Base class for API providers that implements common functionality
6060
*/
6161
export abstract class BaseProvider implements ApiHandler {
62-
protected isFirstRequest = true
62+
protected requestCount = 0
6363
protected tokenComparator = new TokenCountComparator()
6464

6565
abstract createMessage(
@@ -87,10 +87,8 @@ export abstract class BaseProvider implements ApiHandler {
8787
return 0
8888
}
8989

90-
const providerName = this.constructor.name
91-
92-
if (this.isFirstRequest) {
93-
this.isFirstRequest = false
90+
if (this.requestCount < 3) {
91+
this.requestCount++
9492
try {
9593
const apiCount = await this.apiBasedTokenCount(content)
9694
const localEstimate = await localCountTokens(content, { useWorker: true })
@@ -110,7 +108,13 @@ export abstract class BaseProvider implements ApiHandler {
110108
const allowedTokens = getAllowedTokens(contextWindow, options.maxTokens)
111109
const projectedTokens = options.totalTokens + localEstimate * this.tokenComparator.getSafetyFactor()
112110

111+
// Checking if we're at 90% of effective threshold for earlier API-based counting
112+
const effectiveThreshold = options.effectiveThreshold ?? 100
113+
const contextPercent = (100 * projectedTokens) / contextWindow
114+
const shouldUseApiCountingEarly = contextPercent >= effectiveThreshold * 0.9
115+
113116
if (
117+
shouldUseApiCountingEarly ||
114118
isSafetyNetTriggered({
115119
projectedTokens,
116120
contextWindow,

0 commit comments

Comments
 (0)