blib/lib/Langertha/Usage.pm

filter: Statementstmt 56.8% 199 / 350 Branchbran 46.9% 121 / 258 Conditioncond 41.0% 124 / 302 MC/DCmcdc 32.9% 55 / 167 Subroutinesub 69.4% 25 / 36 Podpod 55.5% 10 / 18 totaltotal 47.2% 534 / 1131 CC 257 SCAR 61.7
CC
257
Cov
49%
from_hash
911.3
from_hash
911.3
merge
64.3
CRAP 479.7
↑ Summary bin/langertha_image »
1 package Langertha::Usage;
2 # ABSTRACT: Immutable value object for LLM token usage with cross-provider conversion
3 200 our $VERSION = '0.505';
4 200 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 200 ▶use Scalar::Util qw( blessed looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 200 ▶use Hash::Util::FieldHash qw( fieldhash );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # The %{} overload (back-compat for `$response->usage->{...}`) hijacks every
9 # `$self->{attr}` deref on the object — including the ones Moose's generated
10 # accessors use internally. A naive overload therefore breaks the accessors
11 # (verified: infinite recursion / undef reads). The canonical Perl solution is
12 # to keep the attribute values in a field hash (keyed by object identity, not
13 # by the object's own hash) and route both the accessors and the overload
14 # through it. See "HASH OVERLOAD" below.
15 200 fieldhash my %DATA;
16  
17 use overload
18 3266 ▶ '%{}' => sub { $_[0]->_as_hash },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
19 200 fallback => 1;
20  
21 200 has input_tokens => ( is => 'ro', isa => 'Int', default => 0 );
22 200 has output_tokens => ( is => 'ro', isa => 'Int', default => 0 );
23 200 has total_tokens => ( is => 'ro', isa => 'Int', lazy => 1, builder => '_build_total_tokens' );
24  
25 200 has cached_tokens => ( is => 'ro', isa => 'Maybe[Int]', default => undef );
26 200 has cache_write_tokens => ( is => 'ro', isa => 'Maybe[Int]', default => undef );
27 200 has reasoning_tokens => ( is => 'ro', isa => 'Maybe[Int]', default => undef );
28 200 has input_includes_cache => ( is => 'ro', isa => 'Maybe[Bool]', default => undef );
29 200 has cost_usd => ( is => 'ro', isa => 'Maybe[Num]', default => undef );
30  
31 200 has raw => (
32 is => 'ro',
33 isa => 'Maybe[HashRef]',
34 default => undef,
35 );
36  
37 # The generated accessors read $self->{attr}, which the %{} overload would
38 # route through _as_hash. Route them through the field hash instead.
39 214 ▶around input_tokens => sub { my ( $orig, $self ) = @_; $DATA{$self}{input_tokens} };
Subroutine __ANON__: called CC=1 SCAR=0.0
40 161 ▶around output_tokens => sub { my ( $orig, $self ) = @_; $DATA{$self}{output_tokens} };
Subroutine __ANON__: called CC=1 SCAR=0.0
41 87 ▶around total_tokens => sub {
Subroutine __ANON__: called CC=5 SCAR=16.8
42 87 my ( $orig, $self ) = @_;
43 87 ▶ my $d = $DATA{$self} || {};
Condition50%1/2
$DATA{$self} || {}
l!l
870
MC/DC0%0/1
$DATA{$self} || {}
$DATA{$self}
Truth table50%1/2
A
A= $DATA{$self}
Aresult
11
00
44 87 ▶ return $d->{total_tokens} if defined $d->{total_tokens};
Branch: 1 branch
BranchTrueFalseTotal
if defined $d->{"total_tokens"} 62 25 87
45 25 ▶ return ( $d->{input_tokens} // 0 ) + ( $d->{output_tokens} // 0 );
Condition50%2/4
$d->{"input_tokens"} // 0
l!l
250
$d->{"output_tokens"} // 0
l!l
250
MC/DC0%0/1
$d->{"input_tokens"} // 0
$d->{"input_tokens"}
MC/DC0%0/1
$d->{"output_tokens"} // 0
$d->{"output_tokens"}
Truth table50%1/2
A
A= $d->{"input_tokens"}
Aresult
11
00
Truth table50%1/2
A
A= $d->{"output_tokens"}
Aresult
11
00
46 200 };
47 453 ▶around cached_tokens => sub { my ( $orig, $self ) = @_; $DATA{$self}{cached_tokens} };
Subroutine __ANON__: called CC=1 SCAR=0.0
48 119 ▶around cache_write_tokens => sub { my ( $orig, $self ) = @_; $DATA{$self}{cache_write_tokens} };
Subroutine __ANON__: called CC=1 SCAR=0.0
49 42 ▶around reasoning_tokens => sub { my ( $orig, $self ) = @_; $DATA{$self}{reasoning_tokens} };
Subroutine __ANON__: called CC=1 SCAR=0.0
50 92 ▶around input_includes_cache => sub { my ( $orig, $self ) = @_; $DATA{$self}{input_includes_cache} };
Subroutine __ANON__: called CC=1 SCAR=0.0
51 60 ▶around cost_usd => sub { my ( $orig, $self ) = @_; $DATA{$self}{cost_usd} };
Subroutine __ANON__: called CC=1 SCAR=0.0
52 0 ▶around raw => sub { my ( $orig, $self ) = @_; $DATA{$self}{raw} };
Subroutine __ANON__: not called CC=1 SCAR=2.6
53  
54 # The constructor's writes to $self->{attr} go through the overload and are
55 # lost, so the field hash is populated here from the raw constructor args.
56 376 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=14.2
Pod undocumented
57 0 ▶ my ( $self, $args ) = @_;
Subroutine BUILD: not called CC=3 SCAR=14.2
Pod undocumented
58 $DATA{$self} = {
59 input_tokens => $args->{input_tokens} // 0,
60 output_tokens => $args->{output_tokens} // 0,
61 total_tokens => $args->{total_tokens},
62 cached_tokens => $args->{cached_tokens},
63 cache_write_tokens => $args->{cache_write_tokens},
64 reasoning_tokens => $args->{reasoning_tokens},
65 input_includes_cache => $args->{input_includes_cache},
66 cost_usd => $args->{cost_usd},
67 raw => $args->{raw},
68 0 ▶ };
Condition50%4/8
$args->{"input_tokens"} // 0
l!l
3742
$args->{"output_tokens"} // 0
l!l
3697
$args->{"input_tokens"} // 0
l!l
00
$args->{"output_tokens"} // 0
l!l
00
MC/DC100%1/1
$args->{"input_tokens"} // 0
$args->{"input_tokens"}
MC/DC100%1/1
$args->{"output_tokens"} // 0
$args->{"output_tokens"}
MC/DC0%0/1
$args->{"input_tokens"} // 0
$args->{"input_tokens"}
MC/DC0%0/1
$args->{"output_tokens"} // 0
$args->{"output_tokens"}
Truth table100%2/2
A
A= $args->{"input_tokens"}
Aresult
11
00
Truth table100%2/2
A
A= $args->{"output_tokens"}
Aresult
11
00
Truth table0%0/2
A
A= $args->{"input_tokens"}
Aresult
11
00
Truth table0%0/2
A
A= $args->{"output_tokens"}
Aresult
11
00
69 }
70  
71 # input_tokens without the cache reads and writes that are counted in it.
72 41 ▶sub uncached_input_tokens {
Subroutine uncached_input_tokens: called CC=6 SCAR=24.3
Pod documented
73 0 ▶ my ($self) = @_;
Subroutine uncached_input_tokens: not called CC=6 SCAR=24.3
Pod documented
74 0 my $input = $self->input_tokens;
75 0 my $includes = $self->input_includes_cache;
76 0 ▶ return $input if defined $includes && !$includes;
Branch: 2 branches
BranchTrueFalseTotal
if defined $includes and not $includes 13 28 41
if defined $includes and not $includes 0 0 0
Condition50%3/6
defined $includes and not $includes
!ll&&!rl&&r
32513
defined $includes and not $includes
!ll&&!rl&&r
000
MC/DC100%2/2
defined $includes and not $includes
defined $includes not $includes
MC/DC0%0/2
defined $includes and not $includes
defined $includes not $includes
Truth table100%3/3
A and not(B)
A= defined $includesB= not $includes
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= defined $includesB= not $includes
ABresult
0X0
100
111
77 0 ▶ my $rest = $input - ( $self->cached_tokens // 0 ) - ( $self->cache_write_tokens // 0 );
Condition37%3/8
$self->cached_tokens // 0
l!l
280
$self->cache_write_tokens // 0
l!l
820
$self->cached_tokens // 0
l!l
00
$self->cache_write_tokens // 0
l!l
00
MC/DC0%0/1
$self->cached_tokens // 0
$self->cached_tokens
MC/DC100%1/1
$self->cache_write_tokens // 0
$self->cache_write_tokens
MC/DC0%0/1
$self->cached_tokens // 0
$self->cached_tokens
MC/DC0%0/1
$self->cache_write_tokens // 0
$self->cache_write_tokens
Truth table50%1/2
A
A= $self->cached_tokens
Aresult
11
00
Truth table100%2/2
A
A= $self->cache_write_tokens
Aresult
11
00
Truth table0%0/2
A
A= $self->cached_tokens
Aresult
11
00
Truth table0%0/2
A
A= $self->cache_write_tokens
Aresult
11
00
78 0 ▶ return $rest < 0 ? 0 : $rest;
Branch: 2 branches
BranchTrueFalseTotal
$rest < 0 ? : 1 27 28
$rest < 0 ? : 0 0 0
79 }
80  
81 0 ▶sub _build_total_tokens {
Subroutine _build_total_tokens: not called CC=1 SCAR=6.9
82 0 ▶ my ($self) = @_;
Subroutine _build_total_tokens: not called CC=1 SCAR=6.9
83 0 return $self->input_tokens + $self->output_tokens;
84 }
85  
86 # Build a Usage from any of the wire-format hashrefs we know about.
87 337 ▶sub from_hash {
Subroutine from_hash: called CC=75 SCAR=68.1
Pod documented
88 0 ▶ my ($class, $hash) = @_;
Subroutine from_hash: not called CC=75 SCAR=68.1
Pod documented
89 0 ▶ return $class->new unless $hash && ref($hash) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless $hash and ref $hash eq "HASH" 1 336 337
unless $hash and ref $hash eq "HASH" 0 0 0
Condition33%2/6
$hash and ref $hash eq "HASH"
!ll&&!rl&&r
10336
$hash and ref $hash eq "HASH"
!ll&&!rl&&r
000
MC/DC50%1/2
$hash and ref $hash eq "HASH"
$hash ref $hash eq "HASH"
MC/DC0%0/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"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $hashB= ref $hash eq "HASH"
ABresult
0X0
100
111
90  
91 0 my $input = $hash->{input_tokens};
92 0 my $output = $hash->{output_tokens};
93 0 my $total = $hash->{total_tokens};
94  
95 0 ▶ $input = $hash->{prompt_tokens} if !defined $input && defined $hash->{prompt_tokens};
Branch: 2 branches
BranchTrueFalseTotal
if not defined $input and defined $hash->{"prompt_tokens"} 192 144 336
if not defined $input and defined $hash->{"prompt_tokens"} 0 0 0
Condition50%3/6
not defined $input and defined $hash->{"prompt_tokens"}
!ll&&!rl&&r
8955192
not defined $input and defined $hash->{"prompt_tokens"}
!ll&&!rl&&r
000
MC/DC100%2/2
not defined $input and defined $hash->{"prompt_tokens"}
not defined $input defined $hash->{"prompt_tokens"}
MC/DC0%0/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"}
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not defined $inputB= defined $hash->{"prompt_tokens"}
ABresult
0X0
100
111
96 0 ▶ $input = $hash->{prompt_eval_count} if !defined $input && defined $hash->{prompt_eval_count};
Branch: 2 branches
BranchTrueFalseTotal
if not defined $input and defined $hash->{"prompt_eval_count"} 5 331 336
if not defined $input and defined $hash->{"prompt_eval_count"} 0 0 0
Condition50%3/6
not defined $input and defined $hash->{"prompt_eval_count"}
!ll&&!rl&&r
281505
not defined $input and defined $hash->{"prompt_eval_count"}
!ll&&!rl&&r
000
MC/DC100%2/2
not defined $input and defined $hash->{"prompt_eval_count"}
not defined $input defined $hash->{"prompt_eval_count"}
MC/DC0%0/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"}
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not defined $inputB= defined $hash->{"prompt_eval_count"}
ABresult
0X0
100
111
97  
98 0 ▶ $output = $hash->{completion_tokens} if !defined $output && defined $hash->{completion_tokens};
Branch: 2 branches
BranchTrueFalseTotal
if not defined $output and defined $hash->{"completion_tokens"} 170 166 336
if not defined $output and defined $hash->{"completion_tokens"} 0 0 0
Condition50%3/6
not defined $output and defined $hash->{"completion_tokens"}
!ll&&!rl&&r
7987170
not defined $output and defined $hash->{"completion_tokens"}
!ll&&!rl&&r
000
MC/DC100%2/2
not defined $output and defined $hash->{"completion_tokens"}
not defined $output defined $hash->{"completion_tokens"}
MC/DC0%0/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"}
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not defined $outputB= defined $hash->{"completion_tokens"}
ABresult
0X0
100
111
99 0 ▶ $output = $hash->{eval_count} if !defined $output && defined $hash->{eval_count};
Branch: 2 branches
BranchTrueFalseTotal
if not defined $output and defined $hash->{"eval_count"} 5 331 336
if not defined $output and defined $hash->{"eval_count"} 0 0 0
Condition50%3/6
not defined $output and defined $hash->{"eval_count"}
!ll&&!rl&&r
249825
not defined $output and defined $hash->{"eval_count"}
!ll&&!rl&&r
000
MC/DC100%2/2
not defined $output and defined $hash->{"eval_count"}
not defined $output defined $hash->{"eval_count"}
MC/DC0%0/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"}
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not defined $outputB= defined $hash->{"eval_count"}
ABresult
0X0
100
111
100  
101 # Gemini's usageMetadata spelling (the raw body, before the engine renames it).
102 # Gemini counts thinking and the tool-use prompt beside the prompt and the
103 # answer: totalTokenCount = promptTokenCount + toolUsePromptTokenCount +
104 # candidatesTokenCount + thoughtsTokenCount. Thinking is billed as output, so
105 # it is folded into output_tokens (what OpenAI's completion_tokens already
106 # includes), the tool-use prompt into input_tokens. proto3 JSON omits zero
107 # counts, so either half of a sum may be missing (k299).
108 0 my $gemini_thoughts = $hash->{thoughtsTokenCount};
109 0 ▶ if ( !defined $input && ( defined $hash->{promptTokenCount} || defined $hash->{toolUsePromptTokenCount} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $input and defined $hash->{"promptTokenCount"} || defined $hash->{"toolUsePromptTokenCount"}) 14 322 336
if (not defined $input and defined $hash->{"promptTokenCount"} || defined $hash->{"toolUsePromptTokenCount"}) 0 0 0
Condition41%5/12
not defined $input and defined $hash->{"promptTokenCount"} || defined $hash->{"toolUsePromptTokenCount"}
!ll&&!rl&&r
2863614
defined $hash->{"promptTokenCount"} or defined $hash->{"toolUsePromptTokenCount"}
l!l&&r!l&&!r
14036
not defined $input and defined $hash->{"promptTokenCount"} || defined $hash->{"toolUsePromptTokenCount"}
!ll&&!rl&&r
000
defined $hash->{"promptTokenCount"} or defined $hash->{"toolUsePromptTokenCount"}
l!l&&r!l&&!r
000
MC/DC66%2/3
not defined $input and defined $hash->{"promptTokenCount"} || defined $hash->{"toolUsePromptTokenCount"}
not defined $input defined $hash->{"promptTokenCount"} defined $hash->{"toolUsePromptTokenCount"}
MC/DC0%0/3
not defined $input and defined $hash->{"promptTokenCount"} || defined $hash->{"toolUsePromptTokenCount"}
not defined $input defined $hash->{"promptTokenCount"} defined $hash->{"toolUsePromptTokenCount"}
Truth table75%3/4
not(A) and B or C
A= not defined $inputB= defined $hash->{"promptTokenCount"}C= defined $hash->{"toolUsePromptTokenCount"}
ABCresult
0XX0
1000
11X1
1011
Truth table0%0/4
not(A) and B or C
A= not defined $inputB= defined $hash->{"promptTokenCount"}C= defined $hash->{"toolUsePromptTokenCount"}
ABCresult
0XX0
1000
11X1
1011
110 0 ▶ $input = ( $hash->{promptTokenCount} // 0 ) + ( $hash->{toolUsePromptTokenCount} // 0 );
Condition37%3/8
$hash->{"promptTokenCount"} // 0
l!l
140
$hash->{"toolUsePromptTokenCount"} // 0
l!l
113
$hash->{"promptTokenCount"} // 0
l!l
00
$hash->{"toolUsePromptTokenCount"} // 0
l!l
00
MC/DC0%0/1
$hash->{"promptTokenCount"} // 0
$hash->{"promptTokenCount"}
MC/DC100%1/1
$hash->{"toolUsePromptTokenCount"} // 0
$hash->{"toolUsePromptTokenCount"}
MC/DC0%0/1
$hash->{"promptTokenCount"} // 0
$hash->{"promptTokenCount"}
MC/DC0%0/1
$hash->{"toolUsePromptTokenCount"} // 0
$hash->{"toolUsePromptTokenCount"}
Truth table50%1/2
A
A= $hash->{"promptTokenCount"}
Aresult
11
00
Truth table100%2/2
A
A= $hash->{"toolUsePromptTokenCount"}
Aresult
11
00
Truth table0%0/2
A
A= $hash->{"promptTokenCount"}
Aresult
11
00
Truth table0%0/2
A
A= $hash->{"toolUsePromptTokenCount"}
Aresult
11
00
111 }
112 0 ▶ if ( !defined $output && ( defined $hash->{candidatesTokenCount} || defined $gemini_thoughts ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $output and defined $hash->{"candidatesTokenCount"} || defined $gemini_thoughts) 14 322 336
if (not defined $output and defined $hash->{"candidatesTokenCount"} || defined $gemini_thoughts) 0 0 0
Condition50%6/12
not defined $output and defined $hash->{"candidatesTokenCount"} || defined $gemini_thoughts
!ll&&!rl&&r
2546814
defined $hash->{"candidatesTokenCount"} or defined $gemini_thoughts
l!l&&r!l&&!r
13168
not defined $output and defined $hash->{"candidatesTokenCount"} || defined $gemini_thoughts
!ll&&!rl&&r
000
defined $hash->{"candidatesTokenCount"} or defined $gemini_thoughts
l!l&&r!l&&!r
000
MC/DC100%3/3
not defined $output and defined $hash->{"candidatesTokenCount"} || defined $gemini_thoughts
not defined $output defined $hash->{"candidatesTokenCount"} defined $gemini_thoughts
MC/DC0%0/3
not defined $output and defined $hash->{"candidatesTokenCount"} || defined $gemini_thoughts
not defined $output defined $hash->{"candidatesTokenCount"} defined $gemini_thoughts
Truth table100%4/4
not(A) and B or C
A= not defined $outputB= defined $hash->{"candidatesTokenCount"}C= defined $gemini_thoughts
ABCresult
0XX0
1000
11X1
1011
Truth table0%0/4
not(A) and B or C
A= not defined $outputB= defined $hash->{"candidatesTokenCount"}C= defined $gemini_thoughts
ABCresult
0XX0
1000
11X1
1011
113 0 ▶ $output = ( $hash->{candidatesTokenCount} // 0 ) + ( $gemini_thoughts // 0 );
Condition50%4/8
$hash->{"candidatesTokenCount"} // 0
l!l
131
$gemini_thoughts // 0
l!l
68
$hash->{"candidatesTokenCount"} // 0
l!l
00
$gemini_thoughts // 0
l!l
00
MC/DC100%1/1
$hash->{"candidatesTokenCount"} // 0
$hash->{"candidatesTokenCount"}
MC/DC100%1/1
$gemini_thoughts // 0
$gemini_thoughts
MC/DC0%0/1
$hash->{"candidatesTokenCount"} // 0
$hash->{"candidatesTokenCount"}
MC/DC0%0/1
$gemini_thoughts // 0
$gemini_thoughts
Truth table100%2/2
A
A= $hash->{"candidatesTokenCount"}
Aresult
11
00
Truth table100%2/2
A
A= $gemini_thoughts
Aresult
11
00
Truth table0%0/2
A
A= $hash->{"candidatesTokenCount"}
Aresult
11
00
Truth table0%0/2
A
A= $gemini_thoughts
Aresult
11
00
114 }
115 0 ▶ $total = $hash->{totalTokenCount} if !defined $total && defined $hash->{totalTokenCount};
Branch: 2 branches
BranchTrueFalseTotal
if not defined $total and defined $hash->{"totalTokenCount"} 11 325 336
if not defined $total and defined $hash->{"totalTokenCount"} 0 0 0
Condition50%3/6
not defined $total and defined $hash->{"totalTokenCount"}
!ll&&!rl&&r
19313211
not defined $total and defined $hash->{"totalTokenCount"}
!ll&&!rl&&r
000
MC/DC100%2/2
not defined $total and defined $hash->{"totalTokenCount"}
not defined $total defined $hash->{"totalTokenCount"}
MC/DC0%0/2
not defined $total and defined $hash->{"totalTokenCount"}
not defined $total defined $hash->{"totalTokenCount"}
Truth table100%3/3
not(A) and B
A= not defined $totalB= defined $hash->{"totalTokenCount"}
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not defined $totalB= defined $hash->{"totalTokenCount"}
ABresult
0X0
100
111
116  
117 0 ▶ $input = 0 + ($input // 0);
Condition50%2/4
$input // 0
l!l
30036
$input // 0
l!l
00
MC/DC100%1/1
$input // 0
$input
MC/DC0%0/1
$input // 0
$input
Truth table100%2/2
A
A= $input
Aresult
11
00
Truth table0%0/2
A
A= $input
Aresult
11
00
118 0 ▶ $output = 0 + ($output // 0);
Condition50%2/4
$output // 0
l!l
26868
$output // 0
l!l
00
MC/DC100%1/1
$output // 0
$output
MC/DC0%0/1
$output // 0
$output
Truth table100%2/2
A
A= $output
Aresult
11
00
Truth table0%0/2
A
A= $output
Aresult
11
00
119  
120 # Prompt-cache read/write counts each carry several wire spellings. OpenAI's
121 # Chat wire nests both under prompt_tokens_details (cached_tokens /
122 # cache_write_tokens); the Open-Responses envelope (OpenAI Responses, the
123 # Perplexity Agent API) nests them under input_tokens_details, mixing the
124 # OpenAI read key (cached_tokens) with the Anthropic-named counts
125 # (cache_read_input_tokens / cache_creation_input_tokens); Anthropic reports
126 # them flat; Gemini reports the read count as usageMetadata's
127 # cachedContentTokenCount (Engine::Gemini renames it cached_content_token_count).
128 # The OpenAI Chat nesting wins, then the Responses nesting, then the Anthropic
129 # flat keys, then Gemini's, then the canonical flat cached_tokens that
130 # Engine::AKI emits (karr #125 / #130 / #159 / k197). The read count and the
131 # write count are distinct quantities — the write count is deliberately NOT
132 # folded into cached_tokens. Values are read into lexicals first so a missing
133 # key never autovivifies the caller's hash.
134 0 my $ptd = $hash->{prompt_tokens_details};
135 0 ▶ $ptd = undef unless ref($ptd) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $ptd eq "HASH" 294 42 336
unless ref $ptd eq "HASH" 0 0 0
136 0 my $itd = $hash->{input_tokens_details};
137 0 ▶ $itd = undef unless ref($itd) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $itd eq "HASH" 284 52 336
unless ref $itd eq "HASH" 0 0 0
138  
139 # Whether the cache counts are part of the input count depends on where they
140 # were found, so it is recorded next to them (input_includes_cache, k263). A
141 # count nested in a *_details block breaks the prompt/input total down
142 # (OpenAI Chat, Open-Responses: the captures in t/data/ show input_tokens 8542
143 # beside a 4394 write, 4071 beside a 4068 write); Gemini's
144 # cachedContentTokenCount is part of promptTokenCount; AKI native's
145 # num_cached_tokens is a subset of prompt_length. Anthropic's flat keys are
146 # counted beside input_tokens, not in it.
147 0 my ( $cached, $cached_in_input );
148 0 ▶ if ( $ptd && defined $ptd->{cached_tokens} ) { $cached = $ptd->{cached_tokens}; $cached_in_input = 1 }
Branch: 14 branches
BranchTrueFalseTotal
if ($ptd and defined $ptd->{'cached_tokens'}) { } 42 294 336
elsif ($itd and defined $itd->{'cached_tokens'}) { } 51 243 294
elsif ($itd and defined $itd->{'cache_read_input_tokens'}) { } 0 243 243
elsif (defined $hash->{'cache_read_input_tokens'}) { } 25 218 243
elsif (defined $hash->{'cachedContentTokenCount'}) { } 2 216 218
elsif (defined $hash->{'cached_content_token_count'}) { } 3 213 216
elsif (defined $hash->{'cached_tokens'}) { } 4 209 213
if ($ptd and defined $ptd->{'cached_tokens'}) { } 0 0 0
elsif ($itd and defined $itd->{'cached_tokens'}) { } 0 0 0
elsif ($itd and defined $itd->{'cache_read_input_tokens'}) { } 0 0 0
elsif (defined $hash->{'cache_read_input_tokens'}) { } 0 0 0
elsif (defined $hash->{'cachedContentTokenCount'}) { } 0 0 0
elsif (defined $hash->{'cached_content_token_count'}) { } 0 0 0
elsif (defined $hash->{'cached_tokens'}) { } 0 0 0
Condition33%2/6
$ptd and defined $ptd->{"cached_tokens"}
!ll&&!rl&&r
294042
$ptd and defined $ptd->{"cached_tokens"}
!ll&&!rl&&r
000
MC/DC50%1/2
$ptd and defined $ptd->{"cached_tokens"}
$ptd defined $ptd->{"cached_tokens"}
MC/DC0%0/2
$ptd and defined $ptd->{"cached_tokens"}
$ptd defined $ptd->{"cached_tokens"}
Truth table66%2/3
A and B
A= $ptdB= defined $ptd->{"cached_tokens"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $ptdB= defined $ptd->{"cached_tokens"}
ABresult
0X0
100
111
149 0 ▶ elsif ( $itd && defined $itd->{cached_tokens} ) { $cached = $itd->{cached_tokens}; $cached_in_input = 1 }
Condition50%3/6
$itd and defined $itd->{"cached_tokens"}
!ll&&!rl&&r
242151
$itd and defined $itd->{"cached_tokens"}
!ll&&!rl&&r
000
MC/DC100%2/2
$itd and defined $itd->{"cached_tokens"}
$itd defined $itd->{"cached_tokens"}
MC/DC0%0/2
$itd and defined $itd->{"cached_tokens"}
$itd defined $itd->{"cached_tokens"}
Truth table100%3/3
A and B
A= $itdB= defined $itd->{"cached_tokens"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $itdB= defined $itd->{"cached_tokens"}
ABresult
0X0
100
111
150 0 ▶ elsif ( $itd && defined $itd->{cache_read_input_tokens} ) { $cached = $itd->{cache_read_input_tokens}; $cached_in_input = 1 }
Condition33%2/6
$itd and defined $itd->{"cache_read_input_tokens"}
!ll&&!rl&&r
24210
$itd and defined $itd->{"cache_read_input_tokens"}
!ll&&!rl&&r
000
MC/DC0%0/2
$itd and defined $itd->{"cache_read_input_tokens"}
$itd defined $itd->{"cache_read_input_tokens"}
MC/DC0%0/2
$itd and defined $itd->{"cache_read_input_tokens"}
$itd defined $itd->{"cache_read_input_tokens"}
Truth table66%2/3
A and B
A= $itdB= defined $itd->{"cache_read_input_tokens"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $itdB= defined $itd->{"cache_read_input_tokens"}
ABresult
0X0
100
111
151 0 elsif ( defined $hash->{cache_read_input_tokens} ) { $cached = $hash->{cache_read_input_tokens}; $cached_in_input = 0 }
152 0 elsif ( defined $hash->{cachedContentTokenCount} ) { $cached = $hash->{cachedContentTokenCount}; $cached_in_input = 1 }
153 0 elsif ( defined $hash->{cached_content_token_count} ) { $cached = $hash->{cached_content_token_count}; $cached_in_input = 1 }
154 0 elsif ( defined $hash->{cached_tokens} ) { $cached = $hash->{cached_tokens}; $cached_in_input = 1 }
155  
156 0 my ( $cache_write, $write_in_input );
157 0 ▶ if ( $ptd && defined $ptd->{cache_write_tokens} ) { $cache_write = $ptd->{cache_write_tokens}; $write_in_input = 1 }
Branch: 10 branches
BranchTrueFalseTotal
if ($ptd and defined $ptd->{'cache_write_tokens'}) { } 5 331 336
elsif ($itd and defined $itd->{'cache_write_tokens'}) { } 14 317 331
elsif ($itd and defined $itd->{'cache_creation_input_tokens'}) { } 26 291 317
elsif (defined $hash->{'cache_creation_input_tokens'}) { } 11 280 291
elsif (ref $hash->{'cache_creation'} eq 'HASH') { } 1 279 280
if ($ptd and defined $ptd->{'cache_write_tokens'}) { } 0 0 0
elsif ($itd and defined $itd->{'cache_write_tokens'}) { } 0 0 0
elsif ($itd and defined $itd->{'cache_creation_input_tokens'}) { } 0 0 0
elsif (defined $hash->{'cache_creation_input_tokens'}) { } 0 0 0
elsif (ref $hash->{'cache_creation'} eq 'HASH') { } 0 0 0
Condition50%3/6
$ptd and defined $ptd->{"cache_write_tokens"}
!ll&&!rl&&r
294375
$ptd and defined $ptd->{"cache_write_tokens"}
!ll&&!rl&&r
000
MC/DC100%2/2
$ptd and defined $ptd->{"cache_write_tokens"}
$ptd defined $ptd->{"cache_write_tokens"}
MC/DC0%0/2
$ptd and defined $ptd->{"cache_write_tokens"}
$ptd defined $ptd->{"cache_write_tokens"}
Truth table100%3/3
A and B
A= $ptdB= defined $ptd->{"cache_write_tokens"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $ptdB= defined $ptd->{"cache_write_tokens"}
ABresult
0X0
100
111
158 0 ▶ elsif ( $itd && defined $itd->{cache_write_tokens} ) { $cache_write = $itd->{cache_write_tokens}; $write_in_input = 1 }
Condition50%3/6
$itd and defined $itd->{"cache_write_tokens"}
!ll&&!rl&&r
2793814
$itd and defined $itd->{"cache_write_tokens"}
!ll&&!rl&&r
000
MC/DC100%2/2
$itd and defined $itd->{"cache_write_tokens"}
$itd defined $itd->{"cache_write_tokens"}
MC/DC0%0/2
$itd and defined $itd->{"cache_write_tokens"}
$itd defined $itd->{"cache_write_tokens"}
Truth table100%3/3
A and B
A= $itdB= defined $itd->{"cache_write_tokens"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $itdB= defined $itd->{"cache_write_tokens"}
ABresult
0X0
100
111
159 0 ▶ elsif ( $itd && defined $itd->{cache_creation_input_tokens} ) { $cache_write = $itd->{cache_creation_input_tokens}; $write_in_input = 1 }
Condition50%3/6
$itd and defined $itd->{"cache_creation_input_tokens"}
!ll&&!rl&&r
2791226
$itd and defined $itd->{"cache_creation_input_tokens"}
!ll&&!rl&&r
000
MC/DC100%2/2
$itd and defined $itd->{"cache_creation_input_tokens"}
$itd defined $itd->{"cache_creation_input_tokens"}
MC/DC0%0/2
$itd and defined $itd->{"cache_creation_input_tokens"}
$itd defined $itd->{"cache_creation_input_tokens"}
Truth table100%3/3
A and B
A= $itdB= defined $itd->{"cache_creation_input_tokens"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $itdB= defined $itd->{"cache_creation_input_tokens"}
ABresult
0X0
100
111
160 0 elsif ( defined $hash->{cache_creation_input_tokens} ) { $cache_write = $hash->{cache_creation_input_tokens}; $write_in_input = 0 }
161 elsif ( ref( $hash->{cache_creation} ) eq 'HASH' ) {
162 # Anthropic's per-TTL split (ephemeral_5m / ephemeral_1h), which Moonshot's
163 # /anthropic shim also reports; only read when the flat total is missing.
164 0 my @tiers = grep { defined } map { $hash->{cache_creation}{"ephemeral_${_}_input_tokens"} } qw( 5m 1h );
165 0 ▶ if (@tiers) { $cache_write = 0; $cache_write += $_ for @tiers; $write_in_input = 0 }
Branch: 2 branches
BranchTrueFalseTotal
if (@tiers) 1 0 1
if (@tiers) 0 0 0
166 }
167  
168 # One flag for both counts: every wire above nests both or flattens both. If
169 # a hash ever mixes the two, "not included" wins — pricing a count on top of
170 # input_tokens can overcharge, subtracting it can go below zero.
171 0 ▶ my $includes = defined $cached_in_input ? $cached_in_input : $write_in_input;
Branch: 2 branches
BranchTrueFalseTotal
defined $cached_in_input ? : 127 209 336
defined $cached_in_input ? : 0 0 0
172 0 ▶ $includes = 0 if defined $write_in_input && !$write_in_input;
Branch: 2 branches
BranchTrueFalseTotal
if defined $write_in_input and not $write_in_input 12 324 336
if defined $write_in_input and not $write_in_input 0 0 0
Condition50%3/6
defined $write_in_input and not $write_in_input
!ll&&!rl&&r
2794512
defined $write_in_input and not $write_in_input
!ll&&!rl&&r
000
MC/DC100%2/2
defined $write_in_input and not $write_in_input
defined $write_in_input not $write_in_input
MC/DC0%0/2
defined $write_in_input and not $write_in_input
defined $write_in_input not $write_in_input
Truth table100%3/3
A and not(B)
A= defined $write_in_inputB= not $write_in_input
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= defined $write_in_inputB= not $write_in_input
ABresult
0X0
100
111
173  
174 # A canonical input_includes_cache key in the hash beats the inference from
175 # the spelling. Role::AnthropicCompatible adds it for a shim that spells the
176 # counts the Anthropic way but counts them inside input_tokens (AKIAnthropic,
177 # ADR 0031 / k265). It only applies when a cache count was found.
178 $includes = $hash->{input_includes_cache}
179 0 ▶ if defined $includes && defined $hash->{input_includes_cache};
Branch: 2 branches
BranchTrueFalseTotal
if defined $includes and defined $hash->{"input_includes_cache"} 9 327 336
if defined $includes and defined $hash->{"input_includes_cache"} 0 0 0
Condition50%3/6
defined $includes and defined $hash->{"input_includes_cache"}
!ll&&!rl&&r
2071209
defined $includes and defined $hash->{"input_includes_cache"}
!ll&&!rl&&r
000
MC/DC100%2/2
defined $includes and defined $hash->{"input_includes_cache"}
defined $includes defined $hash->{"input_includes_cache"}
MC/DC0%0/2
defined $includes and defined $hash->{"input_includes_cache"}
defined $includes defined $hash->{"input_includes_cache"}
Truth table100%3/3
A and B
A= defined $includesB= defined $hash->{"input_includes_cache"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $includesB= defined $hash->{"input_includes_cache"}
ABresult
0X0
100
111
180  
181 # The reasoning share of output_tokens (already counted in it on every wire):
182 # OpenAI Chat nests it under completion_tokens_details, Open-Responses under
183 # output_tokens_details, Gemini reports thoughtsTokenCount (k299).
184 0 my $ctd = $hash->{completion_tokens_details};
185 0 my $otd = $hash->{output_tokens_details};
186 0 my $reasoning;
187 0 ▶ if ( ref($ctd) eq 'HASH' && defined $ctd->{reasoning_tokens} ) { $reasoning = $ctd->{reasoning_tokens} }
Branch: 6 branches
BranchTrueFalseTotal
if (ref $ctd eq 'HASH' and defined $ctd->{'reasoning_tokens'}) { } 45 291 336
elsif (ref $otd eq 'HASH' and defined $otd->{'reasoning_tokens'}) { } 18 273 291
elsif (defined $gemini_thoughts) { } 6 267 273
if (ref $ctd eq 'HASH' and defined $ctd->{'reasoning_tokens'}) { } 0 0 0
elsif (ref $otd eq 'HASH' and defined $otd->{'reasoning_tokens'}) { } 0 0 0
elsif (defined $gemini_thoughts) { } 0 0 0
Condition33%2/6
ref $ctd eq "HASH" and defined $ctd->{"reasoning_tokens"}
!ll&&!rl&&r
291045
ref $ctd eq "HASH" and defined $ctd->{"reasoning_tokens"}
!ll&&!rl&&r
000
MC/DC50%1/2
ref $ctd eq "HASH" and defined $ctd->{"reasoning_tokens"}
ref $ctd eq "HASH" defined $ctd->{"reasoning_tokens"}
MC/DC0%0/2
ref $ctd eq "HASH" and defined $ctd->{"reasoning_tokens"}
ref $ctd eq "HASH" defined $ctd->{"reasoning_tokens"}
Truth table66%2/3
A and B
A= ref $ctd eq "HASH"B= defined $ctd->{"reasoning_tokens"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= ref $ctd eq "HASH"B= defined $ctd->{"reasoning_tokens"}
ABresult
0X0
100
111
188 0 ▶ elsif ( ref($otd) eq 'HASH' && defined $otd->{reasoning_tokens} ) { $reasoning = $otd->{reasoning_tokens} }
Condition33%2/6
ref $otd eq "HASH" and defined $otd->{"reasoning_tokens"}
!ll&&!rl&&r
273018
ref $otd eq "HASH" and defined $otd->{"reasoning_tokens"}
!ll&&!rl&&r
000
MC/DC50%1/2
ref $otd eq "HASH" and defined $otd->{"reasoning_tokens"}
ref $otd eq "HASH" defined $otd->{"reasoning_tokens"}
MC/DC0%0/2
ref $otd eq "HASH" and defined $otd->{"reasoning_tokens"}
ref $otd eq "HASH" defined $otd->{"reasoning_tokens"}
Truth table66%2/3
A and B
A= ref $otd eq "HASH"B= defined $otd->{"reasoning_tokens"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= ref $otd eq "HASH"B= defined $otd->{"reasoning_tokens"}
ABresult
0X0
100
111
189 0 elsif ( defined $gemini_thoughts ) { $reasoning = $gemini_thoughts }
190  
191 # What the provider says the request was billed, normalized to USD. xAI
192 # reports it in the usage block as cost_in_usd_ticks (1 USD = 10^10 ticks;
193 # chat/completions, Responses, images, video) and, on Responses, also as
194 # cost_in_nano_usd (1 USD = 10^9); both may be null there. The finer ticks
195 # win. The integers stay verbatim in raw (k354).
196 # The Perplexity Agent API sends usage.cost as an object that names its unit
197 # ({ currency => 'USD', total_cost => ..., input_cost => ... }, the captures
198 # in t/data/perplexity_agent_*); it is read only when the currency is USD and
199 # a total is there. A bare number under the generic name cost (OpenRouter's
200 # credits) carries no unit, so it is not read here: the engine that knows the
201 # unit copies it to the canonical cost_usd key, which is read first (k363).
202 0 my $cost_usd;
203 0 my $cost = $hash->{cost};
204 0 ▶ if ( defined $hash->{cost_usd} && !ref $hash->{cost_usd} && looks_like_number( $hash->{cost_usd} ) ) {
Branch: 8 branches
BranchTrueFalseTotal
if (defined $hash->{'cost_usd'} and not ref $hash->{'cost_usd'} and &looks_like_number($hash->{'cost_usd'})) { } 3 333 336
elsif (defined $hash->{'cost_in_usd_ticks'}) { } 7 326 333
elsif (defined $hash->{'cost_in_nano_usd'}) { } 2 324 326
elsif (ref $cost eq 'HASH' and uc($cost->{'currency'} // '') eq 'USD' and defined $cost->{'total_cost'} and not ref $cost->{'total_cost'} and &looks_like_number($cost->{'total_cost'})) { } 28 592 620
if (defined $hash->{'cost_usd'} and not ref $hash->{'cost_usd'} and &looks_like_number($hash->{'cost_usd'})) { } 0 0 0
elsif (defined $hash->{'cost_in_usd_ticks'}) { } 0 0 0
elsif (defined $hash->{'cost_in_nano_usd'}) { } 0 0 0
elsif (ref $cost eq 'HASH' and uc($cost->{'currency'} // '') eq 'USD' and defined $cost->{'total_cost'} and not ref $cost->{'total_cost'} and &looks_like_number($cost->{'total_cost'})) { } 0 0 0
Condition33%4/12
defined $hash->{"cost_usd"} and not ref $hash->{"cost_usd"} and &looks_like_number($hash->{"cost_usd"})
!ll&&!rl&&r
33303
defined $hash->{"cost_usd"} and not ref $hash->{"cost_usd"}
!ll&&!rl&&r
33303
defined $hash->{"cost_usd"} and not ref $hash->{"cost_usd"} and &looks_like_number($hash->{"cost_usd"})
!ll&&!rl&&r
000
defined $hash->{"cost_usd"} and not ref $hash->{"cost_usd"}
!ll&&!rl&&r
000
MC/DC33%1/3
defined $hash->{"cost_usd"} and not ref $hash->{"cost_usd"} and &looks_like_number($hash->{"cost_usd"})
defined $hash->{"cost_usd"} not ref $hash->{"cost_usd"} &looks_like_number($hash->{"cost_usd"})
MC/DC0%0/3
defined $hash->{"cost_usd"} and not ref $hash->{"cost_usd"} and &looks_like_number($hash->{"cost_usd"})
defined $hash->{"cost_usd"} not ref $hash->{"cost_usd"} &looks_like_number($hash->{"cost_usd"})
Truth table50%2/4
A and not(B) and C
A= defined $hash->{"cost_usd"}B= not ref $hash->{"cost_usd"}C= &looks_like_number($hash->{"cost_usd"})
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and not(B) and C
A= defined $hash->{"cost_usd"}B= not ref $hash->{"cost_usd"}C= &looks_like_number($hash->{"cost_usd"})
ABCresult
0XX0
10X0
1100
1111
205 0 $cost_usd = 0 + $hash->{cost_usd};
206 }
207 0 elsif ( defined $hash->{cost_in_usd_ticks} ) { $cost_usd = $hash->{cost_in_usd_ticks} / 10_000_000_000 }
208 0 elsif ( defined $hash->{cost_in_nano_usd} ) { $cost_usd = $hash->{cost_in_nano_usd} / 1_000_000_000 }
209 ▶ elsif ( ref($cost) eq 'HASH' && uc( $cost->{currency} // '' ) eq 'USD'
Condition42%12/28
ref $cost eq "HASH" and uc($cost->{"currency"} // "") eq "USD" and defined $cost->{"total_cost"} and not ref $cost->{"total_cost"} and &looks_like_number($cost->{"total_cost"})
!ll&&!rl&&r
296028
ref $cost eq "HASH" and uc($cost->{"currency"} // "") eq "USD" and defined $cost->{"total_cost"} and not ref $cost->{"total_cost"}
!ll&&!rl&&r
296028
ref $cost eq "HASH" and uc($cost->{"currency"} // "") eq "USD" and defined $cost->{"total_cost"}
!ll&&!rl&&r
295128
ref $cost eq "HASH" and uc($cost->{"currency"} // "") eq "USD"
!ll&&!rl&&r
293229
$cost->{"currency"} // ""
l!l
301
ref $cost eq "HASH" and uc($cost->{"currency"} // "") eq "USD" and defined $cost->{"total_cost"} and not ref $cost->{"total_cost"} and &looks_like_number($cost->{"total_cost"})
!ll&&!rl&&r
000
ref $cost eq "HASH" and uc($cost->{"currency"} // "") eq "USD" and defined $cost->{"total_cost"} and not ref $cost->{"total_cost"}
!ll&&!rl&&r
000
ref $cost eq "HASH" and uc($cost->{"currency"} // "") eq "USD" and defined $cost->{"total_cost"}
!ll&&!rl&&r
000
ref $cost eq "HASH" and uc($cost->{"currency"} // "") eq "USD"
!ll&&!rl&&r
000
$cost->{"currency"} // ""
l!l
00
MC/DC60%3/5
ref $cost eq "HASH" and uc($cost->{"currency"} // "") eq "USD" and defined $cost->{"total_cost"} and not ref $cost->{"total_cost"} and &looks_like_number($cost->{"total_cost"})
ref $cost eq "HASH" $cost->{"currency"} defined $cost->{"total_cost"} not ref $cost->{"total_cost"} &looks_like_number($cost->{"total_cost"})
MC/DC0%0/5
ref $cost eq "HASH" and uc($cost->{"currency"} // "") eq "USD" and defined $cost->{"total_cost"} and not ref $cost->{"total_cost"} and &looks_like_number($cost->{"total_cost"})
ref $cost eq "HASH" $cost->{"currency"} defined $cost->{"total_cost"} not ref $cost->{"total_cost"} &looks_like_number($cost->{"total_cost"})
Truth table66%4/6
A and B and C and not(D) and E
A= ref $cost eq "HASH"B= $cost->{"currency"}C= defined $cost->{"total_cost"}D= not ref $cost->{"total_cost"}E= &looks_like_number($cost->{"total_cost"})
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
Truth table0%0/6
A and B and C and not(D) and E
A= ref $cost eq "HASH"B= $cost->{"currency"}C= defined $cost->{"total_cost"}D= not ref $cost->{"total_cost"}E= &looks_like_number($cost->{"total_cost"})
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
210 && defined $cost->{total_cost} && !ref $cost->{total_cost} && looks_like_number( $cost->{total_cost} ) ) {
211 0 $cost_usd = 0 + $cost->{total_cost};
212 }
213  
214 0 my %args = ( input_tokens => $input, output_tokens => $output );
215 0 ▶ $args{total_tokens} = 0 + $total if defined $total;
Branch: 2 branches
BranchTrueFalseTotal
if defined $total 204 132 336
if defined $total 0 0 0
216 0 ▶ $args{cached_tokens} = 0 + $cached if defined $cached;
Branch: 2 branches
BranchTrueFalseTotal
if defined $cached 127 209 336
if defined $cached 0 0 0
217 0 ▶ $args{cache_write_tokens} = 0 + $cache_write if defined $cache_write;
Branch: 2 branches
BranchTrueFalseTotal
if defined $cache_write 57 279 336
if defined $cache_write 0 0 0
218 0 ▶ $args{reasoning_tokens} = 0 + $reasoning if defined $reasoning;
Branch: 2 branches
BranchTrueFalseTotal
if defined $reasoning 69 267 336
if defined $reasoning 0 0 0
219 0 ▶ $args{cost_usd} = $cost_usd if defined $cost_usd;
Branch: 2 branches
BranchTrueFalseTotal
if defined $cost_usd 40 296 336
if defined $cost_usd 0 0 0
220 0 ▶ $args{input_includes_cache} = $includes ? 1 : 0 if defined $includes;
Branch: 4 branches
BranchTrueFalseTotal
if defined $includes 129 207 336
$includes ? : 111 18 129
if defined $includes 0 0 0
$includes ? : 0 0 0
221 0 $args{raw} = $hash;
222 0 return $class->new(%args);
223 }
224  
225 # Build a Usage from any response shape: a Langertha::Response, a HashRef
226 # with a usage key, or undef.
227 5 ▶sub from_response {
Subroutine from_response: called CC=11 SCAR=38.6
Pod documented
228 0 ▶ my ($class, $response) = @_;
Subroutine from_response: not called CC=11 SCAR=38.6
Pod documented
229 0 ▶ return $class->new unless $response;
Branch: 2 branches
BranchTrueFalseTotal
unless $response 0 5 5
unless $response 0 0 0
230  
231 0 ▶ if ( blessed($response) && $response->isa('Langertha::Response') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($response) and $response->isa("Langertha::Response")) 1 4 5
if (builtin::blessed($response) and $response->isa("Langertha::Response")) 0 0 0
Condition33%2/6
builtin::blessed($response) and $response->isa("Langertha::Response")
!ll&&!rl&&r
401
builtin::blessed($response) and $response->isa("Langertha::Response")
!ll&&!rl&&r
000
MC/DC50%1/2
builtin::blessed($response) and $response->isa("Langertha::Response")
builtin::blessed($response) $response->isa("Langertha::Response")
MC/DC0%0/2
builtin::blessed($response) and $response->isa("Langertha::Response")
builtin::blessed($response) $response->isa("Langertha::Response")
Truth table66%2/3
A and B
A= builtin::blessed($response)B= $response->isa("Langertha::Response")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($response)B= $response->isa("Langertha::Response")
ABresult
0X0
100
111
232 0 ▶ my $usage = $response->has_usage ? $response->usage : undef;
Branch: 2 branches
BranchTrueFalseTotal
$response->has_usage ? : 0 1 1
$response->has_usage ? : 0 0 0
233 0 ▶ return $usage if blessed($usage) && $usage->isa('Langertha::Usage');
Branch: 2 branches
BranchTrueFalseTotal
if builtin::blessed($usage) and $usage->isa("Langertha::Usage") 0 1 1
if builtin::blessed($usage) and $usage->isa("Langertha::Usage") 0 0 0
Condition16%1/6
builtin::blessed($usage) and $usage->isa("Langertha::Usage")
!ll&&!rl&&r
100
builtin::blessed($usage) and $usage->isa("Langertha::Usage")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($usage) and $usage->isa("Langertha::Usage")
builtin::blessed($usage) $usage->isa("Langertha::Usage")
MC/DC0%0/2
builtin::blessed($usage) and $usage->isa("Langertha::Usage")
builtin::blessed($usage) $usage->isa("Langertha::Usage")
Truth table33%1/3
A and B
A= builtin::blessed($usage)B= $usage->isa("Langertha::Usage")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($usage)B= $usage->isa("Langertha::Usage")
ABresult
0X0
100
111
234 0 ▶ return $class->from_hash( $usage || {} );
Condition25%1/4
$usage || {}
l!l
01
$usage || {}
l!l
00
MC/DC0%0/1
$usage || {}
$usage
MC/DC0%0/1
$usage || {}
$usage
Truth table50%1/2
A
A= $usage
Aresult
11
00
Truth table0%0/2
A
A= $usage
Aresult
11
00
235 }
236 0 ▶ if ( ref($response) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $response eq "HASH") 4 0 4
if (ref $response eq "HASH") 0 0 0
237 0 ▶ return $class->from_raw($response) // $class->from_hash( $response->{usage} || {} );
Condition30%3/10
$class->from_raw($response) // $class->from_hash($response->{"usage"} || {})
l!l&&r!l&&!r
310
$response->{'usage'} || {}
l!l
01
$class->from_raw($response) // $class->from_hash($response->{"usage"} || {})
l!l&&r!l&&!r
000
$response->{'usage'} || {}
l!l
00
MC/DC0%0/2
$class->from_raw($response) // $class->from_hash($response->{"usage"} || {})
$class->from_raw($response) $class->from_hash($response->{"usage"} || {})
MC/DC0%0/1
$response->{'usage'} || {}
$response->{'usage'}
MC/DC0%0/2
$class->from_raw($response) // $class->from_hash($response->{"usage"} || {})
$class->from_raw($response) $class->from_hash($response->{"usage"} || {})
MC/DC0%0/1
$response->{'usage'} || {}
$response->{'usage'}
Truth table66%2/3
A // B
A= $class->from_raw($response)B= $class->from_hash($response->{"usage"} || {})
ABresult
1X1
011
000
Truth table50%1/2
A
A= $response->{'usage'}
Aresult
11
00
Truth table0%0/3
A // B
A= $class->from_raw($response)B= $class->from_hash($response->{"usage"} || {})
ABresult
1X1
011
000
Truth table0%0/2
A
A= $response->{'usage'}
Aresult
11
00
238 }
239 0 return $class->new;
240 }
241  
242 # Build a Usage from a raw decoded provider response body; undef when the body
243 # reports no usage. Probes with lexicals so the caller's body never autovivifies.
244 61 ▶sub from_raw {
Subroutine from_raw: called CC=13 SCAR=36.3
Pod documented
245 0 ▶ my ($class, $data) = @_;
Subroutine from_raw: not called CC=13 SCAR=36.3
Pod documented
246 0 ▶ return undef unless ref($data) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $data eq "HASH" 2 59 61
unless ref $data eq "HASH" 0 0 0
247 0 for my $key (qw( usage usageMetadata )) {
248 0 ▶ return $class->from_hash( $data->{$key} ) if ref( $data->{$key} ) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
if ref $data->{$key} eq "HASH" 38 47 85
if ref $data->{$key} eq "HASH" 0 0 0
249 }
250 0 my $envelope = $data->{response};
251 0 ▶ if ( ref($envelope) eq 'HASH' && ref( $envelope->{usage} ) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $envelope eq "HASH" and ref $envelope->{"usage"} eq "HASH") 1 20 21
if (ref $envelope eq "HASH" and ref $envelope->{"usage"} eq "HASH") 0 0 0
Condition33%2/6
ref $envelope eq "HASH" and ref $envelope->{"usage"} eq "HASH"
!ll&&!rl&&r
2001
ref $envelope eq "HASH" and ref $envelope->{"usage"} eq "HASH"
!ll&&!rl&&r
000
MC/DC50%1/2
ref $envelope eq "HASH" and ref $envelope->{"usage"} eq "HASH"
ref $envelope eq "HASH" ref $envelope->{"usage"} eq "HASH"
MC/DC0%0/2
ref $envelope eq "HASH" and ref $envelope->{"usage"} eq "HASH"
ref $envelope eq "HASH" ref $envelope->{"usage"} eq "HASH"
Truth table66%2/3
A and B
A= ref $envelope eq "HASH"B= ref $envelope->{"usage"} eq "HASH"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= ref $envelope eq "HASH"B= ref $envelope->{"usage"} eq "HASH"
ABresult
0X0
100
111
252 0 return $class->from_hash( $envelope->{usage} );
253 }
254 # Ollama native: top-level counts. A zero count is "not reported", as in
255 # Engine::Ollama's chat_response, so a body with only zeros has no usage.
256 0 ▶ my %ollama = map { $data->{$_} ? ( $_ => $data->{$_} ) : () } qw( prompt_eval_count eval_count );
Branch: 2 branches
BranchTrueFalseTotal
$data->{$_} ? : 6 34 40
$data->{$_} ? : 0 0 0
257 0 ▶ return $class->from_hash( \%ollama ) if %ollama;
Branch: 2 branches
BranchTrueFalseTotal
if %ollama 4 16 20
if %ollama 0 0 0
258 # AKI native: top-level counts under their own names (Engine::AKI's
259 # chat_response reads the same keys, by definedness).
260 0 ▶ my %aki = map { defined $data->{$_} ? ( $_ => $data->{$_} ) : () }
Branch: 2 branches
BranchTrueFalseTotal
defined $data->{$_} ? : 7 41 48
defined $data->{$_} ? : 0 0 0
261 qw( prompt_length num_generated_tokens num_cached_tokens );
262 0 ▶ if (%aki) {
Branch: 2 branches
BranchTrueFalseTotal
if (%aki) 3 13 16
if (%aki) 0 0 0
263 return $class->new(
264 input_tokens => 0 + ( $aki{prompt_length} // 0 ),
265 output_tokens => 0 + ( $aki{num_generated_tokens} // 0 ),
266 exists $aki{num_cached_tokens}
267 0 ▶ ? ( cached_tokens => 0 + $aki{num_cached_tokens}, input_includes_cache => 1 ) : (),
Branch: 2 branches
BranchTrueFalseTotal
exists $aki{'num_cached_tokens'} ? : 3 0 3
exists $aki{'num_cached_tokens'} ? : 0 0 0
Condition50%4/8
$aki{"prompt_length"} // 0
l!l
21
$aki{"num_generated_tokens"} // 0
l!l
21
$aki{"prompt_length"} // 0
l!l
00
$aki{"num_generated_tokens"} // 0
l!l
00
MC/DC100%1/1
$aki{"prompt_length"} // 0
$aki{"prompt_length"}
MC/DC100%1/1
$aki{"num_generated_tokens"} // 0
$aki{"num_generated_tokens"}
MC/DC0%0/1
$aki{"prompt_length"} // 0
$aki{"prompt_length"}
MC/DC0%0/1
$aki{"num_generated_tokens"} // 0
$aki{"num_generated_tokens"}
Truth table100%2/2
A
A= $aki{"prompt_length"}
Aresult
11
00
Truth table100%2/2
A
A= $aki{"num_generated_tokens"}
Aresult
11
00
Truth table0%0/2
A
A= $aki{"prompt_length"}
Aresult
11
00
Truth table0%0/2
A
A= $aki{"num_generated_tokens"}
Aresult
11
00
268 raw => \%aki,
269 );
270 }
271 0 return undef;
272 }
273  
274  
275 # Immutable merge — returns a new Usage that is the sum of self + other.
276 # Cache and reasoning counts are summed (undef only when neither side reported one). When
277 # one side counts its cache inside input_tokens (flag 1, or undef with counts)
278 # and the other beside it (flag 0), the beside side's cache counts are added to
279 # its input_tokens first, so the sum is "inside" throughout and priced without
280 # loss (k265).
281 15 ▶sub merge {
Subroutine merge: called CC=17 SCAR=41.6
Pod documented
282 0 ▶ my ($self, $other) = @_;
Subroutine merge: not called CC=17 SCAR=41.6
Pod documented
283 0 ▶ return $self unless $other;
Branch: 2 branches
BranchTrueFalseTotal
unless $other 0 15 15
unless $other 0 0 0
284 0 my %cache;
285 0 for my $count (qw( cached_tokens cache_write_tokens reasoning_tokens )) {
286 0 my @seen = grep { defined } $self->$count, $other->$count;
287 0 ▶ next unless @seen;
Branch: 2 branches
BranchTrueFalseTotal
unless @seen 30 15 45
unless @seen 0 0 0
288 0 $cache{$count} = 0;
289 0 $cache{$count} += $_ for @seen;
290 }
291 0 ▶ my @reported = grep { defined $_->cached_tokens || defined $_->cache_write_tokens } $self, $other;
Condition33%2/6
defined $_->cached_tokens || defined $_->cache_write_tokens
l!l&&r!l&&!r
13017
defined $_->cached_tokens || defined $_->cache_write_tokens
l!l&&r!l&&!r
000
MC/DC50%1/2
defined $_->cached_tokens || defined $_->cache_write_tokens
defined $_->cached_tokens defined $_->cache_write_tokens
MC/DC0%0/2
defined $_->cached_tokens || defined $_->cache_write_tokens
defined $_->cached_tokens defined $_->cache_write_tokens
Truth table66%2/3
A || B
A= defined $_->cached_tokensB= defined $_->cache_write_tokens
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= defined $_->cached_tokensB= defined $_->cache_write_tokens
ABresult
1X1
011
000
292 0 ▶ my @beside = grep { defined $_->input_includes_cache && !$_->input_includes_cache } @reported;
Condition50%3/6
defined $_->input_includes_cache && !$_->input_includes_cache
!ll&&!rl&&r
247
defined $_->input_includes_cache && !$_->input_includes_cache
!ll&&!rl&&r
000
MC/DC100%2/2
defined $_->input_includes_cache && !$_->input_includes_cache
defined $_->input_includes_cache !$_->input_includes_cache
MC/DC0%0/2
defined $_->input_includes_cache && !$_->input_includes_cache
defined $_->input_includes_cache !$_->input_includes_cache
Truth table100%3/3
A && not(B)
A= defined $_->input_includes_cacheB= !$_->input_includes_cache
ABresult
0X0
100
111
Truth table0%0/3
A && not(B)
A= defined $_->input_includes_cacheB= !$_->input_includes_cache
ABresult
0X0
100
111
293 0 my $input = $self->input_tokens + $other->input_tokens;
294 0 my $includes;
295 0 ▶ if ( @beside && @beside < @reported ) {
Branch: 6 branches
BranchTrueFalseTotal
if (@beside and @beside < @reported) { } 4 11 15
elsif (@beside) { } 2 9 11
elsif (@reported) { } 1 8 9
if (@beside and @beside < @reported) { } 0 0 0
elsif (@beside) { } 0 0 0
elsif (@reported) { } 0 0 0
Condition50%3/6
@beside and @beside < @reported
!ll&&!rl&&r
924
@beside and @beside < @reported
!ll&&!rl&&r
000
MC/DC100%2/2
@beside and @beside < @reported
@beside @beside < @reported
MC/DC0%0/2
@beside and @beside < @reported
@beside @beside < @reported
Truth table100%3/3
A and B
A= @besideB= @beside < @reported
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= @besideB= @beside < @reported
ABresult
0X0
100
111
296 0 ▶ $input += ( $_->cached_tokens // 0 ) + ( $_->cache_write_tokens // 0 ) for @beside;
Condition25%2/8
$_->cached_tokens // 0
l!l
40
$_->cache_write_tokens // 0
l!l
40
$_->cached_tokens // 0
l!l
00
$_->cache_write_tokens // 0
l!l
00
MC/DC0%0/1
$_->cached_tokens // 0
$_->cached_tokens
MC/DC0%0/1
$_->cache_write_tokens // 0
$_->cache_write_tokens
MC/DC0%0/1
$_->cached_tokens // 0
$_->cached_tokens
MC/DC0%0/1
$_->cache_write_tokens // 0
$_->cache_write_tokens
Truth table50%1/2
A
A= $_->cached_tokens
Aresult
11
00
Truth table50%1/2
A
A= $_->cache_write_tokens
Aresult
11
00
Truth table0%0/2
A
A= $_->cached_tokens
Aresult
11
00
Truth table0%0/2
A
A= $_->cache_write_tokens
Aresult
11
00
297 0 $includes = 1;
298 }
299 elsif (@beside) {
300 0 $includes = 0;
301 }
302 elsif (@reported) {
303 # All inside: 1 when every side said so, else undef (which reads as inside).
304 0 ▶ $includes = ( grep { !defined $_->input_includes_cache } @reported ) ? undef : 1;
Branch: 2 branches
BranchTrueFalseTotal
grep({not defined $_->input_includes_cache;} @reported) ? : 0 1 1
grep({not defined $_->input_includes_cache;} @reported) ? : 0 0 0
305 }
306 # A cost is only known for the sum when both sides report one: a partial
307 # sum would read as the whole bill.
308 0 my @costs = grep { defined } $self->cost_usd, $other->cost_usd;
309 0 ▶ return ref($self)->new(
Branch: 4 branches
BranchTrueFalseTotal
@costs == 2 ? : 1 14 15
defined $includes ? : 7 8 15
@costs == 2 ? : 0 0 0
defined $includes ? : 0 0 0
310 input_tokens => $input,
311 output_tokens => $self->output_tokens + $other->output_tokens,
312 %cache,
313 @costs == 2 ? ( cost_usd => $costs[0] + $costs[1] ) : (),
314 defined $includes ? ( input_includes_cache => $includes ) : (),
315 );
316 }
317  
318  
319 # Canonical hash representation (input_tokens / output_tokens / total_tokens).
320 14 ▶sub to_hash {
Subroutine to_hash: called CC=1 SCAR=1.2
Pod undocumented
321 0 ▶ my ($self) = @_;
Subroutine to_hash: not called CC=1 SCAR=1.2
Pod undocumented
322 return {
323 0 input_tokens => $self->input_tokens,
324 output_tokens => $self->output_tokens,
325 total_tokens => $self->total_tokens,
326 };
327 }
328  
329 # Backing for the %{} overload. When a provider hash was captured (raw), that
330 # hash is returned verbatim so existing `$response->usage->{...}` callers keep
331 # seeing the exact engine-normalized keys they always saw — including
332 # provider-specific extras (cache tokens, token details) and the deliberate
333 # absence of keys the engine normalized away. Without a raw hash (a Usage
334 # constructed directly), the canonical to_hash shape is returned.
335 3266 ▶sub _as_hash {
Subroutine _as_hash: called CC=8 SCAR=32.1
336 0 ▶ my ($self) = @_;
Subroutine _as_hash: not called CC=8 SCAR=32.1
337 0 ▶ my $d = $DATA{$self} || {};
Condition50%2/4
$DATA{$self} || {}
l!l
543212
$DATA{$self} || {}
l!l
00
MC/DC100%1/1
$DATA{$self} || {}
$DATA{$self}
MC/DC0%0/1
$DATA{$self} || {}
$DATA{$self}
Truth table100%2/2
A
A= $DATA{$self}
Aresult
11
00
Truth table0%0/2
A
A= $DATA{$self}
Aresult
11
00
338 0 ▶ return $d->{raw} if $d->{raw};
Branch: 2 branches
BranchTrueFalseTotal
if $d->{"raw"} 54 3212 3266
if $d->{"raw"} 0 0 0
339 return {
340 input_tokens => $d->{input_tokens} // 0,
341 output_tokens => $d->{output_tokens} // 0,
342 0 ▶ total_tokens => $d->{total_tokens} // ( ( $d->{input_tokens} // 0 ) + ( $d->{output_tokens} // 0 ) ),
Condition25%5/20
$d->{"input_tokens"} // 0
l!l
03212
$d->{"output_tokens"} // 0
l!l
03212
$d->{"total_tokens"} // ($d->{"input_tokens"} // 0) + ($d->{"output_tokens"} // 0)
l!l
03212
$d->{"input_tokens"} // 0
l!l
03212
$d->{"output_tokens"} // 0
l!l
03212
$d->{"input_tokens"} // 0
l!l
00
$d->{"output_tokens"} // 0
l!l
00
$d->{"total_tokens"} // ($d->{"input_tokens"} // 0) + ($d->{"output_tokens"} // 0)
l!l
00
$d->{"input_tokens"} // 0
l!l
00
$d->{"output_tokens"} // 0
l!l
00
MC/DC0%0/1
$d->{"output_tokens"} // 0
$d->{"output_tokens"}
MC/DC0%0/1
$d->{"total_tokens"} // ($d->{"input_tokens"} // 0) + ($d->{"output_tokens"} // 0)
$d->{"total_tokens"}
MC/DC0%0/1
$d->{"output_tokens"} // 0
$d->{"output_tokens"}
MC/DC0%0/1
$d->{"output_tokens"} // 0
$d->{"output_tokens"}
MC/DC0%0/1
$d->{"total_tokens"} // ($d->{"input_tokens"} // 0) + ($d->{"output_tokens"} // 0)
$d->{"total_tokens"}
MC/DC0%0/1
$d->{"output_tokens"} // 0
$d->{"output_tokens"}
Truth table50%1/2
A
A= $d->{"output_tokens"}
Aresult
11
00
Truth table50%1/2
A
A= $d->{"total_tokens"}
Aresult
11
00
Truth table50%1/2
A
A= $d->{"output_tokens"}
Aresult
11
00
Truth table0%0/2
A
A= $d->{"output_tokens"}
Aresult
11
00
Truth table0%0/2
A
A= $d->{"total_tokens"}
Aresult
11
00
Truth table0%0/2
A
A= $d->{"output_tokens"}
Aresult
11
00
343 };
344 }
345  
346 # Make the object transparent to any JSON encoder configured with
347 # convert_blessed => 1 (the house default, see Langertha::Plugin::Langfuse).
348 # to_hash is the complete canonical representation, so this is a plain
349 # delegator — nothing is dropped.
350 11 ▶sub TO_JSON { shift->to_hash }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod undocumented
351  
352 2 ▶sub to_openai_format {
Subroutine to_openai_format: called CC=1 SCAR=0.0
Pod undocumented
353 2 my ($self) = @_;
354 return {
355 2 prompt_tokens => $self->input_tokens,
356 completion_tokens => $self->output_tokens,
357 total_tokens => $self->total_tokens,
358 };
359 }
360  
361 2 ▶sub to_anthropic_format {
Subroutine to_anthropic_format: called CC=1 SCAR=0.0
Pod undocumented
362 2 my ($self) = @_;
363 return {
364 2 input_tokens => $self->input_tokens,
365 output_tokens => $self->output_tokens,
366 };
367 }
368  
369 2 ▶sub to_ollama_format {
Subroutine to_ollama_format: called CC=1 SCAR=0.0
Pod undocumented
370 2 my ($self) = @_;
371 return {
372 2 prompt_eval_count => $self->input_tokens,
373 eval_count => $self->output_tokens,
374 };
375 }
376  
377  
378 200 __PACKAGE__->meta->make_immutable;
379 200 1;
380  
381 __END__
↑ Summary bin/langertha_image »