PRODUCTION SCENARIO
An enterprise gateway must reject prompts likely to blow a per-call token budget before paying for any generation. Prompts carry system instructions, messages, and tool definitions.
Which approach gives the closest count before the request is sent?
Answering here is anonymous. Nothing is saved unless you sign in.
Show answer and explanation
Answer: Call /v1/messages/count_tokens with the exact model and full request body
The endpoint takes the same request shape and counts the system prompt, messages, and tool definitions under the target model's tokenizer, free and with no generation. The docs call the result an estimate that can differ by a small amount, which is still far closer than the alternatives. Scaling from a sample assumes a linearity the tokenizer lacks. Tool definitions are counted natively and their system-prompt overhead varies by model and tool_choice, so no fixed number replaces them. A max_tokens of 1 bills the input and runs inference anyway.