Skip to content

Conversation

ascender1729
Copy link

@ascender1729 ascender1729 commented Oct 8, 2025

Problem

Cache token metrics are exported as objects instead of strings when zero. For example, gen_ai.usage.cache_write_input_tokens shows as {intValue: 0} instead of "0", causing issues in trace analysis tools like Langfuse.

Changes

Convert all token usage metrics to strings in tracer.py:

  • gen_ai.usage.prompt_tokens
  • gen_ai.usage.completion_tokens
  • gen_ai.usage.cache_read_input_tokens
  • gen_ai.usage.cache_write_input_tokens
  • And all other token metrics

Updated test assertions accordingly.

Testing

All 62 tracer tests pass. Verified that zero cache tokens now export as "0" (string) instead of objects.

Fixes #971

Convert all gen_ai.usage token metrics to strings to ensure
consistent export format in OpenTelemetry traces. Previously,
when cache_write_input_tokens or cache_read_input_tokens were
zero, they were exported as objects {intValue: 0} instead of
string "0", causing issues in trace analysis tools like Langfuse.

Changes:
- Convert all token metrics to strings in end_model_invoke_span
- Convert all token metrics to strings in end_agent_span
- Update test assertions to expect string values

Fixes strands-agents#971
Copy link
Contributor

@poshinchen poshinchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://opentelemetry.io/docs/specs/semconv/registry/attributes/gen-ai/#gen-ai-input-messages

These values are meant to be int though, it is the platform that decides to re-map it as string. We should keep it as int within strands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] cache read/write usage not exported as string when it is zero

2 participants