Skip to content

[bot] Anthropic messages tracer does not instrument the count_tokens endpoint #138

@braintrust-bot

Description

@braintrust-bot

What instrumentation is missing

client.Messages.CountTokens() calls POST /v1/messages/count_tokens. The current anthropicRouter in trace/contrib/anthropic/traceanthropic.go only matches the exact path /v1/messages:

func anthropicRouter(cfg *middlewareConfig, path string) internal.MiddlewareTracer {
    if path == "/v1/messages" {
        return newMessagesTracer(cfg)
    }
    return nil
}

/v1/messages/count_tokens does not match, so every CountTokens() call is silently untraced — no span, no token metrics, no input metadata.

The endpoint accepts the same parameters as messages.New() (model, messages, system, tools, thinking, etc.) and returns {input_tokens, cache_creation_input_tokens, cache_read_input_tokens}. These map directly to the prompt_tokens, prompt_cache_creation_tokens, and prompt_cached_tokens metrics already parsed by parseUsageTokens() in trace/contrib/anthropic/traceanthropic.go. A dedicated countTokensTracer could emit a span with input metadata and token metrics but no output, analogous to how the embeddings tracer in trace/contrib/openai/embeddings.go omits the vector body.

Braintrust docs status

supported — Braintrust LLM spans already capture model, input, and token metrics for messages.New(); the count_tokens response provides the same token fields and can be recorded with the same span schema.

Source: https://www.braintrust.dev/docs

Upstream sources

  • Anthropic API reference: https://docs.anthropic.com/en/api/messages-count-tokens
  • anthropic-sdk-go method: client.Messages.CountTokens(ctx, anthropic.MessageCountTokensParams{})
  • SDK version in use: github.com/anthropics/anthropic-sdk-go v1.23.0 (trace/contrib/anthropic/go.mod)

Local repo files inspected

  • trace/contrib/anthropic/traceanthropic.goanthropicRouter (lines 104–110), parseUsageTokens
  • trace/contrib/anthropic/messages.go — existing messagesTracer
  • trace/contrib/anthropic/go.mod — SDK version
  • trace/contrib/openai/embeddings.go — pattern reference for token-only spans

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions