feature about anthropic count token and fix token usage problem and update claude code settings #115
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request introduces major improvements to token counting and model compatibility for Anthropic and OpenAI-style chat and tool calls. The most significant changes include a complete rewrite of the token counting logic to support multiple model tokenizers, improved handling of tool tokens, and new endpoints for Anthropic-compatible token counting. Several translation functions now properly account for cached tokens, ensuring more accurate usage reporting.
Token counting and model compatibility improvements:
src/lib/tokenizer.ts
to support multiple GPT encoding schemes, more accurate token counting for messages and tools, and dynamic model-based constants. The new implementation allows for flexible token calculation across Anthropic and OpenAI models, including tool call and parameter support.Anthropic endpoint and translation enhancements:
/v1/messages/count_tokens
endpoint, with handlerhandleCountTokens
insrc/routes/messages/count-tokens-handler.ts
, to accurately compute input tokens for Anthropic requests, including model-specific adjustments and tool bonuses. [1] [2] [3] [4]non-stream-translation.ts
andstream-translation.ts
to subtract cached tokens from input token counts and include detailed cache token usage in the response, improving Anthropic usage reporting. [1] [2] [3]Model and payload type improvements:
Model
interface insrc/services/copilot/get-models.ts
to support new capabilities and ensure compatibility with the updated tokenizer.ChatCompletionResponse
type insrc/services/copilot/create-chat-completions.ts
to includeprompt_tokens_details
, allowing reporting of cached token usage.Configuration and setup updates:
README.md
andsrc/start.ts
to include new environment variables for model selection and non-essential traffic disabling, supporting the expanded Anthropic model options. [1] [2]