Skip to content

fix(messages): add Anthropic count tokens endpoint#612

Merged
icebear0828 merged 1 commit into
icebear0828:devfrom
hangox:fix/anthropic-count-tokens
May 31, 2026
Merged

fix(messages): add Anthropic count tokens endpoint#612
icebear0828 merged 1 commit into
icebear0828:devfrom
hangox:fix/anthropic-count-tokens

Conversation

@hangox
Copy link
Copy Markdown
Contributor

@hangox hangox commented May 31, 2026

Summary

  • add POST /v1/messages/count_tokens for Anthropic-compatible clients
  • validate the count-tokens request shape and return { input_tokens }
  • keep the endpoint local-only so it does not require an authenticated Codex account or an upstream request
  • reuse the configured proxy API key check when server.proxy_api_key is set

Why

Claude Code calls Anthropic SDK beta.messages.countTokens(...), which maps to /v1/messages/count_tokens?beta=true. Without this endpoint, /context token accounting can fall back to inaccurate estimates and significantly over-report categories such as custom agents.

Testing

  • npm test -- tests/e2e/messages.test.ts tests/unit/routes/messages-count-tokens.test.ts
  • npx tsc --noEmit
  • npm run build

@icebear0828 icebear0828 merged commit ccffbb2 into icebear0828:dev May 31, 2026
3 checks 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.

2 participants