Skip to content

Conversation

@hayke102
Copy link
Contributor

@hayke102 hayke102 commented Nov 13, 2025

Summary

Fixed AttributeError by replacing GenAIAttributes with SpanAttributes for cache-related token attributes in Langchain and Anthropic instrumentation packages.

Problem

The code was incorrectly attempting to use GenAIAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS and GenAIAttributes.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, but these attributes don't exist in the upstream OpenTelemetry incubating semantic conventions.

These cache token attributes are custom extensions added by OpenLLMetry to support prompt caching features (used by Anthropic, OpenAI, etc.) and are defined in the local SpanAttributes class in opentelemetry-semantic-conventions-ai package.

Solution

Replaced GenAIAttributes with SpanAttributes for both cache token attributes:

  • GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS
  • GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS

Files Changed

  1. packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/span_utils.py

    • Line 349: Fixed cache read tokens attribute
  2. packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py

    • Lines 286, 290, 400, 404: Fixed both cache read and cache creation tokens attributes in sync and async functions

Impact

This bug would have caused AttributeError at runtime when processing responses that include cache token information (e.g., Anthropic's prompt caching, OpenAI's cached tokens).

Testing

The fix aligns with the pattern already used correctly in other instrumentation packages:

  • opentelemetry-instrumentation-openai (correctly uses SpanAttributes.LLM_USAGE_CACHE_READ_INPUT_TOKENS)
  • traceloop-sdk (correctly uses SpanAttributes for cache attributes)

🤖 Generated with Claude Code


Important

Fixes AttributeError by replacing GenAIAttributes with SpanAttributes for cache token attributes in Langchain and Anthropic packages.

  • Behavior:
    • Replaced GenAIAttributes with SpanAttributes for cache token attributes GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS and GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS.
    • Fixes AttributeError when processing cache token information in Langchain and Anthropic packages.
  • Files Changed:
    • span_utils.py in Langchain: Line 349.
    • __init__.py in Anthropic: Lines 286, 290, 400, 404.
  • Impact:
    • Prevents runtime AttributeError in cache token processing.
  • Testing:
    • Aligns with correct usage in opentelemetry-instrumentation-openai and traceloop-sdk.

This description was created by Ellipsis for 1dc9943. You can customize this summary. It will automatically update as commits are pushed.

Summary by CodeRabbit

  • Refactor
    • Standardized cache-related token-usage telemetry across OpenTelemetry instrumentation to use the unified SpanAttributes namespace, ensuring consistent observability and attribute naming for cache read/creation token metrics.

✏️ Tip: You can customize this high-level summary in your review settings.

…tributes

Fixed AttributeError by replacing GenAIAttributes with SpanAttributes for
cache-related token attributes (GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS and
GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS).

These cache token attributes are custom extensions added by OpenLLMetry to
support prompt caching features (Anthropic, OpenAI) and are not part of the
upstream OpenTelemetry incubating semantic conventions. They are defined in
the local SpanAttributes class, not in GenAIAttributes.

Changed files:
- opentelemetry-instrumentation-langchain/span_utils.py (line 349)
- opentelemetry-instrumentation-anthropic/__init__.py (lines 286, 290, 400, 404)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@CLAassistant
Copy link

CLAassistant commented Nov 13, 2025

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link

coderabbitai bot commented Nov 13, 2025

Walkthrough

This pull request replaces cache-related token attribute constants from GenAIAttributes to SpanAttributes in two instrumentation packages (Anthropic and LangChain). No control-flow or error-handling changes were made.

Changes

Cohort / File(s) Summary
Anthropic instrumentation
packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py
Replaced GenAIAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS with SpanAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS and GenAIAttributes.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS with SpanAttributes.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS in _aset_token_usage and _set_token_usage.
LangChain instrumentation
packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/span_utils.py
Replaced GenAIAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS with SpanAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS in set_chat_response.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Potential review focus:

  • Confirm correct import/source of SpanAttributes and that no other GenAIAttributes usages remain for these keys.
  • Verify unit tests or span-attribute consumers expect the new attribute names.

Possibly related PRs

Poem

🐰 I hopped through constants, small and bright,
Swapping GenAI for Span in the soft moonlight.
Tokens now rest where names align,
Cache metrics tidy, neat, and fine.
🥕✨

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Title check ⚠️ Warning The title mentions 'openai' but the PR primarily fixes cache token attribute issues in Langchain and Anthropic instrumentation, not OpenAI. Revise title to accurately reflect the packages being fixed, such as: 'fix: use SpanAttributes instead of GenAIAttributes for cache token attributes in anthropic and langchain'
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 1dc9943 and 9f48bcb.

📒 Files selected for processing (1)
  • packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/init.py

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to 1dc9943 in 58 seconds. Click for details.
  • Reviewed 45 lines of code in 2 files
  • Skipped 0 files when reviewing.
  • Skipped posting 3 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py:286
  • Draft comment:
    Replaced GenAIAttributes with SpanAttributes for cache read tokens in the async token usage block. This ensures the correct custom attribute is used.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% This comment is purely informative, explaining a change that was made without asking for confirmation or suggesting improvements. It doesn't align with the rules for good comments, as it doesn't provide a specific suggestion or ask for confirmation of intent.
2. packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py:400
  • Draft comment:
    Switched cache creation tokens attribute to use SpanAttributes in the sync token usage block to avoid AttributeError.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% This comment seems to be purely informative, explaining a change that was made. It doesn't ask for any specific action or provide a suggestion for improvement. According to the rules, purely informative comments should be removed.
3. packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/span_utils.py:346
  • Draft comment:
    Updated cache read tokens attribute to use SpanAttributes instead of GenAIAttributes, aligning with the custom semantic conventions.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% This comment is purely informative, describing a change made in the code without providing any actionable feedback or suggestions. It doesn't ask for confirmation or suggest improvements, nor does it point out any potential issues. Therefore, it violates the rule against making purely informative comments.

Workflow ID: wflow_uNTK4jwkKuzbZIl9

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

@hayke102
Copy link
Contributor Author

@coderabbitai generate docstrings

@coderabbitai
Copy link

coderabbitai bot commented Nov 13, 2025

✅ Actions performed

Initiated docstring generation; will generate only if new commits exist.

@coderabbitai
Copy link

coderabbitai bot commented Nov 13, 2025

Note

Docstrings generation - SUCCESS
Generated docstrings for this pull request at #3443

coderabbitai bot added a commit that referenced this pull request Nov 13, 2025
Docstrings generation was requested by @hayke102.

* #3442 (comment)

The following files were modified:

* `packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py`
* `packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/span_utils.py`
@nirga nirga changed the title fix: use SpanAttributes instead of GenAIAttributes for cache token attributes fix(openai): use SpanAttributes instead of GenAIAttributes for cache token attributes Nov 23, 2025
Copy link
Member

@nirga nirga left a comment

Choose a reason for hiding this comment

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

Thanks @hayke102!

@nirga nirga merged commit d9a0d7d into traceloop:main Nov 23, 2025
9 checks passed
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.

3 participants