Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement API Cost Estimation and Logging for Agent Interactions (Fixes #66) #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

minimalProviderAgentMarket
Copy link
Contributor

Pull Request Description

Overview

This pull request introduces a new feature to estimate the cost of each API interaction and log it, as outlined in Issue #66: Estimate cost of each API interaction and log it. The modifications include the creation of a cost tracking module, updates to existing functionalities, and integrated logging capabilities.

Summary of Changes

  1. New Module - cost_tracker.py:

    • A new utility module named cost_tracker.py has been added to the utils directory. This module includes:
      • CostTracker class: Responsible for tracking API costs associated with different agent types.
      • Pricing Information: Contains pricing structures for different models, facilitating accurate cost estimation.
      • Logging Functionality: Automatically records API costs into an api_costs.log file, ensuring all interactions are logged with timestamps and cost breakdowns.
  2. Modifications to agent_market.py:

    • Enhanced the existing functions get_pr_title and get_pr_body to incorporate cost tracking for OpenAI API calls.
    • Included logging that captures the input and output token usage, ensuring transparency in API interactions.
  3. Updates to utils/__init__.py:

    • Integrated the cost_tracker instance into the module exports.
    • Updated the __all__ list to include cost_tracker, ensuring accessibility across other modules.

Implementation Outcome

This implementation effectively addresses the requirements set out in Issue #66 by providing a robust cost tracking and logging system for API interactions. The new functionality:

  • Accurately estimates and logs costs associated with each API call.
  • Supports multiple model pricing, allowing for future scalability and adaptability.
  • Generates detailed logs that can be crucial for monitoring and analysis purposes.

With this enhancement, the project now offers better financial transparency in API interactions, supporting both developers and users in understanding and managing their API usage costs.

Closing Notes

Please review the changes and provide feedback or suggestions for further improvements. This pull request aims to enhance the overall functionality and maintainability of the project.

Fixes #66

Implement a CostTracker class to monitor and log API usage costs:
- Create new CostTracker class in cost_tracker.py
- Add pricing data for OpenAI and Bedrock models
- Track token usage and calculate costs in USD
- Log API costs to api_costs.log file
- Integrate cost tracking in agent_market.py functions
- Export cost_tracker instance in utils/__init__.py

The system logs each API call with:
- Model name
- Input/output token counts
- Total cost in USD
- Timestamp
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.

Estimate cost of each API interaction and log it
1 participant