Skip to content

Fix token usage- total tokens#45202

Open
YoYoJa wants to merge 2 commits intomainfrom
jessli/FixTotalTokens
Open

Fix token usage- total tokens#45202
YoYoJa wants to merge 2 commits intomainfrom
jessli/FixTotalTokens

Conversation

@YoYoJa
Copy link
Contributor

@YoYoJa YoYoJa commented Feb 15, 2026

Description

Please add an informative description that covers that changes made by the pull request and link all relevant issues.

If an SDK is being regenerated based on a new API spec, a link to the pull request containing these API spec changes should be included above.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

Copilot AI review requested due to automatic review settings February 15, 2026 16:02
@YoYoJa YoYoJa requested a review from a team as a code owner February 15, 2026 16:02
@github-actions github-actions bot added the Evaluation Issues related to the client library for Azure AI Evaluation label Feb 15, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request aims to fix token usage calculation in the RAI service evaluator by converting token counts from strings to integers. The PR changes how prompt_tokens, completion_tokens, and total_tokens are handled in two locations within the _parse_eval_result method.

Changes:

  • Convert token counts from strings to integers to align with codebase conventions
  • Change default token values from empty strings to 0 (integer)
  • Update total token calculation to use integers instead of string concatenation

Comment on lines 341 to 345
total_tokens = (
str(int(prompt_tokens) + int(completion_tokens))
int(prompt_tokens) + int(completion_tokens)
if prompt_tokens and completion_tokens
else ""
else 0
)
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

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

Same bug as lines 307-311. The condition on line 343 checks if both tokens are truthy, which incorrectly returns 0 when either value is 0. Since the variables are already integers, the calculation should be unconditional:

total_tokens = prompt_tokens + completion_tokens

Copilot uses AI. Check for mistakes.
Comment on lines 302 to 303
prompt_tokens = int(metrics.get("promptTokens", 0)) if metrics.get("promptTokens") else 0
completion_tokens = int(metrics.get("completionTokens", 0)) if metrics.get("completionTokens") else 0
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

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

The logic for converting prompt_tokens and completion_tokens is redundant. The code first converts to int, then checks if the value is truthy and converts to int again. Consider simplifying to:

prompt_tokens = int(metrics.get("promptTokens", 0) or 0)
completion_tokens = int(metrics.get("completionTokens", 0) or 0)

This handles both string and None values correctly while avoiding double conversion.

Suggested change
prompt_tokens = int(metrics.get("promptTokens", 0)) if metrics.get("promptTokens") else 0
completion_tokens = int(metrics.get("completionTokens", 0)) if metrics.get("completionTokens") else 0
prompt_tokens = int(metrics.get("promptTokens", 0) or 0)
completion_tokens = int(metrics.get("completionTokens", 0) or 0)

Copilot uses AI. Check for mistakes.
Comment on lines 307 to 311
total_tokens = (
str(int(prompt_tokens) + int(completion_tokens))
int(prompt_tokens) + int(completion_tokens)
if prompt_tokens and completion_tokens
else ""
else 0
)
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

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

The condition on line 309 checks if both prompt_tokens and completion_tokens are truthy, which means if either value is 0, total_tokens will be set to 0 instead of the correct sum. Since prompt_tokens and completion_tokens are already guaranteed to be integers (defaulting to 0), this condition is incorrect. The calculation should be unconditional:

total_tokens = prompt_tokens + completion_tokens

This bug causes total_tokens to be 0 when either prompt_tokens or completion_tokens is 0, even though the other might have a non-zero value.

Copilot uses AI. Check for mistakes.
Comment on lines 317 to 318
parsed_result[f"{self._eval_metric.value}_prompt_tokens"] = int(prompt_tokens) if prompt_tokens else 0
parsed_result[f"{self._eval_metric.value}_completion_tokens"] = int(completion_tokens) if completion_tokens else 0
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

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

The int() conversion here is redundant since prompt_tokens and completion_tokens are already integers from lines 302-303. The variables can be used directly:

parsed_result[f"{self._eval_metric.value}_prompt_tokens"] = prompt_tokens
parsed_result[f"{self._eval_metric.value}_completion_tokens"] = completion_tokens

Additionally, the condition if prompt_tokens else 0 is unnecessary since prompt_tokens is already guaranteed to be an integer.

Copilot uses AI. Check for mistakes.
Comment on lines 337 to 338
prompt_tokens = int(metrics.get("promptTokens", 0)) if metrics.get("promptTokens") else 0
completion_tokens = int(metrics.get("completionTokens", 0)) if metrics.get("completionTokens") else 0
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

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

Same redundancy issue as lines 302-303. Consider simplifying to:

prompt_tokens = int(metrics.get("promptTokens", 0) or 0)
completion_tokens = int(metrics.get("completionTokens", 0) or 0)
Suggested change
prompt_tokens = int(metrics.get("promptTokens", 0)) if metrics.get("promptTokens") else 0
completion_tokens = int(metrics.get("completionTokens", 0)) if metrics.get("completionTokens") else 0
prompt_tokens = int(metrics.get("promptTokens", 0) or 0)
completion_tokens = int(metrics.get("completionTokens", 0) or 0)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Evaluation Issues related to the client library for Azure AI Evaluation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments