Skip to content

Added response cost in the Usage #682

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

RVV-karma
Copy link

@RVV-karma RVV-karma commented May 12, 2025

Feature: Add Response Cost Tracking to Usage Class

This PR introduces a new attribute, response_cost, to the Usage class to enable tracking of the total cost incurred from API responses. This addition enhances observability and cost management for developers using the OpenAI Agents SDK by making cost metrics available alongside existing usage metrics such as request count and token usage.

How it works:

  • A new field, response_cost: float = 0.0, is added to the Usage dataclass.
  • The add() method has been updated to correctly accumulate the response_cost from another Usage instance.
  • This allows developers to easily aggregate and monitor financial cost metrics in tandem with usage data.

Example:

usage_1 = Usage(requests=1, input_tokens=100, output_tokens=200, total_tokens=300, response_cost=0.012)
usage_2 = Usage(requests=2, input_tokens=150, output_tokens=250, total_tokens=400, response_cost=0.015)

usage_1.add(usage_2)

# usage_1 now reflects:
# requests: 3
# input_tokens: 250
# output_tokens: 450
# total_tokens: 700
# response_cost: 0.027

This addition is useful for developers needing cost-aware usage insights for budgeting, reporting, or scaling decisions.

Closes #683

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.

Add response cost in the Usage
1 participant