Skip to content

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

Rust

share anthropic token counting

Raine Virtad857ab372f4e
diff --git a/src/providers/codex/count-tokens.ts b/src/providers/codex/count-tokens.ts
index 204ae21..3a1253f 100644
--- a/src/providers/codex/count-tokens.ts
+++ b/src/providers/codex/count-tokens.ts
@@ -1,11 +1,8 @@
import { encode } from "gpt-tokenizer/model/gpt-4o";
import type { AnthropicRequest } from "../../anthropic/schema.ts";
import type { ResponsesRequest } from "./translate/request.ts";
-import {
- flattenSystemText,
- normalizeContent,
- toolResultToString,
-} from "../translate/anthropic-content.ts";
+import { flattenSystemText } from "../translate/anthropic-content.ts";
+import { countAnthropicRequestTokens } from "../shared/count-tokens.ts";
import { countToolSchemaTokens } from "../shared/tool-schema.ts";
const IMAGE_TOKEN_ESTIMATE = 2000;
@@ -14,31 +11,14 @@ export function countTokens(req: AnthropicRequest): number {
let total = 0;
const instructions = flattenSystemText(req.system);
if (instructions) total += encode(instructions).length;
-
- for (const msg of req.messages) {
- const blocks = normalizeContent(msg.content);
- for (const block of blocks) {
- if (block.type === "text") {
- total += encode(block.text).length;
- } else if (block.type === "image") {
- total += IMAGE_TOKEN_ESTIMATE;
- } else if (block.type === "tool_use") {
- total += encode(block.name).length;
- total += encode(JSON.stringify(block.input ?? {})).length;
- } else if (block.type === "tool_result") {
- total += encode(toolResultToString(block.content)).length;
- }
- }
- }
-
- total += countToolSchemaTokens(
- req.tools,
- (tool) => tool.name,
- (tool) => tool.description,
- (tool) => tool.input_schema,
- );
-
- total += req.messages.length * 4;
+ total += countAnthropicRequestTokens({
+ req,
+ countToken: (value) => encode(value).length,
+ tools: req.tools,
+ readToolName: (tool) => tool.name,
+ readToolDescription: (tool) => tool.description,
+ readToolSchema: (tool) => tool.input_schema,
+ });
return total;
}
diff --git a/src/providers/kimi/count-tokens.ts b/src/providers/kimi/count-tokens.ts
index 96e4f75..2ef710f 100644
--- a/src/providers/kimi/count-tokens.ts
+++ b/src/providers/kimi/count-tokens.ts
@@ -1,11 +1,8 @@
import { encode } from "gpt-tokenizer/model/gpt-4o";
import type { AnthropicRequest } from "../../anthropic/schema.ts";
import type { KimiChatRequest } from "./translate/request.ts";
-import {
- flattenSystemText,
- normalizeContent,
- toolResultToString,
-} from "../translate/anthropic-content.ts";
+import { flattenSystemText } from "../translate/anthropic-content.ts";
+import { countAnthropicRequestTokens } from "../shared/count-tokens.ts";
import { countToolSchemaTokens } from "../shared/tool-schema.ts";
const IMAGE_TOKEN_ESTIMATE = 2000;
@@ -16,33 +13,15 @@ export function countTokens(req: AnthropicRequest): number {
let total = 0;
const system = flattenSystemText(req.system);
if (system) total += encode(system).length;
-
- for (const msg of req.messages) {
- const blocks = normalizeContent(msg.content);
- for (const block of blocks) {
- if (block.type === "text") {
- total += encode(block.text).length;
- } else if (block.type === "image") {
- total += IMAGE_TOKEN_ESTIMATE;
- } else if (block.type === "tool_use") {
- total += encode(block.name).length;
- total += encode(JSON.stringify(block.input ?? {})).length;
- } else if (block.type === "tool_result") {
- total += encode(toolResultToString(block.content)).length;
- } else if (block.type === "thinking") {
- total += encode(block.thinking).length;
- }
- }
- }
-
- total += countToolSchemaTokens(
- req.tools,
- (tool) => tool.name,
- (tool) => tool.description,
- (tool) => tool.input_schema,
- );
-
- total += req.messages.length * 4;
+ total += countAnthropicRequestTokens({
+ 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 total;
}
diff --git a/src/providers/shared/count-tokens.ts b/src/providers/shared/count-tokens.ts
new file mode 100644
index 0000000..4dcf30e
--- /dev/null
+++ b/src/providers/shared/count-tokens.ts
@@ -0,0 +1,72 @@
+import { countToolSchemaTokens } from "./tool-schema.ts";
+import { normalizeContent, toolResultToString } from "../translate/anthropic-content.ts";
+import type { AnthropicRequest } from "../../anthropic/schema.ts";
+
+const IMAGE_TOKEN_ESTIMATE = 2000;
+
+export type AnthropicRequestTokenCounter = (text: string) => number;
+
+type AnthropicToolReaders<TTool> = {
+ readToolName: (tool: TTool) => string;
+ readToolDescription: (tool: TTool) => string | undefined;
+ readToolSchema: (tool: TTool) => unknown;
+};
+
+type CountAnthropicRequestTokenOptions<TTool> = {
+ req: AnthropicRequest;
+ countToken: AnthropicRequestTokenCounter;
+ tools?: TTool[];
+} & AnthropicToolReaders<TTool>;
+
+export function countAnthropicRequestTokens<TTool>(
+ options: CountAnthropicRequestTokenOptions<TTool> & {
+ includeThinking?: false;
+ },
+): number;
+
+export function countAnthropicRequestTokens<TTool>(
+ options: CountAnthropicRequestTokenOptions<TTool> & {
+ includeThinking: true;
+ },
+): number;
+
+export function countAnthropicRequestTokens<TTool>(
+ {
+ req,
+ countToken,
+ tools,
+ readToolName,
+ readToolDescription,
+ readToolSchema,
+ includeThinking = false,
+ }: CountAnthropicRequestTokenOptions<TTool> & { includeThinking?: boolean },
+): number {
+ let total = 0;
+ for (const msg of req.messages) {
+ const blocks = normalizeContent(msg.content);
+ for (const block of blocks) {
+ if (block.type === "text") {
+ total += countToken(block.text);
+ } else if (block.type === "image") {
+ total += IMAGE_TOKEN_ESTIMATE;
+ } else if (block.type === "tool_use") {
+ total += countToken(block.name);
+ total += countToken(JSON.stringify(block.input ?? {}));
+ } else if (block.type === "tool_result") {
+ total += countToken(toolResultToString(block.content));
+ } else if (includeThinking && block.type === "thinking") {
+ total += countToken(block.thinking);
+ }
+ }
+ }
+
+ total += countToolSchemaTokens(
+ tools,
+ readToolName,
+ readToolDescription,
+ readToolSchema,
+ );
+
+ total += req.messages.length * 4;
+ return total;
+}