Skip to content

Conversation

@amanjaiswal73892
Copy link
Collaborator

@amanjaiswal73892 amanjaiswal73892 commented Jul 7, 2025

This pull request refines token usage tracking and cost calculation logic in the src/agentlab/llm/tracking.py file. The changes improve handling of cached tokens and introduce better differentiation between API types (chatcompletion vs. response) for more accurate cost computation.

Enhancements to token usage tracking:

  • src/agentlab/llm/tracking.py: Updated the __call__ method to extract and include cached_tokens from prompt_tokens_details or input_tokens_details in the usage dictionary, ensuring more precise tracking of cached token usage.

Improvements to cost calculation logic:

  • src/agentlab/llm/tracking.py: Refactored the get_effective_cost_from_openai_api method to handle two distinct API types (chatcompletion and response). Added logic to compute cached_input_tokens and non_cached_input_tokens separately for each type, improving the accuracy of effective cost calculations. Introduced warnings for missing usage information or unsupported API types.…ponses API

Description by Korbit AI

What change is being made?

Update cost-tracking logic for OpenAI chatcompletion and response APIs to account for token caching and improve cost calculation accuracy.

Why are these changes being made?

The change introduces handling for prompt_tokens_details and input_tokens_details to accurately assess cached tokens, addressing previous shortcomings in cost estimation by accounting for cached and non-cached token differences. This ensures more precise cost tracking and provides fallback handling for missing API usage information, enhancing robustness and logging unsupported API types to prevent miscalculations.

Is this description stale? Ask me to generate a new description by commenting /korbit-generate-pr-description

Copy link

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

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

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Status
Functionality Incomplete API type detection logic ▹ view ✅ Fix detected
Functionality Inconsistent dictionary key names causing AttributeError ▹ view ✅ Fix detected
Logging Missing Response Context in Log ▹ view
Readability Redundant Token Extraction Logic ▹ view
Files scanned
File Path Reviewed
src/agentlab/llm/tracking.py

Explore our documentation to understand the languages and file types we support and the files we ignore.

Check out our docs on how you can make Korbit work best for you and your team.

Loving Korbit!? Share us on LinkedIn Reddit and X

Comment on lines 166 to 167
if 'prompt_tokens_details' in usage:
usage['cached_tokens'] = usage['prompt_token_details'].cached_tokens

This comment was marked as resolved.

if usage is None:
logging.warning("No usage information found in the response. Defaulting cost to 0.0.")
return 0.0
api_type = 'chatcompletion' if hasattr(usage, "prompt_tokens_details") else 'response'

This comment was marked as resolved.

Comment on lines 310 to 319
if api_type == 'chatcompletion':
total_input_tokens = usage.prompt_tokens
output_tokens = usage.completion_tokens
cached_input_tokens = usage.prompt_tokens_details.cached_tokens
non_cached_input_tokens = total_input_tokens - cached_input_tokens
elif api_type == 'response':
total_input_tokens = usage.input_tokens
output_tokens = usage.output_tokens
cached_input_tokens = usage.input_tokens_details.cached_tokens
non_cached_input_tokens = total_input_tokens - cached_input_tokens
Copy link

Choose a reason for hiding this comment

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

Redundant Token Extraction Logic category Readability

Tell me more
What is the issue?

Duplicated token extraction logic with only attribute names differing between API types.

Why this matters

The duplicated structure makes the code harder to maintain and obscures the fact that both branches follow the same pattern.

Suggested change ∙ Feature Preview
TOKEN_MAPPINGS = {
    'chatcompletion': {
        'total_tokens': 'prompt_tokens',
        'output_tokens': 'completion_tokens',
        'details_attr': 'prompt_tokens_details'
    },
    'response': {
        'total_tokens': 'input_tokens',
        'output_tokens': 'output_tokens',
        'details_attr': 'input_tokens_details'
    }
}

mapping = TOKEN_MAPPINGS.get(api_type)
if mapping:
    total_input_tokens = getattr(usage, mapping['total_tokens'])
    output_tokens = getattr(usage, mapping['output_tokens'])
    details = getattr(usage, mapping['details_attr'])
    cached_input_tokens = details.cached_tokens
    non_cached_input_tokens = total_input_tokens - cached_input_tokens
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

@ServiceNow ServiceNow deleted a comment from korbit-ai bot Jul 8, 2025
@amanjaiswal73892 amanjaiswal73892 self-assigned this Jul 8, 2025
@amanjaiswal73892 amanjaiswal73892 merged commit 8523f83 into main Jul 9, 2025
5 of 6 checks passed
@amanjaiswal73892 amanjaiswal73892 deleted the enhance-cost-tracking branch July 9, 2025 22:27
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