| 1 | package Langertha::Usage; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Immutable value object for LLM token usage with cross-provider conversion | ||||||||||||||||||||||||||||
| 3 | 3 | our $VERSION = '0.502'; | |||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | has input_tokens => ( is => 'ro', isa => 'Int', default => 0 ); | |||||||||||||||||||||||||||
| 8 | 3 | has output_tokens => ( is => 'ro', isa => 'Int', default => 0 ); | |||||||||||||||||||||||||||
| 9 | 3 | has total_tokens => ( is => 'ro', isa => 'Int', lazy => 1, builder => '_build_total_tokens' ); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub _build_total_tokens { | ||||||||||||||||||||||||||||
| 12 | 10 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_total_tokens: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 10 | return $self->input_tokens + $self->output_tokens; | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Build a Usage from any of the wire-format hashrefs we know about. | ||||||||||||||||||||||||||||
| 17 | sub from_hash { | ||||||||||||||||||||||||||||
| 18 | 11 | ▶ | my ($class, $hash) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_hash: called CC=14 SCAR=26.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 11 | ▶ | return $class->new unless $hash && ref($hash) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$hash and ref $hash eq "HASH"
MC/DC50%1/2
$hash and ref $hash eq "HASH"
$hash
ref $hash eq "HASH"
Truth table66%2/3
A and B
A= $hashB= ref $hash eq "HASH"
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 10 | my $input = $hash->{input_tokens}; | |||||||||||||||||||||||||||
| 22 | 10 | my $output = $hash->{output_tokens}; | |||||||||||||||||||||||||||
| 23 | 10 | my $total = $hash->{total_tokens}; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 10 | ▶ | $input = $hash->{prompt_tokens} if !defined $input && defined $hash->{prompt_tokens}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $input and defined $$hash{"prompt_tokens"}
MC/DC100%2/2
not defined $input and defined $$hash{"prompt_tokens"}
not defined $input
defined $$hash{"prompt_tokens"}
Truth table100%3/3
not(A) and B
A= not defined $inputB= defined $$hash{"prompt_tokens"}
| |||||||||||||||||||||||||||||
| 26 | 10 | ▶ | $input = $hash->{prompt_eval_count} if !defined $input && defined $hash->{prompt_eval_count}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $input and defined $$hash{"prompt_eval_count"}
MC/DC100%2/2
not defined $input and defined $$hash{"prompt_eval_count"}
not defined $input
defined $$hash{"prompt_eval_count"}
Truth table100%3/3
not(A) and B
A= not defined $inputB= defined $$hash{"prompt_eval_count"}
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 10 | ▶ | $output = $hash->{completion_tokens} if !defined $output && defined $hash->{completion_tokens}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $output and defined $$hash{"completion_tokens"}
MC/DC100%2/2
not defined $output and defined $$hash{"completion_tokens"}
not defined $output
defined $$hash{"completion_tokens"}
Truth table100%3/3
not(A) and B
A= not defined $outputB= defined $$hash{"completion_tokens"}
| |||||||||||||||||||||||||||||
| 29 | 10 | ▶ | $output = $hash->{eval_count} if !defined $output && defined $hash->{eval_count}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $output and defined $$hash{"eval_count"}
MC/DC100%2/2
not defined $output and defined $$hash{"eval_count"}
not defined $output
defined $$hash{"eval_count"}
Truth table100%3/3
not(A) and B
A= not defined $outputB= defined $$hash{"eval_count"}
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 10 | ▶ | $input = 0 + ($input // 0); | ||||||||||||||||||||||||||
|
Condition100%2/2
$input // 0
MC/DC100%1/1
$input // 0
$input
Truth table100%2/2
A
A= $input
| |||||||||||||||||||||||||||||
| 32 | 10 | ▶ | $output = 0 + ($output // 0); | ||||||||||||||||||||||||||
|
Condition100%2/2
$output // 0
MC/DC100%1/1
$output // 0
$output
Truth table100%2/2
A
A= $output
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 10 | my %args = ( input_tokens => $input, output_tokens => $output ); | |||||||||||||||||||||||||||
| 35 | 10 | ▶ | $args{total_tokens} = 0 + $total if defined $total; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 10 | return $class->new(%args); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Build a Usage from any response shape: a Langertha::Response, a HashRef | ||||||||||||||||||||||||||||
| 40 | # with a usage key, or undef. | ||||||||||||||||||||||||||||
| 41 | sub from_response { | ||||||||||||||||||||||||||||
| 42 | 1 | ▶ | my ($class, $response) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_response: called CC=7 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | return $class->new unless $response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | if ( blessed($response) && $response->isa('Langertha::Response') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($response) and $response->isa("Langertha::Response")
MC/DC0%0/2
builtin::blessed($response) and $response->isa("Langertha::Response")
builtin::blessed($response)
$response->isa("Langertha::Response")
Truth table33%1/3
A and B
A= builtin::blessed($response)B= $response->isa("Langertha::Response")
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | return $class->from_hash( $response->has_usage ? $response->usage : {} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 1 | ▶ | if ( ref($response) eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | return $class->from_hash( $response->{usage} || {} ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$$response{'usage'} || {}
MC/DC0%0/1
$$response{'usage'} || {}
$$response{'usage'}
Truth table50%1/2
A
A= $$response{'usage'}
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 0 | return $class->new; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # Immutable merge — returns a new Usage that is the sum of self + other. | ||||||||||||||||||||||||||||
| 55 | sub merge { | ||||||||||||||||||||||||||||
| 56 | 1 | ▶ | my ($self, $other) = @_; | ||||||||||||||||||||||||||
|
Subroutine
merge: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | return $self unless $other; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 1 | return ref($self)->new( | |||||||||||||||||||||||||||
| 59 | input_tokens => $self->input_tokens + $other->input_tokens, | ||||||||||||||||||||||||||||
| 60 | output_tokens => $self->output_tokens + $other->output_tokens, | ||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Canonical hash representation (input_tokens / output_tokens / total_tokens). | ||||||||||||||||||||||||||||
| 65 | sub to_hash { | ||||||||||||||||||||||||||||
| 66 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_hash: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | return { | ||||||||||||||||||||||||||||
| 68 | 3 | input_tokens => $self->input_tokens, | |||||||||||||||||||||||||||
| 69 | output_tokens => $self->output_tokens, | ||||||||||||||||||||||||||||
| 70 | total_tokens => $self->total_tokens, | ||||||||||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub to_openai_format { | ||||||||||||||||||||||||||||
| 75 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_openai_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | return { | ||||||||||||||||||||||||||||
| 77 | 1 | prompt_tokens => $self->input_tokens, | |||||||||||||||||||||||||||
| 78 | completion_tokens => $self->output_tokens, | ||||||||||||||||||||||||||||
| 79 | total_tokens => $self->total_tokens, | ||||||||||||||||||||||||||||
| 80 | }; | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub to_anthropic_format { | ||||||||||||||||||||||||||||
| 84 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_anthropic_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | return { | ||||||||||||||||||||||||||||
| 86 | 1 | input_tokens => $self->input_tokens, | |||||||||||||||||||||||||||
| 87 | output_tokens => $self->output_tokens, | ||||||||||||||||||||||||||||
| 88 | }; | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub to_ollama_format { | ||||||||||||||||||||||||||||
| 92 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_ollama_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | return { | ||||||||||||||||||||||||||||
| 94 | 1 | prompt_eval_count => $self->input_tokens, | |||||||||||||||||||||||||||
| 95 | eval_count => $self->output_tokens, | ||||||||||||||||||||||||||||
| 96 | }; | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 100 | 3 | 1; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | __END__ | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =pod | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 NAME | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Langertha::Usage - Immutable value object for LLM token usage with cross-provider conversion | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 VERSION | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | version 0.502 | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 Issues | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 121 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head2 IRC | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 140 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =cut | ||||||||||||||||||||||||||||