| 1 | package Langertha::Pricing; | ||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Model→price catalog producing Langertha::Cost from Langertha::Usage | ||||||||||||||||||||||||||||||||||||||
| 3 | 9 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use Langertha::Cost; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | # Map of model id → { input_per_million => N, output_per_million => N, | ||||||||||||||||||||||||||||||||||||||
| 8 | # cached_input_per_million => N, cache_write_per_million => N }. The last two | ||||||||||||||||||||||||||||||||||||||
| 9 | # are optional. All keys are USD per 1,000,000 tokens. | ||||||||||||||||||||||||||||||||||||||
| 10 | has rules => ( | ||||||||||||||||||||||||||||||||||||||
| 11 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||
| 12 | isa => 'HashRef', | ||||||||||||||||||||||||||||||||||||||
| 13 | 22 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 14 | 9 | ); | |||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | # Optional fallback rule applied when a model id is unknown. | ||||||||||||||||||||||||||||||||||||||
| 17 | has default_rule => ( | ||||||||||||||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||
| 19 | isa => 'Maybe[HashRef]', | ||||||||||||||||||||||||||||||||||||||
| 20 | 8 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 21 | 9 | ); | |||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | 46 | ▶ | sub rule_for { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
rule_for: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 24 | 46 | my ($self, $model) = @_; | |||||||||||||||||||||||||||||||||||||
| 25 | 46 | ▶ | return $self->rules->{$model} if defined $model && exists $self->rules->{$model}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $model and exists $self->rules->{$model}
MC/DC100%2/2
defined $model and exists $self->rules->{$model}
defined $model
exists $self->rules->{$model}
Truth table100%3/3
A and B
A= defined $modelB= exists $self->rules->{$model}
| |||||||||||||||||||||||||||||||||||||||
| 26 | 39 | return $self->default_rule; | |||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||
| 29 | 41 | ▶ | sub cost_for { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cost_for: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 30 | 41 | my ($self, $usage, $model) = @_; | |||||||||||||||||||||||||||||||||||||
| 31 | 41 | ▶ | my $rule = $self->rule_for($model) || {}; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->rule_for($model) || {}
MC/DC100%1/1
$self->rule_for($model) || {}
$self->rule_for($model)
Truth table100%2/2
A
A= $self->rule_for($model)
| |||||||||||||||||||||||||||||||||||||||
| 32 | 41 | ▶ | my $ipm = 0 + ( $rule->{input_per_million} // 0 ); | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$$rule{"input_per_million"} // 0
MC/DC100%1/1
$$rule{"input_per_million"} // 0
$$rule{"input_per_million"}
Truth table100%2/2
A
A= $$rule{"input_per_million"}
| |||||||||||||||||||||||||||||||||||||||
| 33 | 41 | ▶ | my $opm = 0 + ( $rule->{output_per_million} // 0 ); | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$$rule{"output_per_million"} // 0
MC/DC100%1/1
$$rule{"output_per_million"} // 0
$$rule{"output_per_million"}
Truth table100%2/2
A
A= $$rule{"output_per_million"}
| |||||||||||||||||||||||||||||||||||||||
| 34 | 41 | my $output_usd = ( $usage->output_tokens / 1_000_000 ) * $opm; | |||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | # A rule without a cache rate prices input_tokens as a whole, as it always | ||||||||||||||||||||||||||||||||||||||
| 37 | # did. A rule with one splits the cache counts out of input_tokens (or adds | ||||||||||||||||||||||||||||||||||||||
| 38 | # them, where the wire counts them beside it — Usage->input_includes_cache) | ||||||||||||||||||||||||||||||||||||||
| 39 | # so no token is priced twice. A missing rate falls back to the input rate: | ||||||||||||||||||||||||||||||||||||||
| 40 | # no discount the rule did not state. (k263, ADR 0031) | ||||||||||||||||||||||||||||||||||||||
| 41 | 41 | my $cached_rate = $rule->{cached_input_per_million}; | |||||||||||||||||||||||||||||||||||||
| 42 | 41 | my $write_rate = $rule->{cache_write_per_million}; | |||||||||||||||||||||||||||||||||||||
| 43 | 41 | ▶ | unless ( defined $cached_rate || defined $write_rate ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $cached_rate or defined $write_rate
MC/DC100%2/2
defined $cached_rate or defined $write_rate
defined $cached_rate
defined $write_rate
Truth table100%3/3
A or B
A= defined $cached_rateB= defined $write_rate
| |||||||||||||||||||||||||||||||||||||||
| 44 | 17 | return Langertha::Cost->new( | |||||||||||||||||||||||||||||||||||||
| 45 | input_usd => ( $usage->input_tokens / 1_000_000 ) * $ipm, | ||||||||||||||||||||||||||||||||||||||
| 46 | output_usd => $output_usd, | ||||||||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||
| 49 | 24 | ▶ | my $cached = $usage->cached_tokens // 0; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$usage->cached_tokens // 0
MC/DC0%0/1
$usage->cached_tokens // 0
$usage->cached_tokens
Truth table50%1/2
A
A= $usage->cached_tokens
| |||||||||||||||||||||||||||||||||||||||
| 50 | 24 | ▶ | my $write = $usage->cache_write_tokens // 0; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$usage->cache_write_tokens // 0
MC/DC100%1/1
$usage->cache_write_tokens // 0
$usage->cache_write_tokens
Truth table100%2/2
A
A= $usage->cache_write_tokens
| |||||||||||||||||||||||||||||||||||||||
| 51 | 24 | ▶ | return Langertha::Cost->new( | ||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$cached_rate // $ipm
$write_rate // $ipm
MC/DC0%0/2
$cached_rate // $ipm
$cached_rate
$ipm
MC/DC0%0/2
$write_rate // $ipm
$write_rate
$ipm
Truth table66%2/3
A // B
A= $cached_rateB= $ipm
Truth table66%2/3
A // B
A= $write_rateB= $ipm
| |||||||||||||||||||||||||||||||||||||||
| 52 | input_usd => ( $usage->uncached_input_tokens / 1_000_000 ) * $ipm, | ||||||||||||||||||||||||||||||||||||||
| 53 | output_usd => $output_usd, | ||||||||||||||||||||||||||||||||||||||
| 54 | cache_read_usd => ( $cached / 1_000_000 ) * ( 0 + ( $cached_rate // $ipm ) ), | ||||||||||||||||||||||||||||||||||||||
| 55 | cache_write_usd => ( $write / 1_000_000 ) * ( 0 + ( $write_rate // $ipm ) ), | ||||||||||||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||
| 60 | 9 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||
| 61 | 9 | 1; | |||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||
| 63 | __END__ | ||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||
| 65 | =pod | ||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||
| 67 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||
| 69 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||
| 71 | Langertha::Pricing - Model→price catalog producing Langertha::Cost from Langertha::Usage | ||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||
| 73 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||
| 75 | version 0.503 | ||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||
| 79 | my $pricing = Langertha::Pricing->new( | ||||||||||||||||||||||||||||||||||||||
| 80 | rules => { | ||||||||||||||||||||||||||||||||||||||
| 81 | 'my-model' => { | ||||||||||||||||||||||||||||||||||||||
| 82 | input_per_million => 3, | ||||||||||||||||||||||||||||||||||||||
| 83 | output_per_million => 15, | ||||||||||||||||||||||||||||||||||||||
| 84 | cached_input_per_million => 0.30, # optional | ||||||||||||||||||||||||||||||||||||||
| 85 | cache_write_per_million => 3.75, # optional | ||||||||||||||||||||||||||||||||||||||
| 86 | }, | ||||||||||||||||||||||||||||||||||||||
| 87 | }, | ||||||||||||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||||||||||||
| 89 | my $cost = $pricing->cost_for( $response->usage, $response->model ); | ||||||||||||||||||||||||||||||||||||||
| 90 | printf "%.6f %s\n", $cost->total_usd, $cost->currency; | ||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||
| 92 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||
| 94 | Turns a L<Langertha::Usage> into a L<Langertha::Cost> from price rules you | ||||||||||||||||||||||||||||||||||||||
| 95 | supply. Langertha ships no prices: provider prices change too often. | ||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | =head2 rules | ||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||
| 99 | HashRef of model id to rule. A rule is a HashRef of USD per 1,000,000 tokens: | ||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | =over | ||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||
| 103 | =item * C<input_per_million> - input (prompt) tokens | ||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||
| 105 | =item * C<output_per_million> - output (completion) tokens | ||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||
| 107 | =item * C<cached_input_per_million> - optional; tokens read from the prompt | ||||||||||||||||||||||||||||||||||||||
| 108 | cache (L<Langertha::Usage/cached_tokens>) | ||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||
| 110 | =item * C<cache_write_per_million> - optional; tokens written to the prompt | ||||||||||||||||||||||||||||||||||||||
| 111 | cache (L<Langertha::Usage/cache_write_tokens>) | ||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||
| 113 | =back | ||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||
| 115 | A missing C<input_per_million> or C<output_per_million> counts as C<0>. | ||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||
| 117 | =head2 default_rule | ||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||
| 119 | Rule used when the model has no entry in L</rules>. C<undef> by default, so an | ||||||||||||||||||||||||||||||||||||||
| 120 | unknown model costs nothing. | ||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||
| 122 | =head2 rule_for | ||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||
| 124 | my $rule = $pricing->rule_for($model); | ||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||
| 126 | The rule for C<$model>, else L</default_rule>. | ||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||
| 128 | =head2 cost_for | ||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | my $cost = $pricing->cost_for( $usage, $model ); | ||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||
| 132 | Prices C<$usage> with the rule for C<$model> and returns a L<Langertha::Cost>. | ||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||
| 134 | A rule with neither cache key prices all of C<input_tokens> | ||||||||||||||||||||||||||||||||||||||
| 135 | at C<input_per_million>, whatever part of it was cached, and sets no cache | ||||||||||||||||||||||||||||||||||||||
| 136 | amounts. | ||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | A rule with at least one cache key prices each token once, in one of three | ||||||||||||||||||||||||||||||||||||||
| 139 | amounts. C<input_usd> covers L<Langertha::Usage/uncached_input_tokens>; | ||||||||||||||||||||||||||||||||||||||
| 140 | C<cache_read_usd> covers the cache reads at C<cached_input_per_million>; | ||||||||||||||||||||||||||||||||||||||
| 141 | C<cache_write_usd> covers the cache writes at C<cache_write_per_million>. A | ||||||||||||||||||||||||||||||||||||||
| 142 | cache key the rule does not have falls back to C<input_per_million>, so no | ||||||||||||||||||||||||||||||||||||||
| 143 | discount is assumed. On wires that count the cache tokens inside the input | ||||||||||||||||||||||||||||||||||||||
| 144 | count (OpenAI, Open-Responses, Gemini, AKI.IO native) they are taken out of | ||||||||||||||||||||||||||||||||||||||
| 145 | it; on Anthropic's wire, which counts them beside it, they are added (see | ||||||||||||||||||||||||||||||||||||||
| 146 | L<Langertha::Usage/input_includes_cache>). | ||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||
| 148 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||
| 150 | =over | ||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||
| 152 | =item * L<Langertha::Usage> | ||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||
| 154 | =item * L<Langertha::Cost> | ||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||
| 156 | =back | ||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||
| 158 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||
| 160 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||
| 162 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||
| 163 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||
| 165 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||
| 167 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||
| 169 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||
| 171 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||
| 173 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||
| 175 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||
| 177 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||
| 179 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||
| 181 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||
| 182 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||||||||||||||||||||