Estimate Input Tokens (Count Tokens)
/v1/messages/count_tokensEstimate the number of input tokens for a Nex N2.6 Messages request. This endpoint returns an estimate for input tokens only. Read actual generation usage from usage in the generation response.
Send the x-api-key: <API Key>, anthropic-version: 2023-06-01, and Content-Type: application/json headers.
Production HTTP requests use https://api.mosi.cn. Before running the examples below, set MOSS_API_ORIGIN to https://api.mosi.cn and MOSS_API_KEY to your MOSS API Key.
Request parameters
Use nex-n2.6.
Pass the text-only user messages to estimate.
For a text-only user message, set this field to user.
Text to estimate.
Response fields
Estimated input token count for this request.
Error handling and compatibility
Call the counting endpoint with the same model and the messages you plan to send. Once you have input_tokens, you can use it to reserve output room for the subsequent generation, or to trim the history first when the input is too long. For the actual generation, record usage from the usage returned by Messages.