Skip to content

Feature/count token#8

Merged
cuipengfei merged 5 commits into
cuipengfei:devfrom
caozhiyuan:feature/count-token
Sep 28, 2025
Merged

Feature/count token#8
cuipengfei merged 5 commits into
cuipengfei:devfrom
caozhiyuan:feature/count-token

Conversation

@cuipengfei

Copy link
Copy Markdown
Owner

No description provided.

caozhiyuan and others added 5 commits September 24, 2025 16:03
- Add new command line option `--claude-code-env` to generate environment variables for Claude Code
- Update `runServer` function to use new `claudeCodeEnv` option
- Add `claude-code-env` to `start` command options in README.md
…ge assignment in translateChunkToAnthropicEvents message_start
Copilot AI review requested due to automatic review settings September 28, 2025 03:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request implements a comprehensive token counting feature for the Anthropic API translation layer, including adding cached token support and creating a new count_tokens endpoint.

Key changes:

  • Added a new /v1/messages/count_tokens endpoint handler for token counting
  • Replaced the simple tokenizer with a comprehensive token calculation system supporting multiple encoders
  • Enhanced usage tracking with cached token support in response translations

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/routes/messages/count-tokens-handler.ts New handler implementing token counting logic with model-specific adjustments
src/lib/tokenizer.ts Complete rewrite from simple counting to comprehensive multi-encoder token calculation
src/routes/messages/route.ts Added count_tokens endpoint routing
src/routes/messages/non-stream-translation.ts Enhanced usage object with cached token support
src/routes/messages/stream-translation.ts Updated streaming response to handle cached tokens
src/routes/chat-completions/handler.ts Updated to use new async token counting with model context
src/services/copilot/create-chat-completions.ts Added prompt_tokens_details interface for cached tokens
src/start.ts Added claude-code-env CLI option
src/routes/messages/anthropic-types.ts Made usage field optional in response type
tests/anthropic-response.test.ts Updated test to handle optional usage field

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +52 to +57
let finalTokenCount = tokenCount.input + tokenCount.output
if (anthropicPayload.model.startsWith("claude")) {
finalTokenCount = Math.round(finalTokenCount * 1.15)
} else if (anthropicPayload.model.startsWith("grok")) {
finalTokenCount = Math.round(finalTokenCount * 1.03)
}

Copilot AI Sep 28, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic numbers 1.15 and 1.03 should be extracted as named constants with comments explaining their purpose (e.g., model-specific token estimation adjustments).

Copilot uses AI. Check for mistakes.
Comment thread src/lib/tokenizer.ts
Comment on lines +291 to +292
if (
typeof func.parameters === "object" // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition

Copilot AI Sep 28, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The eslint disable comment is placed incorrectly and creates confusing formatting. Move it to the line above the condition or use a block comment format.

Suggested change
if (
typeof func.parameters === "object" // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (
typeof func.parameters === "object"

Copilot uses AI. Check for mistakes.
Comment on lines +43 to +48
if (anthropicPayload.model.startsWith("claude")) {
// https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/overview#pricing
tokenCount.input = tokenCount.input + 346
} else if (anthropicPayload.model.startsWith("grok")) {
tokenCount.input = tokenCount.input + 480
}

Copilot AI Sep 28, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic numbers 346 and 480 should be extracted as named constants with clear comments explaining they represent tool usage token overhead for different model families.

Copilot uses AI. Check for mistakes.
@cuipengfei cuipengfei merged commit d9d9445 into cuipengfei:dev Sep 28, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants