blib/lib/Langertha/Role/PromptCache.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran 100% 12 / 12 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 80.0% 4 / 5 Podpod 100% 2 / 2 totaltotal 93.1% 41 / 44 CC 8 SCAR 17.5
CC
8
Cov
94%
prompt_cache_kwargs_for
8.0
prompt_cache_kwargs
2.0
BEGIN
1.0
CRAP 5.8
« blib/lib/Langertha/Metrics.pm ↑ Summary blib/lib/Langertha/RateLimit.pm »
1 package Langertha::Role::PromptCache;
2 # ABSTRACT: Role for an engine with a request-side prompt-caching control
3 160 our $VERSION = '0.503';
4 160 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 160 ▶use Langertha::PromptCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 160 has prompt_cache => (
8 is => 'ro',
9 isa => 'Bool',
10 default => 0,
11 );
12  
13  
14 160 has prompt_cache_ttl => (
15 is => 'ro',
16 isa => 'Str',
17 predicate => 'has_prompt_cache_ttl',
18 );
19  
20  
21 160 has prompt_cache_key => (
22 is => 'ro',
23 isa => 'Str',
24 predicate => 'has_prompt_cache_key',
25 );
26  
27  
28 160 has cache_wire_format => (
29 is => 'ro',
30 isa => 'Str',
31 lazy => 1,
32 builder => '_build_cache_wire_format',
33 );
34  
35 # Defaults to the OpenAI dialect; AnthropicBase overrides the builder. This role
36 # is composed only on OpenAIBase and AnthropicBase (the two providers with a
37 # real request-side knob), so those are the only two formats.
38 9 ▶sub _build_cache_wire_format { 'openai' }
Subroutine _build_cache_wire_format: called CC=1 SCAR=0.0
39  
40  
41 2234 ▶sub prompt_cache_kwargs_for {
Subroutine prompt_cache_kwargs_for: called CC=8 SCAR=20.8
Pod documented
42 2234 my ( $self, %args ) = @_;
43  
44 # Per-request controls (chat_f, karr #46) beat the engine attributes on a
45 # per-key basis: %args may carry prompt_cache / prompt_cache_ttl /
46 # prompt_cache_key, and any key it does not carry falls back to the
47 # configured attribute.
48 2234 ▶ my %merged = (
Branch: 2 branches
BranchTrueFalseTotal
$self->has_prompt_cache_ttl ? : 1 2233 2234
$self->has_prompt_cache_key ? : 20 2214 2234
49 ( prompt_cache => $self->prompt_cache ),
50 ( $self->has_prompt_cache_ttl ? ( prompt_cache_ttl => $self->prompt_cache_ttl ) : () ),
51 ( $self->has_prompt_cache_key ? ( prompt_cache_key => $self->prompt_cache_key ) : () ),
52 %args,
53 );
54 # The wire agrees with the capability registry for the routing key (karr
55 # #200, ADR 0009): an engine that clears prompt_cache_key does not send it.
56 # prompt_cache is deliberately not gated -- the family-wide flag would drop
57 # cache_control under an explicit cache_wire_format => 'anthropic' override.
58 2234 ▶ delete $merged{prompt_cache_key} unless $self->supports('prompt_cache_key');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->supports("prompt_cache_key") 898 1336 2234
59 2234 ▶ return () unless $merged{prompt_cache} || defined $merged{prompt_cache_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $merged{"prompt_cache"} or defined $merged{"prompt_cache_key"} 2214 20 2234
Condition100%3/3
$merged{"prompt_cache"} or defined $merged{"prompt_cache_key"}
l!l&&r!l&&!r
9112214
MC/DC100%2/2
$merged{"prompt_cache"} or defined $merged{"prompt_cache_key"}
$merged{"prompt_cache"} defined $merged{"prompt_cache_key"}
Truth table100%3/3
A or B
A= $merged{"prompt_cache"}B= defined $merged{"prompt_cache_key"}
ABresult
1X1
011
000
60 return Langertha::PromptCache->new(
61 enable => $merged{prompt_cache},
62 ( defined $merged{prompt_cache_ttl} ? ( ttl => $merged{prompt_cache_ttl} ) : () ),
63 20 ▶ ( defined $merged{prompt_cache_key} ? ( key => $merged{prompt_cache_key} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
defined $merged{'prompt_cache_ttl'} ? : 1 19 20
defined $merged{'prompt_cache_key'} ? : 11 9 20
64 )->to( $self->cache_wire_format );
65 }
66  
67  
68 0 ▶sub prompt_cache_kwargs {
Subroutine prompt_cache_kwargs: not called CC=1 SCAR=6.9
Pod documented
69 0 my ( $self ) = @_;
70 0 return $self->prompt_cache_kwargs_for;
71 }
72  
73  
74  
75 160 1;
76  
77 __END__
78  
79 =pod
80  
81 =encoding UTF-8
82  
83 =head1 NAME
84  
85 Langertha::Role::PromptCache - Role for an engine with a request-side prompt-caching control
86  
87 =head1 VERSION
88  
89 version 0.503
90  
91 =head2 prompt_cache
92  
93 Enable an Anthropic C<cache_control> breakpoint on the request (the top-level
94 auto-place form). Defaults off. No effect on the OpenAI wire, where caching is
95 automatic — see L</prompt_cache_key> for the only OpenAI-side lever.
96  
97 The top-level C<cache_control> Langertha emits is Anthropic's documented
98 "automatic caching" form: the system applies the cache breakpoint to the last
99 cacheable block and it consumes one of the four available breakpoints. This is
100 the intended shape — do not "fix" it into per-block breakpoints.
101  
102 B<Turning C<prompt_cache> on does not prove a cache write happened.> The minimum
103 cacheable prefix is model-dependent (roughly 512–4096 tokens depending on the
104 model), and Anthropic silently processes a shorter prompt B<without> caching —
105 no error is returned. A 200 response therefore says nothing; only
106 C<< $response->usage->{cache_creation_input_tokens} >> /
107 C<cache_read_input_tokens> confirm the cache was actually used.
108  
109 =head2 prompt_cache_ttl
110  
111 Optional Anthropic cache time-to-live: C<5m> (the current default when unset)
112 or C<1h>. Only meaningful together with L</prompt_cache>; the C<1h> window
113 requires this set explicitly.
114  
115 =head2 prompt_cache_key
116  
117 Optional OpenAI C<prompt_cache_key> routing hint. OpenAI prompt caching is
118 automatic; this only steers which cache shard is used. No effect on the
119 Anthropic wire. Sent only where C<< $engine->supports('prompt_cache_key') >>;
120 the self-hosted OpenAI-compatible engines (vLLM, SGLang, llama.cpp, Ollama,
121 LM Studio) do not advertise it and use L<Langertha::Role::RuntimeKnobs> for
122 their own prefix-cache controls.
123  
124 =head2 cache_wire_format
125  
126 cache_wire_format => 'anthropic'
127  
128 The per-engine enum naming which caching dialect this engine speaks —
129 C<openai> | C<anthropic>. Drives the value-object dispatch in
130 L</prompt_cache_kwargs>. The default follows the engine base-class hierarchy:
131 C<OpenAIBase> leaves it at C<openai>, C<AnthropicBase> overrides to
132 C<anthropic>.
133  
134 =head2 prompt_cache_kwargs_for
135  
136 my %kwargs = $engine->prompt_cache_kwargs_for( prompt_cache => 1 );
137  
138 Returns the body kwargs to merge into a chat request for the caching control,
139 serialized for L</cache_wire_format> via L<Langertha::PromptCache>. C<%args> may
140 carry C<prompt_cache>, C<prompt_cache_ttl> and/or C<prompt_cache_key>; keys it
141 does not carry fall back to the engine attributes, so a per-request control
142 (chat_f, karr #46) beats the configured attribute on a per-key basis. Empty
143 list when nothing applies to the engine's wire (caching off / no key).
144  
145 A C<prompt_cache_key> the engine does not advertise
146 (C<< $engine->supports('prompt_cache_key') >> false, e.g. on the self-hosted
147 OpenAI-compatible servers) is dropped silently, so the request body never
148 carries a routing key the capability registry says the wire does not honor.
149 C<prompt_cache> is not gated, so an explicit C<cache_wire_format> override
150 keeps emitting C<cache_control>.
151  
152 =head2 prompt_cache_kwargs
153  
154 my %kwargs = $engine->prompt_cache_kwargs;
155  
156 Returns the body kwargs to merge into a chat request for the configured caching
157 options, serialized for L</cache_wire_format> via L<Langertha::PromptCache>.
158 Empty list when nothing applies to the engine's wire (caching off / no key).
159 Delegates to L</prompt_cache_kwargs_for> with no per-request overrides.
160  
161 =head1 SEE ALSO
162  
163 =over
164  
165 =item * L<Langertha::PromptCache> - The value object this role dispatches to
166  
167 =item * L<Langertha::Role::Capabilities> - Where C<prompt_cache> / C<prompt_cache_key> are registered
168  
169 =item * L<Langertha::Role::ReasoningEffort> - Sibling request-side reasoning control
170  
171 =back
172  
173 =head1 SUPPORT
174  
175 =head2 Issues
176  
177 Please report bugs and feature requests on GitHub at
178 L<https://github.com/Getty/langertha/issues>.
179  
180 =head2 IRC
181  
182 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
183  
184 =head1 CONTRIBUTING
185  
186 Contributions are welcome! Please fork the repository and submit a pull request.
187  
188 =head1 AUTHOR
189  
190 Torsten Raudssus <getty@cpan.org>
191  
192 =head1 COPYRIGHT AND LICENSE
193  
194 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
195  
196 This is free software; you can redistribute it and/or modify it under
197 the same terms as the Perl 5 programming language system itself.
198  
199 =cut
« blib/lib/Langertha/Metrics.pm ↑ Summary blib/lib/Langertha/RateLimit.pm »