« blib/lib/Langertha/Engine/OllamaOpenAI.pm
↑ Summary
blib/lib/Langertha/Engine/OpenAIResponses.pm »
| 1 | package Langertha::Cost; | ||
| 2 | # ABSTRACT: Immutable value object for the monetary cost of a single LLM call | ||
| 3 | 10 | our $VERSION = '0.503'; | |
| 4 | 10 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 10 | has input_usd => ( is => 'ro', isa => 'Num', default => 0 ); | |
| 7 | 10 | has output_usd => ( is => 'ro', isa => 'Num', default => 0 ); | |
| 8 | 10 | has cache_read_usd => ( is => 'ro', isa => 'Num', default => 0 ); | |
| 9 | 10 | has cache_write_usd => ( is => 'ro', isa => 'Num', default => 0 ); | |
| 10 | 10 | has total_usd => ( is => 'ro', isa => 'Num', lazy => 1, builder => '_build_total_usd' ); | |
| 11 | 10 | has currency => ( is => 'ro', isa => 'Str', default => 'USD' ); | |
| 12 | |||
| 13 | 43 | ▶ | sub _build_total_usd { |
|
Subroutine
_build_total_usd: called CC=1 SCAR=0.0
| |||
| 14 | 43 | my ($self) = @_; | |
| 15 | 43 | return $self->input_usd + $self->output_usd + $self->cache_read_usd + $self->cache_write_usd; | |
| 16 | } | ||
| 17 | |||
| 18 | 4 | ▶ | sub to_hash { |
|
Subroutine
to_hash: called CC=1 SCAR=0.0
Pod
documented
| |||
| 19 | 4 | my ($self) = @_; | |
| 20 | return { | ||
| 21 | 4 | input_cost_usd => $self->input_usd + 0, | |
| 22 | output_cost_usd => $self->output_usd + 0, | ||
| 23 | cache_read_cost_usd => $self->cache_read_usd + 0, | ||
| 24 | cache_write_cost_usd => $self->cache_write_usd + 0, | ||
| 25 | total_cost_usd => $self->total_usd + 0, | ||
| 26 | currency => $self->currency, | ||
| 27 | }; | ||
| 28 | } | ||
| 29 | |||
| 30 | # Make the object transparent to any JSON encoder configured with | ||
| 31 | # convert_blessed => 1 (the house default, see Langertha::Plugin::Langfuse). | ||
| 32 | # to_hash is the complete canonical representation, so this is a plain | ||
| 33 | # delegator — nothing is dropped. | ||
| 34 | 1 | ▶ | sub TO_JSON { shift->to_hash } |
|
Subroutine
TO_JSON: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 35 | |||
| 36 | |||
| 37 | 10 | __PACKAGE__->meta->make_immutable; | |
| 38 | 10 | 1; | |
| 39 | |||
| 40 | __END__ | ||
| 41 | |||
| 42 | =pod | ||
| 43 | |||
| 44 | =encoding UTF-8 | ||
| 45 | |||
| 46 | =head1 NAME | ||
| 47 | |||
| 48 | Langertha::Cost - Immutable value object for the monetary cost of a single LLM call | ||
| 49 | |||
| 50 | =head1 VERSION | ||
| 51 | |||
| 52 | version 0.503 | ||
| 53 | |||
| 54 | =head2 input_usd | ||
| 55 | |||
| 56 | Cost of the input tokens. When the pricing rule had a cache rate, only the | ||
| 57 | tokens that were neither read from nor written to the prompt cache; otherwise | ||
| 58 | all of the usage's C<input_tokens>. Default C<0>. | ||
| 59 | |||
| 60 | =head2 output_usd | ||
| 61 | |||
| 62 | Cost of the output tokens. Default C<0>. | ||
| 63 | |||
| 64 | =head2 cache_read_usd | ||
| 65 | |||
| 66 | Cost of the prompt-cache reads (L<Langertha::Usage/cached_tokens>). C<0> unless | ||
| 67 | the pricing rule has a cache rate. Default C<0>. | ||
| 68 | |||
| 69 | =head2 cache_write_usd | ||
| 70 | |||
| 71 | Cost of the prompt-cache writes (L<Langertha::Usage/cache_write_tokens>). C<0> | ||
| 72 | unless the pricing rule has a cache rate. Default C<0>. | ||
| 73 | |||
| 74 | =head2 total_usd | ||
| 75 | |||
| 76 | The sum of the four amounts above, unless passed to C<new>. | ||
| 77 | |||
| 78 | =head2 currency | ||
| 79 | |||
| 80 | Default C<USD>. | ||
| 81 | |||
| 82 | =head2 to_hash | ||
| 83 | |||
| 84 | { input_cost_usd => ..., output_cost_usd => ..., cache_read_cost_usd => ..., | ||
| 85 | cache_write_cost_usd => ..., total_cost_usd => ..., currency => 'USD' } | ||
| 86 | |||
| 87 | The canonical hash; C<TO_JSON> returns the same. | ||
| 88 | |||
| 89 | =head1 SEE ALSO | ||
| 90 | |||
| 91 | =over | ||
| 92 | |||
| 93 | =item * L<Langertha::Pricing> - builds a Cost from a L<Langertha::Usage> | ||
| 94 | |||
| 95 | =back | ||
| 96 | |||
| 97 | =head1 SUPPORT | ||
| 98 | |||
| 99 | =head2 Issues | ||
| 100 | |||
| 101 | Please report bugs and feature requests on GitHub at | ||
| 102 | L<https://github.com/Getty/langertha/issues>. | ||
| 103 | |||
| 104 | =head2 IRC | ||
| 105 | |||
| 106 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||
| 107 | |||
| 108 | =head1 CONTRIBUTING | ||
| 109 | |||
| 110 | Contributions are welcome! Please fork the repository and submit a pull request. | ||
| 111 | |||
| 112 | =head1 AUTHOR | ||
| 113 | |||
| 114 | Torsten Raudssus <getty@cpan.org> | ||
| 115 | |||
| 116 | =head1 COPYRIGHT AND LICENSE | ||
| 117 | |||
| 118 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||
| 119 | |||
| 120 | This is free software; you can redistribute it and/or modify it under | ||
| 121 | the same terms as the Perl 5 programming language system itself. | ||
| 122 | |||
| 123 | =cut | ||