Skip to content

feat: add /v1/messages/count_tokens endpoint for Anthropic API#103

Open
juslintek wants to merge 1 commit intojwadow:mainfrom
juslintek:feat/count-tokens-endpoint
Open

feat: add /v1/messages/count_tokens endpoint for Anthropic API#103
juslintek wants to merge 1 commit intojwadow:mainfrom
juslintek:feat/count-tokens-endpoint

Conversation

@juslintek
Copy link
Copy Markdown

Problem

Claude Code calls /v1/messages/count_tokens before each request to check conversation size and decide whether to trigger compaction. Without it, the gateway returns 404, Claude Code cannot estimate context usage, and long conversations eventually hit the upstream CONTENT_LENGTH_EXCEEDS_THRESHOLD error (400).

Solution

Add the endpoint that builds the full Kiro payload and counts tokens on the serialized JSON using tiktoken, consistent with the token counting approach used in the messages endpoint.

Depends on

@cla-bot
Copy link
Copy Markdown

cla-bot bot commented Mar 20, 2026

Thanks for the PR! 🎉

Before merge, we need a one-time CLA confirmation.
It confirms that you have the right to contribute this code and allow the project to use it.

Full CLA text:
https://github.com/jwadow/kiro-gateway/blob/main/CLA.md

Please reply once with:

I have read the CLA and I accept its terms

You need to write once, all further messages from me can be ignored.

Claude Code calls this endpoint before each request to check conversation
size and decide whether to trigger compaction. Without it, the gateway
returns 404, Claude Code cannot estimate context usage, and long
conversations eventually hit the upstream CONTENT_LENGTH_EXCEEDS_THRESHOLD
error (400).

The endpoint builds the full Kiro payload and counts tokens on the
serialized JSON using tiktoken, consistent with the token counting
approach used in the messages endpoint.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@juslintek juslintek force-pushed the feat/count-tokens-endpoint branch from 5587e2f to 3b3c190 Compare March 21, 2026 01:45
@cla-bot
Copy link
Copy Markdown

cla-bot bot commented Mar 21, 2026

Thanks for the PR! 🎉

Before merge, we need a one-time CLA confirmation.
It confirms that you have the right to contribute this code and allow the project to use it.

Full CLA text:
https://github.com/jwadow/kiro-gateway/blob/main/CLA.md

Please reply once with:

I have read the CLA and I accept its terms

You need to write once, all further messages from me can be ignored.

@juslintek
Copy link
Copy Markdown
Author

I have read the CLA and I accept its terms

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