Skip to content

Commit 2f659bc

Browse files
committed
Remove token counting changes, keep only cache support
- Removed custom countTokens override from claude-code.ts - Deleted claude-code-token-counting.spec.ts test file - Kept cache token collection and reporting functionality - Kept supportsPromptCache: true for all Claude Code models - Kept claude-code-caching.spec.ts tests This focuses the PR on enabling cache support without modifying token counting behavior.
1 parent 24eca3f commit 2f659bc

File tree

2 files changed

+0
-165
lines changed

2 files changed

+0
-165
lines changed

src/api/providers/__tests__/claude-code-token-counting.spec.ts

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

src/api/providers/claude-code.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,9 @@ import { filterMessagesForClaudeCode } from "../../integrations/claude-code/mess
77
import { BaseProvider } from "./base-provider"
88
import { t } from "../../i18n"
99
import { ApiHandlerOptions } from "../../shared/api"
10-
import { Tiktoken } from "tiktoken/lite"
11-
import o200kBase from "tiktoken/encoders/o200k_base"
12-
13-
// Conservative token estimate for images (even though Claude Code doesn't support them)
14-
// This matches the estimate used in src/utils/tiktoken.ts for consistency
15-
const IMAGE_TOKEN_ESTIMATE = 300
1610

1711
export class ClaudeCodeHandler extends BaseProvider implements ApiHandler {
1812
private options: ApiHandlerOptions
19-
private encoder: Tiktoken | null = null
2013

2114
constructor(options: ApiHandlerOptions) {
2215
super()
@@ -152,40 +145,4 @@ export class ClaudeCodeHandler extends BaseProvider implements ApiHandler {
152145
return null
153146
}
154147
}
155-
156-
/**
157-
* Override the base provider's token counting to provide accurate counting for Claude Code.
158-
* Claude Code uses the same tokenizer as Anthropic, so we don't need any fudge factor.
159-
* The actual token counts are reported accurately in the API response.
160-
*/
161-
override async countTokens(content: Anthropic.Messages.ContentBlockParam[]): Promise<number> {
162-
if (content.length === 0) {
163-
return 0
164-
}
165-
166-
let totalTokens = 0
167-
168-
// Lazily create and cache the encoder if it doesn't exist
169-
if (!this.encoder) {
170-
this.encoder = new Tiktoken(o200kBase.bpe_ranks, o200kBase.special_tokens, o200kBase.pat_str)
171-
}
172-
173-
// Process each content block
174-
for (const block of content) {
175-
if (block.type === "text") {
176-
const text = block.text || ""
177-
if (text.length > 0) {
178-
const tokens = this.encoder.encode(text)
179-
totalTokens += tokens.length
180-
}
181-
} else if (block.type === "image") {
182-
// Claude Code doesn't support images, but we handle them just in case
183-
totalTokens += IMAGE_TOKEN_ESTIMATE
184-
}
185-
}
186-
187-
// No fudge factor needed - Claude Code uses the same tokenizer as Anthropic
188-
// and reports accurate token counts in the API response
189-
return totalTokens
190-
}
191148
}

0 commit comments

Comments
 (0)