Skip to content

⚡️ Speed up method UsageLimits.has_token_limits by 387% #22

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
11 changes: 7 additions & 4 deletions pydantic_ai_slim/pydantic_ai/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ def has_token_limits(self) -> bool:
This is useful because if we have token limits, we need to check them after receiving each streamed message.
If there are no limits, we can skip that processing in the streaming response iterator.
"""
return any(
limit is not None
for limit in (self.request_tokens_limit, self.response_tokens_limit, self.total_tokens_limit)
)
if self.request_tokens_limit is not None:
return True
if self.response_tokens_limit is not None:
return True
if self.total_tokens_limit is not None:
return True
return False

def check_before_request(self, usage: Usage) -> None:
"""Raises a `UsageLimitExceeded` exception if the next request would exceed the request_limit."""
Expand Down
Loading