| 1 | package Langertha::RateLimit; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Rate limit information from API response headers | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 179 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 179 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 179 | ▶ | use Langertha::Moment; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 179 | has requests_limit => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | isa => 'Maybe[Num]', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | default => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 179 | has requests_remaining => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | isa => 'Maybe[Num]', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | default => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 179 | has requests_reset => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | default => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 179 | has tokens_limit => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | isa => 'Maybe[Num]', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | default => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 179 | has tokens_remaining => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | isa => 'Maybe[Num]', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | default => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 179 | has tokens_reset => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | default => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | has received => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | isa => 'Langertha::Moment', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 25 | ▶ | default => sub { Langertha::Moment->now_utc }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 179 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 179 | has requests_reset_at => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | isa => 'Maybe[Langertha::Moment]', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | builder => '_build_requests_reset_at', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | predicate => 'has_requests_reset_at', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 179 | has requests_reset_after => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | isa => 'Maybe[Num]', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | builder => '_build_requests_reset_after', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | predicate => 'has_requests_reset_after', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 179 | has tokens_reset_at => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | isa => 'Maybe[Langertha::Moment]', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | builder => '_build_tokens_reset_at', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | predicate => 'has_tokens_reset_at', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 179 | has tokens_reset_after => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | isa => 'Maybe[Num]', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | builder => '_build_tokens_reset_after', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | predicate => 'has_tokens_reset_after', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 179 | has retry_after => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | isa => 'Maybe[Num]', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | builder => '_build_retry_after', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | predicate => 'has_retry_after', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | has raw => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | isa => 'HashRef', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 12 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 179 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # Lazy derivation of the missing half of a reset bucket. A bucket is populated | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # with exactly the shape the wire spoke (a duration on the OpenAI family, an | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # instant on Anthropic); the other half is derived here against `received`, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # only on demand, and only when the wire actually spoke one of the two. When | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | # neither was sent both stay undef — the expected path for the many providers | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # that send no reset header. The sibling *predicate* is the guard that keeps a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # never-spoken bucket from triggering the other builder (no mutual recursion), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # and the `defined` guards keep it correct regardless of access order. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 5 | ▶ | sub _reset_at_from { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reset_at_from: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 5 | my ( $self, $after ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 5 | ▶ | return undef unless defined $after; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 4 | my $whole = int $after; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 4 | my $nanos = int( ( $after - $whole ) * 1_000_000_000 + 0.5 ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 4 | return $self->received->plus_seconds($whole)->plus_nanoseconds($nanos); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 20 | ▶ | sub _reset_after_from { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reset_after_from: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 20 | my ( $self, $at ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 20 | ▶ | return undef unless defined $at; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 3 | return $self->received->delta_nanoseconds($at) / 1_000_000_000; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 12 | ▶ | sub _build_requests_reset_at { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_requests_reset_at: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 12 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 12 | ▶ | return undef unless $self->has_requests_reset_after; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 3 | return $self->_reset_at_from( $self->requests_reset_after ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 11 | ▶ | sub _build_requests_reset_after { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_requests_reset_after: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 11 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 11 | ▶ | return undef unless $self->has_requests_reset_at; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 10 | return $self->_reset_after_from( $self->requests_reset_at ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 17 | ▶ | sub _build_tokens_reset_at { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_tokens_reset_at: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 17 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 17 | ▶ | return undef unless $self->has_tokens_reset_after; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 2 | return $self->_reset_at_from( $self->tokens_reset_after ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 10 | ▶ | sub _build_tokens_reset_after { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_tokens_reset_after: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 10 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 10 | ▶ | return undef unless $self->has_tokens_reset_at; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 10 | return $self->_reset_after_from( $self->tokens_reset_at ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 49 | ▶ | sub _build_retry_after { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_retry_after: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 49 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 49 | return _resolve_retry_after( $self->raw, $self->received ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | # retry-after-ms (Azure OpenAI; openai-python reads it first) is the more | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | # precise answer and wins; Retry-After otherwise (karr k312). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 154 | ▶ | sub _resolve_retry_after { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_resolve_retry_after: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 154 | my ( $raw, $received ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 154 | my $ms = $raw->{'retry-after-ms'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 154 | ▶ | if ( defined $ms ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 33 | ( my $trimmed = $ms ) =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 33 | ▶ | return $trimmed / 1000 if $trimmed =~ /\A[0-9]+(?:\.[0-9]+)?\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 122 | return _parse_retry_after( $raw->{'retry-after'}, $received ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 122 | ▶ | sub _parse_retry_after { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_retry_after: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 122 | my ( $value, $received ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 122 | ▶ | return undef unless defined $value; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 43 | ( my $trimmed = $value ) =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 43 | ▶ | return $trimmed + 0 if $trimmed =~ /\A[0-9]+(?:\.[0-9]+)?\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 5 | require HTTP::Date; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 5 | my $epoch = HTTP::Date::str2time($trimmed); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 5 | ▶ | return undef unless defined $epoch; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 4 | ▶ | $received //= Langertha::Moment->now_utc; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$received //= "Langertha::Moment"->now_utc
MC/DC0%0/2
$received //= "Langertha::Moment"->now_utc
$received
"Langertha::Moment"->now_utc
Truth table66%2/3
A //= B
A= $receivedB= "Langertha::Moment"->now_utc
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 4 | my $wait = $epoch - ( $received->epoch + $received->nanosecond / 1_000_000_000 ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 4 | ▶ | return $wait > 0 ? $wait : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # Go time.Duration.String() unit table, in seconds. Go emits compound | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | # ("2m59.56s", "6m0s", "1h2m3s"), sub-second ("250ms", "35ms"), and fractional | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # ("7.66s") forms; the micro unit is written "µs" (U+00B5) but "us" is accepted. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 179 | my %go_duration_seconds = ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | ns => 1e-9, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | us => 1e-6, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | ms => 1e-3, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | s => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | m => 60, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | h => 3600, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 46 | ▶ | sub _parse_go_duration { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_go_duration: called CC=7 SCAR=19.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 46 | my ( $string ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 46 | ▶ | return undef unless defined $string; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | # Whole-string shape check: one or more <number><unit> segments, nothing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | # else. A bare number, an RFC 3339 stamp, or an epoch is rejected outright — | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | # this parser never guesses at a shape that is not a Go duration. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 45 | ▶ | unless $string =~ /\A-?(?:[0-9]+(?:\.[0-9]+)?(?:ns|us|\x{b5}s|\x{3bc}s|ms|s|m|h))+\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 41 | ▶ | my $sign = $string =~ /\A-/ ? -1 : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 41 | my $total = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 41 | ▶ | while ( $string =~ /([0-9]+(?:\.[0-9]+)?)(ns|us|\x{b5}s|\x{3bc}s|ms|s|m|h)/g ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 45 | my ( $num, $unit ) = ( $1, $2 ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 45 | ▶ | $unit = 'us' if $unit eq "\x{b5}s" or $unit eq "\x{3bc}s"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$unit eq "\265s" or $unit eq "\x{3bc}s"
MC/DC0%0/2
$unit eq "\265s" or $unit eq "\x{3bc}s"
$unit eq "\265s"
$unit eq "\x{3bc}s"
Truth table33%1/3
A or B
A= $unit eq "\265s"B= $unit eq "\x{3bc}s"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 45 | $total += $num * $go_duration_seconds{$unit}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 41 | return $sign * $total; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 1489 | ▶ | sub _collect_headers { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_collect_headers: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 1489 | my ( $http_response ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 1489 | my %raw; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 1489 | my $headers = $http_response->headers; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 1489 | for my $name ( $headers->header_field_names ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | next | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 3516 | ▶ | unless $name =~ /\A(?:x-ratelimit-|anthropic-ratelimit-|anthropic-priority-|anthropic-fast-|ratelimitbysize-)/i | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$name =~ /\A(?:x-ratelimit-|anthropic-ratelimit-|anthropic-priority-|anthropic-fast-|ratelimitbysize-)/i or lc($name) =~ /\Aretry-after(?:-ms)?\z/
MC/DC100%2/2
$name =~ /\A(?:x-ratelimit-|anthropic-ratelimit-|anthropic-priority-|anthropic-fast-|ratelimitbysize-)/i or lc($name) =~ /\Aretry-after(?:-ms)?\z/
$name =~ /\A(?:x-ratelimit-|anthropic-ratelimit-|anthropic-priority-|anthropic-fast-|ratelimitbysize-)/i
lc($name) =~ /\Aretry-after(?:-ms)?\z/
Truth table100%3/3
A or B
A= $name =~ /\A(?:x-ratelimit-|anthropic-ratelimit-|anthropic-priority-|anthropic-fast-|ratelimitbysize-)/iB= lc($name) =~ /\Aretry-after(?:-ms)?\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | or lc($name) =~ /\Aretry-after(?:-ms)?\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 375 | my $val = $http_response->header($name); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 375 | ▶ | $raw{ lc $name } = $val if defined $val; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 1489 | return %raw; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 12 | ▶ | sub to_hash { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_hash: called CC=12 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 12 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | # The *_reset_at Moments are numified to a plain epoch number, exactly as | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | # Langertha::Response emits `created` (0+ overload) — to_hash returns plain | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | # scalars, not value objects. `received` is the derivation anchor, not a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | # rate-limit field, and is reachable via its accessor; it stays out of the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | # serialized view so it does not appear in every trace of a rate-limited | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | # response. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 12 | ▶ | ( defined $self->requests_limit ? ( requests_limit => $self->requests_limit ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 11 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | ( defined $self->requests_remaining ? ( requests_remaining => $self->requests_remaining ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | ( defined $self->requests_reset ? ( requests_reset => $self->requests_reset ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | ( defined $self->requests_reset_at ? ( requests_reset_at => 0 + $self->requests_reset_at ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | ( defined $self->requests_reset_after ? ( requests_reset_after => $self->requests_reset_after ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | ( defined $self->tokens_limit ? ( tokens_limit => $self->tokens_limit ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | ( defined $self->tokens_remaining ? ( tokens_remaining => $self->tokens_remaining ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | ( defined $self->tokens_reset ? ( tokens_reset => $self->tokens_reset ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | ( defined $self->tokens_reset_at ? ( tokens_reset_at => 0 + $self->tokens_reset_at ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | ( defined $self->tokens_reset_after ? ( tokens_reset_after => $self->tokens_reset_after ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | ( defined $self->retry_after ? ( retry_after => $self->retry_after ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | raw => $self->raw, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 4 | ▶ | sub TO_JSON { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 4 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 4 | my $hash = $self->to_hash; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 4 | delete $hash->{raw}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 4 | return $hash; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 179 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 179 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | Langertha::RateLimit - Rate limit information from API response headers | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | version 0.503 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | my $response = $engine->simple_chat('Hello'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | if ($response->has_rate_limit) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | my $rl = $response->rate_limit; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | say "Requests remaining: ", $rl->requests_remaining // 'unknown'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | say "Tokens remaining: ", $rl->tokens_remaining // 'unknown'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | say "Reset in: ", $rl->requests_reset // 'unknown', " seconds"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | # Access raw provider-specific headers | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | my $raw = $response->rate_limit->raw; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # Also available on the engine (always reflects latest response) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | if ($engine->has_rate_limit) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | say "Engine requests remaining: ", $engine->rate_limit->requests_remaining; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | Normalized rate limit data extracted from HTTP response headers. Different | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | providers use different header naming conventions; this class provides a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | unified interface. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | B<Supported providers:> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | =item * OpenAI, Groq, Cerebras, OpenRouter, Replicate, HuggingFace (C<x-ratelimit-*>) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | =item * Anthropic (C<anthropic-ratelimit-*>) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | Engines that do not return rate limit headers (DeepSeek, Ollama, vLLM, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | LlamaCpp, etc.) will not have a rate_limit set. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | B<Reset timing.> Providers spell the reset of a bucket in incompatible ways: the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | OpenAI family sends a duration (a Go C<time.Duration> string), Anthropic sends | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | an instant (RFC 3339), and many send nothing. Rather than leave a single | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | untyped field holding whichever shape arrived, each bucket exposes both a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | typed instant — L</requests_reset_at> / L</tokens_reset_at> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | (L<Langertha::Moment>, I<when>) — and a typed duration — | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | L</requests_reset_after> / L</tokens_reset_after> (seconds, I<in how long>). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | The parser fills whichever half the wire actually spoke; the other is derived | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | lazily against L</received>. Both stay C<undef> for a bucket no header | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | described. The verbatim header strings remain available, untyped, as | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | L</requests_reset> / L</tokens_reset>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | =head2 requests_limit | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | Maximum number of requests allowed in the current window. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | =head2 requests_remaining | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | Number of requests remaining in the current window. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | =head2 requests_reset | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | The B<verbatim> C<*-reset-requests> / C<requests-reset> header string, exactly | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | as the provider sent it. Its shape is provider-dependent and untyped: OpenAI | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | and the OpenAI-compatible family send a Go C<time.Duration> string (C<"6m0s">, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | C<"2m59.56s">, C<"250ms">), Anthropic sends an RFC 3339 instant, and most | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | providers send nothing. A consumer cannot tell which kind it holds without | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | knowing the provider. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | Kept for back-compatibility. Prefer the typed pair L</requests_reset_at> (WHEN, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | a L<Langertha::Moment>) and L</requests_reset_after> (IN HOW LONG, seconds), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | which normalize this string and derive the missing half against L</received>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | =head2 tokens_limit | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | Maximum number of tokens allowed in the current window. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | =head2 tokens_remaining | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | Number of tokens remaining in the current window. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | =head2 tokens_reset | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | The B<verbatim> C<*-reset-tokens> / C<tokens-reset> header string, exactly as | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | the provider sent it — same untyped, provider-dependent shape as | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | L</requests_reset>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | Kept for back-compatibility. Prefer the typed pair L</tokens_reset_at> and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | L</tokens_reset_after>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | =head2 received | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | L<Langertha::Moment> stamped when the response headers were parsed. It is the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | reference instant against which the two halves of each reset bucket are | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | reconciled: a provider that sent only a duration (OpenAI family) gets its | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | C<*_reset_at> from C<< received + *_reset_after >>, and one that sent only an | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | instant (Anthropic) gets its C<*_reset_after> from C<< *_reset_at - received >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | Defaults to L<Time::Moment/now_utc> at construction; the header readers | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | stamp it at parse time. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | =head2 requests_reset_at | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | Maybe[L<Langertha::Moment>] — the instant the request limit resets (I<when>). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | Set directly from an RFC 3339 reset header (Anthropic); otherwise derived | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | lazily from L</received> plus L</requests_reset_after> when the provider sent | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | only a duration (OpenAI family). C<undef> when the provider sent no request | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | reset header at all — absence is the expected path for the many providers that | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | send none, not an error, and no default is invented. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | =head2 requests_reset_after | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | Maybe[Num] — seconds until the request limit resets (I<in how long>); may be | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | fractional. Set directly from a Go C<time.Duration> reset header (OpenAI | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | family); otherwise derived lazily from L</requests_reset_at> minus L</received> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | when the provider sent only an instant (Anthropic). C<undef> when the provider | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | sent no request reset header at all. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | =head2 tokens_reset_at | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | Maybe[L<Langertha::Moment>] — the instant the token limit resets (I<when>). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | The token-bucket mirror of L</requests_reset_at>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | =head2 tokens_reset_after | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | Maybe[Num] — seconds until the token limit resets (I<in how long>); may be | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | fractional. The token-bucket mirror of L</requests_reset_after>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | =head2 retry_after | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | Maybe[Num] — seconds the provider asks the client to wait before retrying, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | read from L</raw>. A numeric C<retry-after-ms> (Azure OpenAI and some | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | proxies send it next to C<retry-after>) wins, divided by 1000. Otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | C<retry-after> is read, a duration whichever form the wire uses: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | delta-seconds (C<8>; a fractional C<1.5> is accepted too) is taken as is, an | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | HTTP-date is measured from L</received> (a date already past gives C<0>, never | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | a negative wait). C<undef> when the response sent neither header, or none in a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | readable form (the verbatim values stay in L</raw>). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | Providers send it mostly on a C<429> or C<503>, which is why the engine records | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | the rate limit of an error response before it croaks (see | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | L<Langertha::Engine::Remote/rate_limit>). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | =head2 raw | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | HashRef of all rate-limit-related headers as returned by the provider, keyed | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | by lower-cased header name. The header readers collect every response header | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | matching C<< /^(x-ratelimit-|anthropic-ratelimit-|anthropic-priority-|anthropic-fast-|ratelimitbysize-)/i >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | plus C<retry-after> — a strict superset of the fields the normalized attributes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | cover, so provider-specific extras survive here even when they carry a window | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | in the name (Groq's per-day request bucket, Cerebras's C<x-ratelimit-reset-requests-day>) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | or a shape the normalizer does not model (Anthropic's C<anthropic-priority-*> / | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | C<anthropic-fast-*>, Mistral's C<-minute>-suffixed names). Useful for accessing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | provider-specific fields not covered by the normalized attributes. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | =head2 _resolve_retry_after | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | my $seconds = Langertha::RateLimit::_resolve_retry_after(\%raw, $received); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | The seconds to wait from a L</raw>-shaped hash: C<retry-after-ms> divided by | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 1000 when it holds a number, else C<retry-after> via L</_parse_retry_after>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | Backs L</retry_after> and the retry note in the error messages of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | L<Langertha::Role::HTTP>, so both say the same number. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | =head2 _parse_retry_after | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | my $seconds = Langertha::RateLimit::_parse_retry_after('8'); # 8 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | my $seconds = Langertha::RateLimit::_parse_retry_after($http_date, $received); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | Reads a C<Retry-After> value as the seconds to wait: delta-seconds directly, an | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | HTTP-date (via L<HTTP::Date>) as its distance from C<$received> (a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | L<Langertha::Moment>, default now), clamped at C<0>. Returns C<undef> for | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | anything else. The C<retry-after> half of L</_resolve_retry_after>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | =head2 _parse_go_duration | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | my $seconds = Langertha::RateLimit::_parse_go_duration('2m59.56s'); # 179.56 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | Parses a Go C<time.Duration.String()> string into fractional seconds. Handles | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | compound (C<"6m0s">, C<"1h2m3s">), sub-second (C<"250ms">, C<"35ms">) and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | fractional (C<"7.66s">) forms. Returns C<undef> — never guesses — for anything | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | that is not that exact shape (a bare number, an RFC 3339 instant, an epoch). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | Used by L<Langertha::Role::OpenAICompatible> to populate L</requests_reset_after> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | / L</tokens_reset_after> from the C<x-ratelimit-reset-*> headers. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | =head2 _collect_headers | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | my %raw = Langertha::RateLimit::_collect_headers($http_response); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | Collects every rate-limit-related response header into a hash keyed by | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | lower-cased name — the single source of truth for the L</raw> superset. Matches | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | the C<x-ratelimit-> / C<anthropic-ratelimit-> / C<anthropic-priority-> / | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | C<anthropic-fast-> / C<ratelimitbysize-> prefixes plus C<retry-after> and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | C<retry-after-ms>. The | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | wire-envelope roles call this, then normalize the known subset out of the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | result. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | =head2 to_hash | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | my $hash = $rate_limit->to_hash; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | Returns a flat HashRef of all defined rate limit fields plus the raw headers. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | The typed reset halves appear here whenever they were sent or can be derived: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | L</requests_reset_at> / L</tokens_reset_at> as a plain epoch number (their | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | C<0+> overload, matching L<Langertha::Response/created>) and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | L</requests_reset_after> / L</tokens_reset_after> as seconds, and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | L</retry_after> in seconds when the provider sent one. A bucket the provider | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | never spoke is omitted rather than defaulted. L</received> is not | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | included; read it from the accessor when the derivation anchor is needed. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | =head2 TO_JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | my $json = JSON::MaybeXS->new(convert_blessed => 1)->encode({ rate_limit => $rl }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | Serialization hook for JSON encoders configured with C<convert_blessed>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | Returns L</to_hash> B<without> the C<raw> key. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | C<TO_JSON> fires implicitly, from wherever the surrounding structure happens | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | to be encoded — a trace, a log line, a queue message. The caller did not ask | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | for this object and cannot see what it contributed, so the implicit path | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | carries only the normalized, provider-agnostic fields. C<raw> holds the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | provider's own rate-limit response headers; shipping those into third-party | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | sinks by accident is not something a caller should have to opt out of. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | A caller who wants the raw headers asks for them explicitly — via L</to_hash> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | or L</raw>. That is the only difference between the two methods, and the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | reason they deliberately do not return the same thing. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | =item * L<Langertha::Response> - Response objects carry rate limit data | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | =item * L<Langertha::Role::HTTP> - Extracts rate limit headers during response parsing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | =item * L<Langertha::Engine::Remote> - Stores the latest rate limit on the engine | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||