Skip to content

Conversation

@habema
Copy link
Contributor

@habema habema commented Oct 27, 2025

Resolves #1994

Changes:

  • Added RequestUsage class to store per-request details
  • Added request_usage_entries field to Usage class
  • Modified Usage.add() to automatically create per-request entries for single requests

Sample Usage object after changes:

Usage(
    requests=2,
    input_tokens=158,
    input_tokens_details=InputTokensDetails(cached_tokens=0),
    output_tokens=98,
    output_tokens_details=OutputTokensDetails(reasoning_tokens=0),
    total_tokens=256,
    request_usage_entries=[
        RequestUsage(
            input_tokens=66,
            output_tokens=14,
            total_tokens=80,
            input_tokens_details=InputTokensDetails(cached_tokens=0),
            output_tokens_details=OutputTokensDetails(reasoning_tokens=0),
        ),
        RequestUsage(
            input_tokens=92,
            output_tokens=84,
            total_tokens=176,
            input_tokens_details=InputTokensDetails(cached_tokens=0),
            output_tokens_details=OutputTokensDetails(reasoning_tokens=0),
        ),
    ],
)

@seratch seratch requested a review from rm-openai October 27, 2025 12:21
@ihower
Copy link
Contributor

ihower commented Oct 27, 2025

@habema 👍 for this small useful feature. I often need the token usage of the last request to estimate how much of the context window is being consumed, rather than looking at the total sum.

By the way, the naming in your PR description doesn’t match the code:

  • Class name: PR says IndividualRequestUsage, but the code uses RequestUsage
  • Attribute name: PR says individual_requests, but the code uses request_usage_entries

Personally, I feel IndividualRequestUsage and request_usage_entries are a bit verbose. I suggest using RequestUsage (class name) and request_usages (attribute name), clear and simple enough.

@habema
Copy link
Contributor Author

habema commented Oct 27, 2025

@ihower thank you. Those mismatches are because of the code review, directly above your comment.

@seratch seratch changed the title Feat: Add individual request usage tracking feat: #1994 Add a list of per-request usage data to Usage Oct 28, 2025
@seratch
Copy link
Member

seratch commented Oct 28, 2025

@rm-openai Let me know if you have any comments. I will merge this within a few business days.

@seratch seratch mentioned this pull request Oct 30, 2025
@seratch
Copy link
Member

seratch commented Oct 31, 2025

@ihower Thanks for sharing your thoughts.

Personally, I feel IndividualRequestUsage and request_usage_entries are a bit verbose. I suggest using RequestUsage (class name) and request_usages (attribute name), clear and simple enough.

request_usages was actually one of the name candidates on my end, but ChatGPT pointed out that it might sound a bit unnatural in English LOL. So, I leaned toward using request_usage_xxx (like entries, list, etc.) instead.

@seratch seratch merged commit 3b9368d into openai:main Oct 31, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a list of per-request usage data to Usage

3 participants