| 1 | package Langertha::RateLimit; | ||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Rate limit information from API response headers | ||||||||||||||||||||||||||||||
| 3 | 34 | our $VERSION = '0.502'; | |||||||||||||||||||||||||||||
| 4 | 34 | ▶ | use Moose; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | 34 | has requests_limit => ( | |||||||||||||||||||||||||||||
| 8 | is => 'ro', | ||||||||||||||||||||||||||||||
| 9 | isa => 'Maybe[Num]', | ||||||||||||||||||||||||||||||
| 10 | default => undef, | ||||||||||||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | 34 | has requests_remaining => ( | |||||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||||
| 16 | isa => 'Maybe[Num]', | ||||||||||||||||||||||||||||||
| 17 | default => undef, | ||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||
| 21 | 34 | has requests_reset => ( | |||||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||||
| 23 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||
| 24 | default => undef, | ||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | 34 | has tokens_limit => ( | |||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||||
| 30 | isa => 'Maybe[Num]', | ||||||||||||||||||||||||||||||
| 31 | default => undef, | ||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||
| 35 | 34 | has tokens_remaining => ( | |||||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||||
| 37 | isa => 'Maybe[Num]', | ||||||||||||||||||||||||||||||
| 38 | default => undef, | ||||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||
| 42 | 34 | has tokens_reset => ( | |||||||||||||||||||||||||||||
| 43 | is => 'ro', | ||||||||||||||||||||||||||||||
| 44 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||
| 45 | default => undef, | ||||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||
| 49 | has raw => ( | ||||||||||||||||||||||||||||||
| 50 | is => 'ro', | ||||||||||||||||||||||||||||||
| 51 | isa => 'HashRef', | ||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 53 | 34 | ); | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||
| 56 | sub to_hash { | ||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
to_hash: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||
| 58 | return { | ||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | ( defined $self->requests_limit ? ( requests_limit => $self->requests_limit ) : () ), | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 60 | ( defined $self->requests_remaining ? ( requests_remaining => $self->requests_remaining ) : () ), | ||||||||||||||||||||||||||||||
| 61 | ( defined $self->requests_reset ? ( requests_reset => $self->requests_reset ) : () ), | ||||||||||||||||||||||||||||||
| 62 | ( defined $self->tokens_limit ? ( tokens_limit => $self->tokens_limit ) : () ), | ||||||||||||||||||||||||||||||
| 63 | ( defined $self->tokens_remaining ? ( tokens_remaining => $self->tokens_remaining ) : () ), | ||||||||||||||||||||||||||||||
| 64 | ( defined $self->tokens_reset ? ( tokens_reset => $self->tokens_reset ) : () ), | ||||||||||||||||||||||||||||||
| 65 | raw => $self->raw, | ||||||||||||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||
| 71 | 34 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||
| 73 | 34 | 1; | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||
| 75 | __END__ | ||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||
| 77 | =pod | ||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||
| 79 | =encoding UTF-8 | ||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | =head1 NAME | ||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||
| 83 | Langertha::RateLimit - Rate limit information from API response headers | ||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | =head1 VERSION | ||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||
| 87 | version 0.502 | ||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||
| 91 | my $response = $engine->simple_chat('Hello'); | ||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | if ($response->has_rate_limit) { | ||||||||||||||||||||||||||||||
| 94 | my $rl = $response->rate_limit; | ||||||||||||||||||||||||||||||
| 95 | say "Requests remaining: ", $rl->requests_remaining // 'unknown'; | ||||||||||||||||||||||||||||||
| 96 | say "Tokens remaining: ", $rl->tokens_remaining // 'unknown'; | ||||||||||||||||||||||||||||||
| 97 | say "Reset in: ", $rl->requests_reset // 'unknown', " seconds"; | ||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||
| 100 | # Access raw provider-specific headers | ||||||||||||||||||||||||||||||
| 101 | my $raw = $response->rate_limit->raw; | ||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||
| 103 | # Also available on the engine (always reflects latest response) | ||||||||||||||||||||||||||||||
| 104 | if ($engine->has_rate_limit) { | ||||||||||||||||||||||||||||||
| 105 | say "Engine requests remaining: ", $engine->rate_limit->requests_remaining; | ||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||
| 108 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | Normalized rate limit data extracted from HTTP response headers. Different | ||||||||||||||||||||||||||||||
| 111 | providers use different header naming conventions; this class provides a | ||||||||||||||||||||||||||||||
| 112 | unified interface. | ||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||
| 114 | B<Supported providers:> | ||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | =over 4 | ||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||
| 118 | =item * OpenAI, Groq, Cerebras, OpenRouter, Replicate, HuggingFace (C<x-ratelimit-*>) | ||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||
| 120 | =item * Anthropic (C<anthropic-ratelimit-*>) | ||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | =back | ||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||
| 124 | Engines that do not return rate limit headers (DeepSeek, Ollama, vLLM, | ||||||||||||||||||||||||||||||
| 125 | LlamaCpp, etc.) will not have a rate_limit set. | ||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||
| 127 | =head2 requests_limit | ||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||
| 129 | Maximum number of requests allowed in the current window. | ||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||
| 131 | =head2 requests_remaining | ||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||
| 133 | Number of requests remaining in the current window. | ||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | =head2 requests_reset | ||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||
| 137 | Time until the request limit resets. Format varies by provider (seconds, | ||||||||||||||||||||||||||||||
| 138 | RFC 3339 timestamp, or epoch). | ||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||
| 140 | =head2 tokens_limit | ||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||
| 142 | Maximum number of tokens allowed in the current window. | ||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||
| 144 | =head2 tokens_remaining | ||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||
| 146 | Number of tokens remaining in the current window. | ||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||
| 148 | =head2 tokens_reset | ||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||
| 150 | Time until the token limit resets. Format varies by provider. | ||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||
| 152 | =head2 raw | ||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||
| 154 | HashRef of all rate-limit-related headers as returned by the provider. | ||||||||||||||||||||||||||||||
| 155 | Useful for accessing provider-specific fields not covered by the | ||||||||||||||||||||||||||||||
| 156 | normalized attributes (e.g. Anthropic's C<input-tokens-limit>). | ||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||
| 158 | =head2 to_hash | ||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||
| 160 | my $hash = $rate_limit->to_hash; | ||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||
| 162 | Returns a flat HashRef of all defined rate limit fields plus the raw headers. | ||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||
| 164 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||
| 166 | =over | ||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||
| 168 | =item * L<Langertha::Response> - Response objects carry rate limit data | ||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||
| 170 | =item * L<Langertha::Role::HTTP> - Extracts rate limit headers during response parsing | ||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||
| 172 | =item * L<Langertha::Engine::Remote> - Stores the latest rate limit on the engine | ||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||
| 174 | =back | ||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||
| 176 | =head1 SUPPORT | ||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||
| 178 | =head2 Issues | ||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||
| 180 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||
| 181 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||
| 183 | =head2 IRC | ||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||
| 185 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||
| 187 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||
| 189 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||
| 191 | =head1 AUTHOR | ||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||
| 193 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||
| 195 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||
| 197 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||
| 199 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||
| 200 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||
| 202 | =cut | ||||||||||||||||||||||||||||||