Skip to content

[BOT ISSUE] Anthropic server_tool_use usage metrics silently dropped from span metrics #1728

@braintrust-bot

Description

@braintrust-bot

Summary

When Anthropic server-side tools (e.g., web search) are used, the API response includes a server_tool_use object in the usage field containing metrics like web_search_requests. The Anthropic instrumentation plugin's parseMetricsFromUsage function uses an explicit allowlist of four numeric fields and silently drops server_tool_use — both because it is not listed and because it is a nested object, not a number.

What instrumentation is missing

Metric extraction (js/src/instrumentation/plugins/anthropic-plugin.ts, lines 106–128): parseMetricsFromUsage explicitly enumerates exactly four fields:

saveIfExistsTo("input_tokens", "prompt_tokens");
saveIfExistsTo("output_tokens", "completion_tokens");
saveIfExistsTo("cache_read_input_tokens", "prompt_cached_tokens");
saveIfExistsTo("cache_creation_input_tokens", "prompt_cache_creation_tokens");

The saveIfExistsTo helper (line 117) also has a typeof value === "number" guard, which would reject server_tool_use even if it were accessed, since its value is a nested object like { "web_search_requests": 3 }.

Vendor SDK types (js/src/vendor-sdk-types/anthropic.ts, lines 101–106): The AnthropicUsage type only declares input_tokens, output_tokens, cache_read_input_tokens, and cache_creation_input_tokens. There is no server_tool_use field and no index signature for pass-through of unknown fields.

Impact

Users who enable Anthropic server-side tools (web search, code execution) cannot see server tool usage metrics in their Braintrust spans. This makes it impossible to track web search request counts or other server tool usage for cost analysis or debugging.

Braintrust docs status

not_found — The Braintrust docs at https://www.braintrust.dev/docs/instrument/wrap-providers do not mention Anthropic server tool usage metrics.

Upstream reference

Local files inspected

  • js/src/instrumentation/plugins/anthropic-plugin.ts — lines 106–128: parseMetricsFromUsage function
  • js/src/vendor-sdk-types/anthropic.ts — lines 101–106: AnthropicUsage type
  • js/src/instrumentation/plugins/anthropic-channels.ts — channel definitions
  • js/src/wrappers/anthropic.ts — wrapper proxy

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions