Skip to content

⚡️ Speed up method Usage.__add__ by 290% #21

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 1 commit into
base: try-refinement
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions pydantic_ai_slim/pydantic_ai/usage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations as _annotations

from copy import copy
from dataclasses import dataclass

from . import _utils
Expand Down Expand Up @@ -51,9 +50,24 @@ def __add__(self, other: Usage) -> Usage:

This is provided so it's trivial to sum usage information from multiple requests and runs.
"""
new_usage = copy(self)
new_usage.incr(other)
return new_usage
new_details = None

if self.details and other.details:
new_details = self.details.copy()
for k, v in other.details.items():
new_details[k] = new_details.get(k, 0) + v
elif self.details:
new_details = self.details.copy()
elif other.details:
new_details = other.details.copy()

return Usage(
requests=(self.requests or 0) + (other.requests or 0),
request_tokens=(self.request_tokens or 0) + (other.request_tokens or 0) if (self.request_tokens is not None or other.request_tokens is not None) else None,
response_tokens=(self.response_tokens or 0) + (other.response_tokens or 0) if (self.response_tokens is not None or other.response_tokens is not None) else None,
total_tokens=(self.total_tokens or 0) + (other.total_tokens or 0) if (self.total_tokens is not None or other.total_tokens is not None) else None,
details=new_details
)

def opentelemetry_attributes(self) -> dict[str, int]:
"""Get the token limits as OpenTelemetry attributes."""
Expand Down
Loading