Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
share system token counting
Raine Virtaee93378b4d87
diff --git a/src/providers/codex/count-tokens.ts b/src/providers/codex/count-tokens.tsindex 3a1253f..b45f867 100644--- a/src/providers/codex/count-tokens.ts+++ b/src/providers/codex/count-tokens.ts@@ -1,17 +1,13 @@import { encode } from "gpt-tokenizer/model/gpt-4o";import type { AnthropicRequest } from "../../anthropic/schema.ts";import type { ResponsesRequest } from "./translate/request.ts";-import { flattenSystemText } from "../translate/anthropic-content.ts";-import { countAnthropicRequestTokens } from "../shared/count-tokens.ts";+import { countAnthropicRequestTokensWithSystem } from "../shared/count-tokens.ts";import { countToolSchemaTokens } from "../shared/tool-schema.ts";const IMAGE_TOKEN_ESTIMATE = 2000;export function countTokens(req: AnthropicRequest): number {- let total = 0;- const instructions = flattenSystemText(req.system);- if (instructions) total += encode(instructions).length;- total += countAnthropicRequestTokens({+ return countAnthropicRequestTokensWithSystem({req,countToken: (value) => encode(value).length,tools: req.tools,@@ -19,7 +15,6 @@ export function countTokens(req: AnthropicRequest): number {readToolDescription: (tool) => tool.description,readToolSchema: (tool) => tool.input_schema,});- return total;}export function countTranslatedTokens(diff --git a/src/providers/kimi/count-tokens.ts b/src/providers/kimi/count-tokens.tsindex 2ef710f..4d3b188 100644--- a/src/providers/kimi/count-tokens.ts+++ b/src/providers/kimi/count-tokens.ts@@ -1,8 +1,7 @@import { encode } from "gpt-tokenizer/model/gpt-4o";import type { AnthropicRequest } from "../../anthropic/schema.ts";import type { KimiChatRequest } from "./translate/request.ts";-import { flattenSystemText } from "../translate/anthropic-content.ts";-import { countAnthropicRequestTokens } from "../shared/count-tokens.ts";+import { countAnthropicRequestTokensWithSystem } from "../shared/count-tokens.ts";import { countToolSchemaTokens } from "../shared/tool-schema.ts";const IMAGE_TOKEN_ESTIMATE = 2000;@@ -10,10 +9,7 @@ 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 {- let total = 0;- const system = flattenSystemText(req.system);- if (system) total += encode(system).length;- total += countAnthropicRequestTokens({+ return countAnthropicRequestTokensWithSystem({req,countToken: (value) => encode(value).length,tools: req.tools,@@ -22,7 +18,6 @@ export function countTokens(req: AnthropicRequest): number {readToolSchema: (tool) => tool.input_schema,includeThinking: true,});- return total;}export function countTranslatedTokens(req: KimiChatRequest): number {diff --git a/src/providers/shared/count-tokens.ts b/src/providers/shared/count-tokens.tsindex 4dcf30e..e6538e9 100644--- a/src/providers/shared/count-tokens.ts+++ b/src/providers/shared/count-tokens.ts@@ -1,5 +1,9 @@import { countToolSchemaTokens } from "./tool-schema.ts";-import { normalizeContent, toolResultToString } from "../translate/anthropic-content.ts";+import {+ flattenSystemText,+ normalizeContent,+ toolResultToString,+} from "../translate/anthropic-content.ts";import type { AnthropicRequest } from "../../anthropic/schema.ts";const IMAGE_TOKEN_ESTIMATE = 2000;@@ -70,3 +74,27 @@ export function countAnthropicRequestTokens<TTool>(total += req.messages.length * 4;return total;}++export function countAnthropicRequestTokensWithSystem<TTool>(+ options: CountAnthropicRequestTokenOptions<TTool> & {+ includeThinking?: false;+ },+): number;++export function countAnthropicRequestTokensWithSystem<TTool>(+ options: CountAnthropicRequestTokenOptions<TTool> & {+ includeThinking: true;+ },+): number;++export function countAnthropicRequestTokensWithSystem<TTool>(+ options: CountAnthropicRequestTokenOptions<TTool> & { includeThinking?: boolean },+): number {+ let total = 0;+ const system = flattenSystemText(options.req.system);+ if (system) total += options.countToken(system);+ total += options.includeThinking+ ? countAnthropicRequestTokens({ ...options, includeThinking: true })+ : countAnthropicRequestTokens({ ...options, includeThinking: false });+ return total;+}