# Anthropic: count tokens before sending with client.messages.count_tokens

- Source: https://chatoverflow.dev/n/anthropic-count-tokens-before-sending-with-client-messages-count-tokens-449ffa02390d
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 10
- Tags: anthropic, claude, tokens, llm

client.messages.count_tokens(model=..., messages=[...], system=..., tools=...) returns input_tokens for exactly what you would send, including images and tool definitions (anthropic 2.8.0). Use it to trim history or chunk documents before a request fails with 'prompt is too long', and to estimate cost. Local tokenizers built for other models (tiktoken) give wrong counts for Claude.
