new-api icon indicating copy to clipboard operation
new-api copied to clipboard

feat(claude): re-enable /v1/messages/count_tokens by forwarding to Anthropic channels

Open akihitohyh opened this issue 1 week ago • 0 comments

Agent: Claude Code 2.1.228 (coordination, issue text) + Codex CLI 0.169.1 (implementation, review) · claude-opus-5-5 / gpt-6.2-astra · macOS 26 host, tests on Ubuntu 20.04 amd64 · 2026-11-25 UTC

Request summary

The user audits Claude relay gateways and noticed that a gateway built on new-api returns 404 for POST /v1/messages/count_tokens. The user asked the agents to check upstream new-api, implement the endpoint, and test it on a separate Linux server. The user then chose to open this issue first and to wait for maintainer agreement before opening a pull request.

Summary

Feature request: re-enable POST /v1/messages/count_tokens by forwarding it to the Anthropic upstream instead of estimating it locally.

Commit 3a9f41ee ("fix: temp disable /messages/count_tokens", 2026-09-04) commented out the route that #7137 added three days earlier. The TODO in router/relay-router.go stops mid-sentence, so the reason is not recorded. Before I open a PR, I would like to ask:

  1. Why was the local-estimate handler disabled? Was it the accuracy of the local tokenizer, or something else?
  2. Is the following approach acceptable?
flowchart LR
    C[Client POST /v1/messages/count_tokens] --> A[TokenAuth, rate limit]
    A --> D{Distribute: Anthropic-type channel for the model?}
    D -- no --> E[404 not_found_error, Anthropic format]
    D -- yes --> U[One request to base_url/v1/messages/count_tokens]
    U --> R[2xx body returned as-is; 4xx/5xx rebuilt and masked]
  • Only channels of type Anthropic are candidates. If a model also has OpenAI or other channels, the count request still goes only to an Anthropic channel.
  • The handler applies model mapping, reasoning-suffix normalization, param override, header override, and the request plugins that /v1/messages runs.
  • The handler does not pre-consume quota, settle, or write a consume log. It does not retry, does not trigger channel auto-disable, and does not read or write channel affinity.
  • AWS Bedrock (CountTokens) and Vertex Claude (count-tokens:rawPredict) are out of scope. They can be added later as extra channel types in the same filter.

A tested branch is ready: https://github.com/QuantumNous/new-api/compare/main...akihitohyh:new-api:feat/claude-count-tokens-upstream (19 files, about +640/-120; the code was written and reviewed with AI agents). Earlier PRs on the same endpoint are #5596 (forward to Anthropic), #4926 (Bedrock only), and #4441 (local estimate). They are open and now conflict with main. #1694 was closed by the stale bot.

Evidence

  • Version / deploy / database: main at 6627f0e3f (2026-11-24), the same behavior in release v1.0.0-rc.51; binary built from source; SQLite for the end-to-end run.
  • Actual: POST /v1/messages/count_tokens falls through to NoRoute and returns 404 {"error":{"message":"Invalid URL (POST /v1/messages/count_tokens)","type":"invalid_request_error","param":"","code":""}}.
  • Expected: the gateway returns the Anthropic upstream result, for example 200 {"input_tokens":123}.
  • Reproduce: create an Anthropic channel and a token, then run curl -X POST http://127.0.0.1:3000/v1/messages/count_tokens -H 'x-api-key: sk-...' -H 'anthropic-version: 2023-06-01' -H 'content-type: application/json' -d '{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hello"}]}'.
  • Branch results on Ubuntu 20.04 with Go 1.26.10: go vet ./..., go build ./..., and make test pass. A mock-upstream run sent 25 count requests: the Anthropic count endpoint received 25, the generation endpoint and the OpenAI channel received 0. User quota, token quota, and consume logs did not change. The channel filter passes on SQLite 3.50.4, MySQL 8.0.46, and PostgreSQL 17.12.

Blast Radius

  • Impact: relay. Claude Code and Anthropic SDK clients get 404 on every count request. #4441 reports that clients then fall back to extra /v1/messages requests.
  • Frequency: every POST /v1/messages/count_tokens call.

akihitohyh avatar Nov 24 '26 17:11 akihitohyh