| 1 | package Langertha::PromptCache; | ||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Immutable prompt-caching request control with cross-provider conversion | ||||||||||||||||||||||||||||||
| 3 | 160 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||||
| 4 | 160 | ▶ | use Moose; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 160 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||
| 8 | 160 | has enable => ( | |||||||||||||||||||||||||||||
| 9 | is => 'ro', | ||||||||||||||||||||||||||||||
| 10 | isa => 'Bool', | ||||||||||||||||||||||||||||||
| 11 | default => 0, | ||||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | has ttl => ( | ||||||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||||
| 17 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||
| 18 | 19 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 19 | 160 | ); | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||
| 22 | has key => ( | ||||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||||
| 24 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||
| 25 | 9 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 26 | 160 | ); | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||
| 29 | 9 | ▶ | sub to_anthropic { | ||||||||||||||||||||||||||||
|
Subroutine
to_anthropic: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 30 | 9 | my ( $self ) = @_; | |||||||||||||||||||||||||||||
| 31 | 9 | ▶ | return () unless $self->enable; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 32 | return ( | ||||||||||||||||||||||||||||||
| 33 | 9 | ▶ | cache_control => { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 34 | type => 'ephemeral', | ||||||||||||||||||||||||||||||
| 35 | ( defined $self->ttl ? ( ttl => $self->ttl ) : () ), | ||||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||
| 41 | 11 | ▶ | sub to_openai { | ||||||||||||||||||||||||||||
|
Subroutine
to_openai: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||||
| 42 | 11 | my ( $self ) = @_; | |||||||||||||||||||||||||||||
| 43 | 11 | ▶ | return () unless defined $self->key && length $self->key; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->key and length $self->key
MC/DC0%0/2
defined $self->key and length $self->key
defined $self->key
length $self->key
Truth table33%1/3
A and B
A= defined $self->keyB= length $self->key
| |||||||||||||||||||||||||||||||
| 44 | 11 | return ( prompt_cache_key => $self->key ); | |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||
| 48 | # Maps a cache_wire_format tag to the per-format serializer method. | ||||||||||||||||||||||||||||||
| 49 | 160 | my %TO_METHOD = ( | |||||||||||||||||||||||||||||
| 50 | openai => 'to_openai', | ||||||||||||||||||||||||||||||
| 51 | anthropic => 'to_anthropic', | ||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||
| 54 | 20 | ▶ | sub to { | ||||||||||||||||||||||||||||
|
Subroutine
to: called CC=4 SCAR=16.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 55 | 20 | my ( $self, $fmt ) = @_; | |||||||||||||||||||||||||||||
| 56 | 20 | ▶ | my $method = $TO_METHOD{ $fmt // '' } | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition25%1/4
$fmt // ""
$fmt // ""
MC/DC0%0/1
$fmt // ""
$fmt
MC/DC0%0/1
$fmt // ""
$fmt
Truth table50%1/2
A
A= $fmt
Truth table0%0/2
A
A= $fmt
| |||||||||||||||||||||||||||||||
| 57 | or croak "Langertha::PromptCache: unknown cache wire format '" . ( $fmt // '' ) . "'"; | ||||||||||||||||||||||||||||||
| 58 | 20 | return $self->$method; | |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||
| 62 | 160 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||
| 65 | 160 | 1; | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||
| 67 | __END__ | ||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||
| 69 | =pod | ||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||
| 71 | =encoding UTF-8 | ||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||
| 73 | =head1 NAME | ||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||
| 75 | Langertha::PromptCache - Immutable prompt-caching request control with cross-provider conversion | ||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||
| 77 | =head1 VERSION | ||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||
| 79 | version 0.503 | ||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||
| 83 | my $c = Langertha::PromptCache->new( | ||||||||||||||||||||||||||||||
| 84 | enable => 1, | ||||||||||||||||||||||||||||||
| 85 | ttl => '1h', | ||||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||||
| 87 | my %kwargs = $c->to('anthropic'); | ||||||||||||||||||||||||||||||
| 88 | # ( cache_control => { type => 'ephemeral', ttl => '1h' } ) | ||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||
| 90 | my $c2 = Langertha::PromptCache->new( key => 'my-route' ); | ||||||||||||||||||||||||||||||
| 91 | my %kw2 = $c2->to('openai'); | ||||||||||||||||||||||||||||||
| 92 | # ( prompt_cache_key => 'my-route' ) | ||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||
| 96 | Canonical value object for the request-side prompt-caching knob, dispatched | ||||||||||||||||||||||||||||||
| 97 | by an engine's C<cache_wire_format>. Mirrors L<Langertha::Reasoning>: the | ||||||||||||||||||||||||||||||
| 98 | per-provider placement of the field lives in this one reviewable place rather | ||||||||||||||||||||||||||||||
| 99 | than scattered across engines (ADR 0001). | ||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | Prompt caching is asymmetric across providers. Only two providers expose a | ||||||||||||||||||||||||||||||
| 102 | per-request knob worth modeling: | ||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||
| 104 | =over | ||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||
| 106 | =item * B<Anthropic> — an explicit C<cache_control> breakpoint enables caching | ||||||||||||||||||||||||||||||
| 107 | (top-level auto-place form). Carries an optional C<ttl> (C<5m> default, C<1h>). | ||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||
| 109 | =item * B<OpenAI> — caching is automatic; the only request-side lever is | ||||||||||||||||||||||||||||||
| 110 | C<prompt_cache_key>, a routing hint that steers which cache shard is used. | ||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||
| 112 | =back | ||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||
| 114 | Every other provider (Gemini, DeepSeek, ...) caches implicitly with no | ||||||||||||||||||||||||||||||
| 115 | request-side parameter, so this value object models exactly these two shapes. | ||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||
| 117 | =head2 enable | ||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||
| 119 | Whether to emit an Anthropic C<cache_control> breakpoint. Ignored by the | ||||||||||||||||||||||||||||||
| 120 | OpenAI serializer (OpenAI caching is automatic). | ||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | =head2 ttl | ||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||
| 124 | Optional Anthropic cache time-to-live: C<5m> (the current default when omitted) | ||||||||||||||||||||||||||||||
| 125 | or C<1h>. Only emitted when set; the C<1h> window needs this set explicitly. | ||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||
| 127 | =head2 key | ||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||
| 129 | Optional OpenAI C<prompt_cache_key> routing hint. Ignored by the Anthropic | ||||||||||||||||||||||||||||||
| 130 | serializer. | ||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||
| 132 | =head2 to_anthropic | ||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||
| 134 | Serializes to the top-level C<cache_control =E<gt> { type =E<gt> 'ephemeral' }> | ||||||||||||||||||||||||||||||
| 135 | auto-place form (plus C<ttl> when set). Empty list when caching is not enabled. | ||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||
| 137 | =head2 to_openai | ||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||
| 139 | Serializes to a flat C<prompt_cache_key>. Empty list when no key is set. | ||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||
| 141 | =head2 to | ||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||
| 143 | my %kwargs = $c->to($cache_wire_format); | ||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||
| 145 | Dispatch to the per-format serializer. Returns the body kwargs to merge into | ||||||||||||||||||||||||||||||
| 146 | the request (an empty list when nothing applies to that wire). | ||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||
| 148 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||
| 150 | =over | ||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||
| 152 | =item * L<Langertha::Role::PromptCache> - The composed role exposing the cache attributes | ||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||
| 154 | =item * L<Langertha::Reasoning> - Sibling value object for the reasoning-effort knob | ||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||