Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

share image token estimate and content-part counting helper

Raine Virta91f53569f33f
diff --git a/src/providers/codex/count-tokens.ts b/src/providers/codex/count-tokens.ts
index b45f867..dbd49b5 100644
--- a/src/providers/codex/count-tokens.ts
+++ b/src/providers/codex/count-tokens.ts
@@ -1,11 +1,12 @@
import { encode } from "gpt-tokenizer/model/gpt-4o";
import type { AnthropicRequest } from "../../anthropic/schema.ts";
import type { ResponsesRequest } from "./translate/request.ts";
-import { countAnthropicRequestTokensWithSystem } from "../shared/count-tokens.ts";
+import {
+ countAnthropicRequestTokensWithSystem,
+ IMAGE_TOKEN_ESTIMATE,
+} from "../shared/count-tokens.ts";
import { countToolSchemaTokens } from "../shared/tool-schema.ts";
-const IMAGE_TOKEN_ESTIMATE = 2000;
-
export function countTokens(req: AnthropicRequest): number {
return countAnthropicRequestTokensWithSystem({
req,
diff --git a/src/providers/kimi/count-tokens.ts b/src/providers/kimi/count-tokens.ts
index 4d3b188..bb7e8e5 100644
--- a/src/providers/kimi/count-tokens.ts
+++ b/src/providers/kimi/count-tokens.ts
@@ -1,11 +1,13 @@
import { encode } from "gpt-tokenizer/model/gpt-4o";
import type { AnthropicRequest } from "../../anthropic/schema.ts";
import type { KimiChatRequest } from "./translate/request.ts";
-import { countAnthropicRequestTokensWithSystem } from "../shared/count-tokens.ts";
+import {
+ countAnthropicRequestTokensWithSystem,
+ IMAGE_TOKEN_ESTIMATE,
+ countContentParts,
+} from "../shared/count-tokens.ts";
import { countToolSchemaTokens } from "../shared/tool-schema.ts";
-const IMAGE_TOKEN_ESTIMATE = 2000;
-
// Approximate: Kimi's tokenizer isn't gpt-tokenizer, but Claude Code's
// compaction logic only needs a monotonic estimate, not an exact count.
export function countTokens(req: AnthropicRequest): number {
@@ -26,13 +28,7 @@ export function countTranslatedTokens(req: KimiChatRequest): number {
if (m.role === "system") {
total += encode(m.content).length;
} else if (m.role === "user") {
- if (typeof m.content === "string") total += encode(m.content).length;
- else {
- for (const p of m.content) {
- if (p.type === "text") total += encode(p.text).length;
- else total += IMAGE_TOKEN_ESTIMATE;
- }
- }
+ total += countContentParts(m.content, (v) => encode(v).length);
} else if (m.role === "assistant") {
if (typeof m.content === "string") total += encode(m.content).length;
if (m.reasoning_content) total += encode(m.reasoning_content).length;
@@ -41,13 +37,7 @@ export function countTranslatedTokens(req: KimiChatRequest): number {
total += encode(tc.function.arguments).length;
}
} else if (m.role === "tool") {
- if (typeof m.content === "string") total += encode(m.content).length;
- else {
- for (const p of m.content) {
- if (p.type === "text") total += encode(p.text).length;
- else total += IMAGE_TOKEN_ESTIMATE;
- }
- }
+ total += countContentParts(m.content, (v) => encode(v).length);
}
}
diff --git a/src/providers/shared/count-tokens.ts b/src/providers/shared/count-tokens.ts
index e6538e9..6913925 100644
--- a/src/providers/shared/count-tokens.ts
+++ b/src/providers/shared/count-tokens.ts
@@ -6,7 +6,20 @@ import {
} from "../translate/anthropic-content.ts";
import type { AnthropicRequest } from "../../anthropic/schema.ts";
-const IMAGE_TOKEN_ESTIMATE = 2000;
+export const IMAGE_TOKEN_ESTIMATE = 2000;
+
+export function countContentParts(
+ content: string | { type: string; text?: string }[],
+ countToken: (text: string) => number,
+): number {
+ if (typeof content === "string") return countToken(content);
+ let total = 0;
+ for (const p of content) {
+ if (p.type === "text") total += countToken(p.text ?? "");
+ else total += IMAGE_TOKEN_ESTIMATE;
+ }
+ return total;
+}
export type AnthropicRequestTokenCounter = (text: string) => number;