Skip to content

Commit b265a7a

Browse files
committed
Stop making count_tokens requests
1 parent dd92453 commit b265a7a

File tree

4 files changed

+1
-79
lines changed

4 files changed

+1
-79
lines changed

src/api/providers/__tests__/minimax.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ import { MiniMaxHandler } from "../minimax"
1616

1717
vitest.mock("@anthropic-ai/sdk", () => {
1818
const mockCreate = vitest.fn()
19-
const mockCountTokens = vitest.fn()
2019
return {
2120
Anthropic: vitest.fn(() => ({
2221
messages: {
2322
create: mockCreate,
24-
countTokens: mockCountTokens,
2523
},
2624
})),
2725
}
@@ -30,13 +28,11 @@ vitest.mock("@anthropic-ai/sdk", () => {
3028
describe("MiniMaxHandler", () => {
3129
let handler: MiniMaxHandler
3230
let mockCreate: any
33-
let mockCountTokens: any
3431

3532
beforeEach(() => {
3633
vitest.clearAllMocks()
3734
const anthropicInstance = (Anthropic as unknown as any)()
3835
mockCreate = anthropicInstance.messages.create
39-
mockCountTokens = anthropicInstance.messages.countTokens
4036
})
4137

4238
describe("International MiniMax (default)", () => {

src/api/providers/anthropic.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -402,30 +402,4 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
402402
const content = message.content.find(({ type }) => type === "text")
403403
return content?.type === "text" ? content.text : ""
404404
}
405-
406-
/**
407-
* Counts tokens for the given content using Anthropic's API
408-
*
409-
* @param content The content blocks to count tokens for
410-
* @returns A promise resolving to the token count
411-
*/
412-
override async countTokens(content: Array<Anthropic.Messages.ContentBlockParam>): Promise<number> {
413-
try {
414-
// Use the current model
415-
const { id: model } = this.getModel()
416-
417-
const response = await this.client.messages.countTokens({
418-
model,
419-
messages: [{ role: "user", content: content }],
420-
})
421-
422-
return response.input_tokens
423-
} catch (error) {
424-
// Log error but fallback to tiktoken estimation
425-
console.warn("Anthropic token counting failed, using fallback", error)
426-
427-
// Use the base provider's implementation as fallback
428-
return super.countTokens(content)
429-
}
430-
}
431405
}

src/api/providers/gemini.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
type GenerateContentConfig,
77
type GroundingMetadata,
88
FunctionCallingConfigMode,
9-
Content,
109
} from "@google/genai"
1110
import type { JWTInput } from "google-auth-library"
1211

@@ -15,7 +14,7 @@ import { type ModelInfo, type GeminiModelId, geminiDefaultModelId, geminiModels
1514
import type { ApiHandlerOptions } from "../../shared/api"
1615
import { safeJsonParse } from "../../shared/safeJsonParse"
1716

18-
import { convertAnthropicContentToGemini, convertAnthropicMessageToGemini } from "../transform/gemini-format"
17+
import { convertAnthropicMessageToGemini } from "../transform/gemini-format"
1918
import { t } from "i18next"
2019
import type { ApiStream, GroundingSource } from "../transform/stream"
2120
import { getModelParams } from "../transform/model-params"
@@ -431,30 +430,6 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
431430
}
432431
}
433432

434-
override async countTokens(content: Array<Anthropic.Messages.ContentBlockParam>): Promise<number> {
435-
try {
436-
const { id: model } = this.getModel()
437-
438-
const countTokensRequest = {
439-
model,
440-
// Token counting does not need encrypted continuation; always drop thoughtSignature.
441-
contents: convertAnthropicContentToGemini(content, { includeThoughtSignatures: false }),
442-
}
443-
444-
const response = await this.client.models.countTokens(countTokensRequest)
445-
446-
if (response.totalTokens === undefined) {
447-
console.warn("Gemini token counting returned undefined, using fallback")
448-
return super.countTokens(content)
449-
}
450-
451-
return response.totalTokens
452-
} catch (error) {
453-
console.warn("Gemini token counting failed, using fallback", error)
454-
return super.countTokens(content)
455-
}
456-
}
457-
458433
public getThoughtSignature(): string | undefined {
459434
return this.lastThoughtSignature
460435
}

src/api/providers/minimax.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -303,27 +303,4 @@ export class MiniMaxHandler extends BaseProvider implements SingleCompletionHand
303303
const content = message.content.find(({ type }) => type === "text")
304304
return content?.type === "text" ? content.text : ""
305305
}
306-
307-
/**
308-
* Counts tokens for the given content using Anthropic's token counting
309-
* Falls back to base provider's tiktoken estimation if counting fails
310-
*/
311-
override async countTokens(content: Array<Anthropic.Messages.ContentBlockParam>): Promise<number> {
312-
try {
313-
const { id: model } = this.getModel()
314-
315-
const response = await this.client.messages.countTokens({
316-
model,
317-
messages: [{ role: "user", content: content }],
318-
})
319-
320-
return response.input_tokens
321-
} catch (error) {
322-
// Log error but fallback to tiktoken estimation
323-
console.warn("MiniMax token counting failed, using fallback", error)
324-
325-
// Use the base provider's implementation as fallback
326-
return super.countTokens(content)
327-
}
328-
}
329306
}

0 commit comments

Comments
 (0)