-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Summary
The Anthropic Python SDK exposes client.messages.count_tokens() (and client.beta.messages.count_tokens()), a stable GA endpoint that returns the input token count for a given set of messages, tools, and system prompt. The Braintrust Anthropic integration does not wrap this method, so calls to it produce no tracing spans.
What's missing
POST /v1/messages/count_tokens accepts the same parameters as the Messages API (messages, model, system, tools, tool_choice, thinking) and returns {"input_tokens": <int>}. It is useful for:
- Pre-flight cost estimation before sending expensive requests
- Prompt optimization workflows where users iterate on prompt length
- Budget enforcement in production pipelines
The endpoint is free to use (subject to rate limits) and is stable/GA. Instrumenting it would create a span capturing the model, input parameters, and returned token count — giving users visibility into their token counting calls alongside their generation calls.
The current Messages and AsyncMessages wrapper classes in tracing.py only wrap create() and stream(). The count_tokens() method falls through to the unwrapped SDK via Wrapper.__getattr__.
Braintrust docs status
The Anthropic integration page does not mention count_tokens(). Status: not_found.
Upstream sources
- Anthropic API — Count tokens documents the stable
POST /v1/messages/count_tokensendpoint - Anthropic Python SDK exposes
client.messages.count_tokens()andclient.beta.messages.count_tokens()
Local files inspected
py/src/braintrust/integrations/anthropic/tracing.py—Messagesclass (lines ~140–180) wrapscreate()andstream()only;AsyncMessagesclass (lines ~60–125) same; nocount_tokensmethod in eitherpy/src/braintrust/integrations/anthropic/_utils.py— no reference tocount_tokenspy/src/braintrust/integrations/anthropic/test_anthropic.py— no tests forcount_tokens()