Skip to main content
Applies to:
  • Plan -
  • Deployment -

Summary

Goal: Access and manually log token metrics on @traced spans when using LangChain with direct-to-provider calls. Features: current_span().log(), metrics fields, SQL sandbox queries, BraintrustCallbackHandler.

Configuration steps

Step 1: Understand where token metrics are captured

Token metrics (prompt_tokens, completion_tokens, tokens) are only automatically captured on LLM-type spans — the actual model call spans created by BraintrustCallbackHandler. @traced decorator spans and chain spans do not automatically capture or roll up token usage from child spans.

Step 2: Query token metrics on LLM-type spans

Use the SQL sandbox, custom columns, or API to access span-level metrics:

Step 3: Manually log metrics to a @traced span

Use current_span().log() inside the decorated function. If LangChain returns usage data on the response object, extract and log it directly:

Step 4: Aggregate token counts across multiple LLM calls

If a single @traced span wraps multiple LLM calls, accumulate counts locally and log the total at the end:

Step 5: Check model name for estimated_cost

estimated_cost is computed at query time using metadata.model matched against Braintrust’s pricing registry.
  • Standard names like gpt-4o resolve correctly.
  • Azure OpenAI custom deployment names (e.g., my-gpt4-deployment) will not match, and estimated_cost returns null.
If your deployment name doesn’t match, tally cost manually using your token counts and per-model pricing.