Skip to content

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

Rust

extract shared countAnthropicTokens helper for codex and kimi

Raine Virta4b0eacde0659
diff --git a/src/providers/codex/count-tokens.ts b/src/providers/codex/count-tokens.ts
index dbd49b5..eab82b1 100644
--- a/src/providers/codex/count-tokens.ts
+++ b/src/providers/codex/count-tokens.ts
@@ -2,20 +2,13 @@ import { encode } from "gpt-tokenizer/model/gpt-4o";
import type { AnthropicRequest } from "../../anthropic/schema.ts";
import type { ResponsesRequest } from "./translate/request.ts";
import {
- countAnthropicRequestTokensWithSystem,
+ countAnthropicTokens,
IMAGE_TOKEN_ESTIMATE,
} from "../shared/count-tokens.ts";
import { countToolSchemaTokens } from "../shared/tool-schema.ts";
export function countTokens(req: AnthropicRequest): number {
- return countAnthropicRequestTokensWithSystem({
- req,
- countToken: (value) => encode(value).length,
- tools: req.tools,
- readToolName: (tool) => tool.name,
- readToolDescription: (tool) => tool.description,
- readToolSchema: (tool) => tool.input_schema,
- });
+ return countAnthropicTokens(req, (value) => encode(value).length);
}
export function countTranslatedTokens(
diff --git a/src/providers/kimi/count-tokens.ts b/src/providers/kimi/count-tokens.ts
index bb7e8e5..c511921 100644
--- a/src/providers/kimi/count-tokens.ts
+++ b/src/providers/kimi/count-tokens.ts
@@ -2,7 +2,7 @@ import { encode } from "gpt-tokenizer/model/gpt-4o";
import type { AnthropicRequest } from "../../anthropic/schema.ts";
import type { KimiChatRequest } from "./translate/request.ts";
import {
- countAnthropicRequestTokensWithSystem,
+ countAnthropicTokens,
IMAGE_TOKEN_ESTIMATE,
countContentParts,
} from "../shared/count-tokens.ts";
@@ -11,15 +11,7 @@ import { countToolSchemaTokens } from "../shared/tool-schema.ts";
// 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 {
- return countAnthropicRequestTokensWithSystem({
- req,
- countToken: (value) => encode(value).length,
- tools: req.tools,
- readToolName: (tool) => tool.name,
- readToolDescription: (tool) => tool.description,
- readToolSchema: (tool) => tool.input_schema,
- includeThinking: true,
- });
+ return countAnthropicTokens(req, (value) => encode(value).length, true);
}
export function countTranslatedTokens(req: KimiChatRequest): number {
diff --git a/src/providers/shared/count-tokens.ts b/src/providers/shared/count-tokens.ts
index 6913925..7e05c65 100644
--- a/src/providers/shared/count-tokens.ts
+++ b/src/providers/shared/count-tokens.ts
@@ -23,6 +23,31 @@ export function countContentParts(
export type AnthropicRequestTokenCounter = (text: string) => number;
+export function countAnthropicTokens(
+ req: AnthropicRequest,
+ countToken: AnthropicRequestTokenCounter,
+ includeThinking = false,
+): number {
+ const base = {
+ req,
+ countToken,
+ tools: req.tools,
+ readToolName: (tool: { name: string; description?: string; input_schema: unknown }) =>
+ tool.name,
+ readToolDescription: (tool: {
+ name: string;
+ description?: string;
+ input_schema: unknown;
+ }) => tool.description,
+ readToolSchema: (tool: { name: string; description?: string; input_schema: unknown }) =>
+ tool.input_schema,
+ };
+ if (includeThinking) {
+ return countAnthropicRequestTokensWithSystem({ ...base, includeThinking: true });
+ }
+ return countAnthropicRequestTokensWithSystem({ ...base, includeThinking: false });
+}
+
type AnthropicToolReaders<TTool> = {
readToolName: (tool: TTool) => string;
readToolDescription: (tool: TTool) => string | undefined;