Skip to content

Commit 7e62d34

Browse files
committed
PR cleanup
1 parent e8de7de commit 7e62d34

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

src/api/providers/base-provider.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ const TOKEN_FUDGE_FACTOR = 1.5
1212
* Base class for API providers that implements common functionality
1313
*/
1414
export abstract class BaseProvider implements ApiHandler {
15+
// Cache the Tiktoken encoder instance since it's stateless
16+
private encoder: Tiktoken | null = null
1517
abstract createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream
1618
abstract getModel(): { id: string; info: ModelInfo }
1719

1820
/**
1921
* Default token counting implementation using tiktoken
2022
* Providers can override this to use their native token counting endpoints
2123
*
24+
* Uses a cached Tiktoken encoder instance for performance since it's stateless.
25+
* The encoder is created lazily on first use and reused for subsequent calls.
26+
*
2227
* @param content The content to count tokens for
2328
* @returns A promise resolving to the token count
2429
*/
@@ -27,17 +32,18 @@ export abstract class BaseProvider implements ApiHandler {
2732

2833
let totalTokens = 0
2934

30-
// Create encoder - currently we only use o200kBase
31-
// In the future, providers could override this method to use more specific tokenizers
32-
const encoder = new Tiktoken(o200kBase)
35+
// Lazily create and cache the encoder if it doesn't exist
36+
if (!this.encoder) {
37+
this.encoder = new Tiktoken(o200kBase)
38+
}
3339

34-
// Process each content block
40+
// Process each content block using the cached encoder
3541
for (const block of content) {
3642
if (block.type === "text") {
3743
// Use tiktoken for text token counting
3844
const text = block.text || ""
3945
if (text.length > 0) {
40-
const tokens = encoder.encode(text)
46+
const tokens = this.encoder.encode(text)
4147
totalTokens += tokens.length
4248
}
4349
} else if (block.type === "image") {

src/core/sliding-window/constants.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/core/sliding-window/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Anthropic } from "@anthropic-ai/sdk"
22
import { ApiHandler } from "../../api"
3-
import { TOKEN_BUFFER_PERCENTAGE } from "./constants"
43

5-
// Re-export constants for external use
6-
export { TOKEN_BUFFER_PERCENTAGE } from "./constants"
4+
/**
5+
* Default percentage of the context window to use as a buffer when deciding when to truncate
6+
*/
7+
export const TOKEN_BUFFER_PERCENTAGE = 0.1
78

89
/**
910
* Counts tokens for user content using the provider's token counting implementation.

0 commit comments

Comments
 (0)