Skip to content

Conversation

@waleedalzarooni
Copy link
Collaborator

Description

Implementing changes from code review of @MaxonZhao

Checklist

Go over all the following points, and put an x in all the boxes that apply.

  • [X ] I have read the CONTRIBUTION guide (required)
  • [ X] I have linked this PR to an issue using the Development section on the right sidebar or by adding Fixes #issue-number in the PR description (required)
  • [ X] I have checked if any dependencies need to be added or updated in pyproject.toml and uv lock
  • [X ] I have updated the tests accordingly (required for a bug fix or a new feature)
  • [ X] I have updated the documentation if needed:
  • [ X] I have added examples if this is a new feature

If you are unsure about any of these, don't hesitate to ask. We are here to help!

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 23, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch token-counter-edits

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.

@waleedalzarooni waleedalzarooni linked an issue Sep 23, 2025 that may be closed by this pull request
2 tasks
Copy link
Member

@Wendong-Fan Wendong-Fan left a comment

Choose a reason for hiding this comment

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

thanks @waleedalzarooni , left some comments below

Union,
)

from deprecation import deprecated # type: ignore[import-untyped]
Copy link
Member

Choose a reason for hiding this comment

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

it's not necessary to introduce a new dependency for deprecation warning

Comment on lines +311 to +315
'prompt_tokens': getattr(usage, 'prompt_tokens', 0),
'completion_tokens': getattr(
usage, 'completion_tokens', 0
),
'total_tokens': getattr(usage, 'total_tokens', 0),
Copy link
Member

Choose a reason for hiding this comment

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

would be better also record cached token

Comment on lines 476 to 504
def extract_usage_from_response(
self, response: Any
) -> Optional[Dict[str, int]]:
r"""Extract native usage data from Anthropic response.
Args:
response: Anthropic response object (Message or similar)
Returns:
Dict with keys: prompt_tokens, completion_tokens, total_tokens
None if usage data not available
"""
try:
if hasattr(response, 'usage') and response.usage is not None:
usage = response.usage
input_tokens = getattr(usage, 'input_tokens', 0)
output_tokens = getattr(usage, 'output_tokens', 0)
return {
'prompt_tokens': input_tokens,
'completion_tokens': output_tokens,
'total_tokens': input_tokens + output_tokens,
}

except Exception as e:
logger.debug(
f"Failed to extract usage from Anthropic response: {e}"
)

return None
Copy link
Member

Choose a reason for hiding this comment

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

seems duplicated, as it's already defined in base class

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This definition is required since this Anthropic specific method implements the abstract method defined earlier

Comment on lines +668 to +670
def extract_usage_from_response(
self, response: Any
) -> Optional[Dict[str, int]]:
Copy link
Member

Choose a reason for hiding this comment

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

seems duplicated, as it's already defined in base class

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Same reasoning as above

Copy link
Member

Choose a reason for hiding this comment

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

this file is not needed in example code

Copy link
Member

Choose a reason for hiding this comment

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

seems this folder is AI-generated, some modules like enable_streaming_usage_for_openai is redundant, could we rewrite the example code to make it tidy?

)


class TestOpenAITokenCounterExtractUsage:
Copy link
Member

Choose a reason for hiding this comment

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

this test code also could be polished to be more meaningful, this kind of mock and test could not do meaningful test

Copy link
Collaborator Author

@waleedalzarooni waleedalzarooni Oct 7, 2025

Choose a reason for hiding this comment

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

I wrote a new example test script with this approach

`def test_extract_basic_usage(self):
"""Test extracting basic usage data from OpenAI response."""
if not OPENAI_AVAILABLE:
pytest.skip("OpenAI not available")

    counter = OpenAITokenCounter(ModelType.GPT_4O_MINI)

    # Create mock response with spec to prevent auto-creation of attributes
    mock_response = Mock()
    mock_response.usage = Mock(spec=['prompt_tokens', 'completion_tokens', 'total_tokens'])
    mock_response.usage.prompt_tokens = 50
    mock_response.usage.completion_tokens = 25
    mock_response.usage.total_tokens = 75

    # Extract usage
    usage = counter.extract_usage_from_response(mock_response)

    # Validate
    assert usage is not None
    assert usage["prompt_tokens"] == 50
    assert usage["completion_tokens"] == 25
    assert usage["total_tokens"] == 75
    assert len(usage) == 3  # Only basic fields`

The only way to make the test more realistic would be to make actual API calls for real extraction but this resembles more of an integration test, let me know if you would prefer this!

@Wendong-Fan Wendong-Fan added the Waiting for Update PR has been reviewed, need to be updated based on review comment label Oct 5, 2025
@Wendong-Fan Wendong-Fan added this to the Sprint 39 milestone Oct 5, 2025
@Saedbhati Saedbhati self-requested a review October 6, 2025 07:48
@Wendong-Fan Wendong-Fan requested a review from fengju0213 October 6, 2025 14:47
@Wendong-Fan
Copy link
Member

Let's put this PR on hold for the time being. We've decided to move forward with a more agentic approach to memory management, and the new implementation will likely supersede the work done here @waleedalzarooni

@Wendong-Fan Wendong-Fan added Pending and removed Waiting for Update PR has been reviewed, need to be updated based on review comment labels Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[Feature Request] Deprecate current token usage calculation

5 participants