Langertha/Plugin/Langfuse.pm

filter: Statementstmt 97.3% 147 / 151 Branchbran 65.7% 75 / 114 Conditioncond 41.9% 13 / 31 MC/DCmcdc 5.2% 1 / 19 Subroutinesub 96.3% 26 / 27 Podpod 69.2% 9 / 13 totaltotal 76.3% 271 / 355 CC 70 SCAR 21.6
CC
70
Cov
79%
create_generation
19.9
create_span
15.0
create_trace
11.4
CRAP 8.7
« Langertha/Usage.pm ↑ Summary Langertha/Role/HTTP.pm »
1 package Langertha::Plugin::Langfuse;
2 # ABSTRACT: Langfuse observability plugin for any PluginHost
3 2 our $VERSION = '0.502';
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Time::HiRes qw( gettimeofday );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use MIME::Base64 qw( encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 extends 'Langertha::Plugin';
12  
13  
14 has public_key => (
15 is => 'ro',
16 isa => 'Str',
17 lazy => 1,
18 1 ▶ default => sub { $ENV{LANGFUSE_PUBLIC_KEY} // '' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$ENV{"LANGFUSE_PUBLIC_KEY"} // ""
l!l
01
MC/DC0%0/1
$ENV{"LANGFUSE_PUBLIC_KEY"} // ""
$ENV{"LANGFUSE_PUBLIC_KEY"}
Truth table50%1/2
A
A= $ENV{"LANGFUSE_PUBLIC_KEY"}
Aresult
11
00
19 2 );
20  
21  
22 has secret_key => (
23 is => 'ro',
24 isa => 'Str',
25 lazy => 1,
26 0 ▶ default => sub { $ENV{LANGFUSE_SECRET_KEY} // '' },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$ENV{"LANGFUSE_SECRET_KEY"} // ""
l!l
00
MC/DC0%0/1
$ENV{"LANGFUSE_SECRET_KEY"} // ""
$ENV{"LANGFUSE_SECRET_KEY"}
Truth table0%0/2
A
A= $ENV{"LANGFUSE_SECRET_KEY"}
Aresult
11
00
27 2 );
28  
29  
30 has url => (
31 is => 'ro',
32 isa => 'Str',
33 lazy => 1,
34 2 ▶ default => sub { $ENV{LANGFUSE_URL} // 'https://cloud.langfuse.com' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$ENV{"LANGFUSE_URL"} // "https://cloud.langfuse.com"
l!l
02
MC/DC0%0/1
$ENV{"LANGFUSE_URL"} // "https://cloud.langfuse.com"
$ENV{"LANGFUSE_URL"}
Truth table50%1/2
A
A= $ENV{"LANGFUSE_URL"}
Aresult
11
00
35 2 );
36  
37  
38 2 has enabled => (
39 is => 'ro',
40 isa => 'Bool',
41 lazy => 1,
42 builder => '_build_enabled',
43 );
44  
45  
46 sub _build_enabled {
47 17 ▶ my ( $self ) = @_;
Subroutine _build_enabled: called CC=3 SCAR=11.1
48 17 ▶ return length($self->public_key) && length($self->secret_key) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $self->public_key && length $self->secret_key ? : 16 1 17
Condition66%2/3
length $self->public_key and length $self->secret_key
!ll&&!rl&&r
1016
MC/DC50%1/2
length $self->public_key and length $self->secret_key
length $self->public_key length $self->secret_key
Truth table66%2/3
A and B
A= length $self->public_keyB= length $self->secret_key
ABresult
0X0
100
111
49 }
50  
51 2 has trace_name => (
52 is => 'ro',
53 isa => 'Str',
54 default => 'llm-call',
55 );
56  
57  
58 2 has user_id => (
59 is => 'ro',
60 isa => 'Str',
61 predicate => 'has_user_id',
62 );
63  
64  
65 2 has session_id => (
66 is => 'ro',
67 isa => 'Str',
68 predicate => 'has_session_id',
69 );
70  
71  
72 2 has tags => (
73 is => 'ro',
74 isa => 'ArrayRef[Str]',
75 predicate => 'has_tags',
76 );
77  
78  
79 2 has metadata => (
80 is => 'ro',
81 isa => 'HashRef',
82 predicate => 'has_metadata',
83 );
84  
85  
86 2 has auto_flush => (
87 is => 'ro',
88 isa => 'Bool',
89 default => 0,
90 );
91  
92  
93 # --- Internal state ---
94  
95 has _batch => (
96 is => 'rw',
97 isa => 'ArrayRef',
98 17 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
99 2 );
100  
101 2 has _trace_id => (
102 is => 'rw',
103 isa => 'Maybe[Str]',
104 );
105  
106 2 has _iter_start => (
107 is => 'rw',
108 isa => 'Maybe[Str]',
109 );
110  
111 has _json => (
112 is => 'ro',
113 lazy => 1,
114 2 ▶ default => sub { JSON::MaybeXS->new(utf8 => 1, canonical => 1, convert_blessed => 1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 2 );
116  
117 # --- Langfuse utility methods ---
118  
119 sub _id {
120 62 ▶ my @hex = map { sprintf("%04x", int(rand(65536))) } 1..8;
Subroutine _id: called CC=1 SCAR=0.0
121 62 return join('-',
122 $hex[0].$hex[1],
123 $hex[2],
124 '4'.substr($hex[3], 1),
125 sprintf("%x", 8 + int(rand(4))).substr($hex[4], 1),
126 $hex[5].$hex[6].$hex[7],
127 );
128 }
129  
130 sub _timestamp {
131 60 ▶ my ($s, $us) = gettimeofday;
Subroutine _timestamp: called CC=1 SCAR=0.0
132 60 my @t = gmtime($s);
133 60 return sprintf("%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
134 $t[5]+1900, $t[4]+1, $t[3], $t[2], $t[1], $t[0], int($us/1000));
135 }
136  
137 # --- Langfuse event creation ---
138  
139 sub create_trace {
140 10 ▶ my ( $self, %opts ) = @_;
Subroutine create_trace: called CC=10 SCAR=24.3
Pod documented
141 10 ▶ return unless $self->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled 0 10 10
142 10 ▶ my $id = $opts{id} || _id();
Condition33%1/3
$opts{'id'} || _id()
l!l&&r!l&&!r
0100
MC/DC0%0/2
$opts{'id'} || _id()
$opts{'id'} _id()
Truth table33%1/3
A || B
A= $opts{'id'}B= _id()
ABresult
1X1
011
000
143 10 ▶ push @{$self->_batch}, {
Branch: 6 branches
BranchTrueFalseTotal
$opts{'input'} ? : 10 0 10
$opts{'output'} ? : 0 10 10
$opts{'metadata'} ? : 1 9 10
$opts{'tags'} ? : 1 9 10
$opts{'user_id'} ? : 1 9 10
$opts{'session_id'} ? : 1 9 10
Condition50%1/2
$opts{"name"} // "langfuse-trace"
l!l
100
MC/DC0%0/1
$opts{"name"} // "langfuse-trace"
$opts{"name"}
Truth table50%1/2
A
A= $opts{"name"}
Aresult
11
00
144 id => _id(),
145 type => 'trace-create',
146 timestamp => _timestamp(),
147 body => {
148 id => $id,
149 name => $opts{name} // 'langfuse-trace',
150 $opts{input} ? ( input => $opts{input} ) : (),
151 $opts{output} ? ( output => $opts{output} ) : (),
152 $opts{metadata} ? ( metadata => $opts{metadata} ) : (),
153 $opts{tags} ? ( tags => $opts{tags} ) : (),
154 $opts{user_id} ? ( userId => $opts{user_id} ) : (),
155 10 $opts{session_id} ? ( sessionId => $opts{session_id} ) : (),
156 },
157 };
158 10 return $id;
159 }
160  
161  
162 sub create_generation {
163 13 ▶ my ( $self, %opts ) = @_;
Subroutine create_generation: called CC=13 SCAR=29.9
Pod documented
164 13 ▶ return unless $self->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled 0 13 13
165 13 ▶ my $id = $opts{id} || _id();
Condition33%1/3
$opts{'id'} || _id()
l!l&&r!l&&!r
0130
MC/DC0%0/2
$opts{'id'} || _id()
$opts{'id'} _id()
Truth table33%1/3
A || B
A= $opts{'id'}B= _id()
ABresult
1X1
011
000
166 13 ▶ push @{$self->_batch}, {
Branch: 8 branches
BranchTrueFalseTotal
$opts{'model'} ? : 1 11 12
$opts{'input'} ? : 2 10 12
$opts{'output'} ? : 0 12 12
$opts{'usage'} ? : 0 12 12
$opts{'start_time'} ? : 12 0 12
$opts{'end_time'} ? : 12 0 12
$opts{'parent_observation_id'} ? : 0 12 12
$opts{'model_parameters'} ? : 0 12 12
Condition60%3/5
$opts{"trace_id"} // croak("create_generation requires trace_id")
l!l&&r!l&&!r
1210
$opts{"name"} // "generation"
l!l
120
MC/DC0%0/2
$opts{"trace_id"} // croak("create_generation requires trace_id")
$opts{"trace_id"} croak("create_generation requires trace_id")
MC/DC0%0/1
$opts{"name"} // "generation"
$opts{"name"}
Truth table33%1/3
A // B
A= $opts{"trace_id"}B= croak("create_generation requires trace_id")
ABresult
1X1
011
000
Truth table50%1/2
A
A= $opts{"name"}
Aresult
11
00
167 id => _id(),
168 type => 'generation-create',
169 timestamp => _timestamp(),
170 body => {
171 id => $id,
172 traceId => $opts{trace_id} // croak("create_generation requires trace_id"),
173 name => $opts{name} // 'generation',
174 $opts{model} ? ( model => $opts{model} ) : (),
175 $opts{input} ? ( input => $opts{input} ) : (),
176 $opts{output} ? ( output => $opts{output} ) : (),
177 $opts{usage} ? ( usage => $opts{usage} ) : (),
178 $opts{start_time} ? ( startTime => $opts{start_time} ) : (),
179 $opts{end_time} ? ( endTime => $opts{end_time} ) : (),
180 $opts{parent_observation_id}? ( parentObservationId => $opts{parent_observation_id} ) : (),
181 13 $opts{model_parameters} ? ( modelParameters => $opts{model_parameters} ) : (),
182 },
183 };
184 12 return $id;
185 }
186  
187  
188 sub create_span {
189 2 ▶ my ( $self, %opts ) = @_;
Subroutine create_span: called CC=11 SCAR=27.1
Pod documented
190 2 ▶ return unless $self->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled 0 2 2
191 2 ▶ my $id = $opts{id} || _id();
Condition33%1/3
$opts{'id'} || _id()
l!l&&r!l&&!r
020
MC/DC0%0/2
$opts{'id'} || _id()
$opts{'id'} _id()
Truth table33%1/3
A || B
A= $opts{'id'}B= _id()
ABresult
1X1
011
000
192 2 ▶ push @{$self->_batch}, {
Branch: 7 branches
BranchTrueFalseTotal
$opts{'name'} ? : 2 0 2
$opts{'input'} ? : 2 0 2
$opts{'output'} ? : 1 1 2
$opts{'start_time'} ? : 1 1 2
$opts{'end_time'} ? : 1 1 2
$opts{'parent_observation_id'} ? : 0 2 2
$opts{'metadata'} ? : 0 2 2
Condition33%1/3
$opts{"trace_id"} // croak("create_span requires trace_id")
l!l&&r!l&&!r
200
MC/DC0%0/2
$opts{"trace_id"} // croak("create_span requires trace_id")
$opts{"trace_id"} croak("create_span requires trace_id")
Truth table33%1/3
A // B
A= $opts{"trace_id"}B= croak("create_span requires trace_id")
ABresult
1X1
011
000
193 id => _id(),
194 type => 'span-create',
195 timestamp => _timestamp(),
196 body => {
197 id => $id,
198 traceId => $opts{trace_id} // croak("create_span requires trace_id"),
199 $opts{name} ? ( name => $opts{name} ) : (),
200 $opts{input} ? ( input => $opts{input} ) : (),
201 $opts{output} ? ( output => $opts{output} ) : (),
202 $opts{start_time} ? ( startTime => $opts{start_time} ) : (),
203 $opts{end_time} ? ( endTime => $opts{end_time} ) : (),
204 $opts{parent_observation_id}? ( parentObservationId => $opts{parent_observation_id} ) : (),
205 2 $opts{metadata} ? ( metadata => $opts{metadata} ) : (),
206 },
207 };
208 2 return $id;
209 }
210  
211  
212 sub update_trace {
213 12 ▶ my ( $self, %opts ) = @_;
Subroutine update_trace: called CC=5 SCAR=17.8
Pod documented
214 12 ▶ return unless $self->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled 0 12 12
215 12 ▶ my $id = $opts{id} // croak("update_trace requires id");
Condition33%1/3
$opts{"id"} // croak("update_trace requires id")
l!l&&r!l&&!r
1200
MC/DC0%0/2
$opts{"id"} // croak("update_trace requires id")
$opts{"id"} croak("update_trace requires id")
Truth table33%1/3
A // B
A= $opts{"id"}B= croak("update_trace requires id")
ABresult
1X1
011
000
216 12 ▶ push @{$self->_batch}, {
Branch: 2 branches
BranchTrueFalseTotal
$opts{'output'} ? : 12 0 12
$opts{'metadata'} ? : 0 12 12
217 id => _id(),
218 type => 'trace-create',
219 timestamp => _timestamp(),
220 body => {
221 id => $id,
222 $opts{output} ? ( output => $opts{output} ) : (),
223 12 $opts{metadata} ? ( metadata => $opts{metadata} ) : (),
224 },
225 };
226 12 return $id;
227 }
228  
229  
230 sub flush {
231 3 ▶ my ( $self ) = @_;
Subroutine flush: called CC=4 SCAR=14.0
Pod documented
232 3 ▶ return unless $self->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled 0 3 3
233 3 my $batch = $self->_batch;
234 3 ▶ return unless @$batch;
Branch: 1 branch
BranchTrueFalseTotal
unless @$batch 1 2 3
235  
236 2 require LWP::UserAgent;
237 2 my $ua = LWP::UserAgent->new(agent => 'Langertha-Plugin-Langfuse/'.$VERSION);
238  
239 2 my $auth = encode_base64(
240 $self->public_key . ':' . $self->secret_key, ''
241 );
242  
243 2 my $body = $self->_json->encode({ batch => $batch });
244  
245 2 my $request = HTTP::Request->new(
246 POST => $self->url . '/api/public/ingestion',
247 [
248 'Content-Type' => 'application/json',
249 'Authorization' => 'Basic ' . $auth,
250 ],
251 $body,
252 );
253  
254 2 my $response = $ua->request($request);
255 2 $self->_batch([]);
256  
257 2 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 2 2
258 0 warn "Langfuse ingestion failed: " . $response->status_line;
259 }
260  
261 2 return $response;
262 }
263  
264  
265 sub reset_trace {
266 1 ▶ my ( $self ) = @_;
Subroutine reset_trace: called CC=1 SCAR=0.0
Pod documented
267 1 $self->_trace_id(undef);
268 1 $self->_iter_start(undef);
269 }
270  
271  
272  
273 # --- Plugin hooks ---
274  
275 10 ▶async sub plugin_before_llm_call {
Subroutine plugin_before_llm_call: called CC=7 SCAR=19.5
Pod documented
276 10 my ( $self, $conversation, $iteration ) = @_;
277 10 ▶ return $conversation unless $self->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled 1 9 10
278  
279 # Create trace on first iteration (or if no trace exists)
280 9 ▶ if (!$self->_trace_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_trace_id) 7 2 9
281 7 ▶ $self->_trace_id($self->create_trace(
Branch: 4 branches
BranchTrueFalseTotal
$self->has_user_id ? : 1 6 7
$self->has_session_id ? : 1 6 7
$self->has_tags ? : 1 6 7
$self->has_metadata ? : 1 6 7
282 name => $self->trace_name,
283 input => $conversation,
284 $self->has_user_id ? ( user_id => $self->user_id ) : (),
285 $self->has_session_id ? ( session_id => $self->session_id ) : (),
286 $self->has_tags ? ( tags => $self->tags ) : (),
287 $self->has_metadata ? ( metadata => $self->metadata ) : (),
288 ));
289 }
290  
291 9 $self->_iter_start(_timestamp());
292 9 return $conversation;
293 2 }
294  
295 9 ▶async sub plugin_after_llm_response {
Subroutine plugin_after_llm_response: called CC=3 SCAR=11.0
Pod documented
296 9 my ( $self, $data, $iteration ) = @_;
297 9 ▶ return $data unless $self->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled 0 9 9
298  
299 # Create generation event
300 9 my $end_time = _timestamp();
301 9 $self->create_generation(
302 trace_id => $self->_trace_id,
303 name => "generation-$iteration",
304 start_time => $self->_iter_start,
305 end_time => $end_time,
306 );
307  
308 # Update trace output (last update wins via upsert)
309 9 $self->update_trace(
310 id => $self->_trace_id,
311 output => $data,
312 );
313  
314 9 ▶ if ($self->auto_flush) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_flush) 1 8 9
315 1 $self->flush;
316 }
317  
318 9 return $data;
319 2 }
320  
321 1 ▶async sub plugin_after_tool_call {
Subroutine plugin_after_tool_call: called CC=2 SCAR=7.0
Pod documented
322 1 my ( $self, $name, $input, $result ) = @_;
323 1 ▶ return $result unless $self->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled 0 1 1
324  
325 1 my $t = _timestamp();
326 1 $self->create_span(
327 trace_id => $self->_trace_id,
328 name => "tool:$name",
329 input => $input,
330 output => $result,
331 start_time => $t,
332 end_time => $t,
333 );
334  
335 1 return $result;
336 2 }
337  
338 1 ▶async sub plugin_before_image_gen {
Subroutine plugin_before_image_gen: called CC=7 SCAR=21.5
Pod undocumented
339 1 my ( $self, $prompt ) = @_;
340 1 ▶ return $prompt unless $self->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled 0 1 1
341  
342 1 ▶ if (!$self->_trace_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_trace_id) 1 0 1
343 1 ▶ $self->_trace_id($self->create_trace(
Branch: 4 branches
BranchTrueFalseTotal
$self->has_user_id ? : 0 1 1
$self->has_session_id ? : 0 1 1
$self->has_tags ? : 0 1 1
$self->has_metadata ? : 0 1 1
344 name => $self->trace_name,
345 input => $prompt,
346 $self->has_user_id ? ( user_id => $self->user_id ) : (),
347 $self->has_session_id ? ( session_id => $self->session_id ) : (),
348 $self->has_tags ? ( tags => $self->tags ) : (),
349 $self->has_metadata ? ( metadata => $self->metadata ) : (),
350 ));
351 }
352  
353 1 $self->_iter_start(_timestamp());
354 1 return $prompt;
355 2 }
356  
357 1 ▶async sub plugin_after_image_gen {
Subroutine plugin_after_image_gen: called CC=3 SCAR=11.3
Pod undocumented
358 1 my ( $self, $prompt, $result ) = @_;
359 1 ▶ return $result unless $self->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled 0 1 1
360  
361 1 my $end_time = _timestamp();
362 1 $self->create_generation(
363 trace_id => $self->_trace_id,
364 name => 'image-generation',
365 start_time => $self->_iter_start,
366 end_time => $end_time,
367 input => $prompt,
368 );
369  
370 1 $self->update_trace(
371 id => $self->_trace_id,
372 output => $result,
373 );
374  
375 1 ▶ if ($self->auto_flush) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_flush) 0 1 1
376 0 $self->flush;
377 }
378  
379 1 return $result;
380 2 }
381  
382 1 ▶async sub plugin_before_embedding {
Subroutine plugin_before_embedding: called CC=7 SCAR=21.5
Pod undocumented
383 1 my ( $self, $text ) = @_;
384 1 ▶ return $text unless $self->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled 0 1 1
385  
386 1 ▶ if (!$self->_trace_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_trace_id) 1 0 1
387 1 ▶ $self->_trace_id($self->create_trace(
Branch: 4 branches
BranchTrueFalseTotal
$self->has_user_id ? : 0 1 1
$self->has_session_id ? : 0 1 1
$self->has_tags ? : 0 1 1
$self->has_metadata ? : 0 1 1
388 name => $self->trace_name,
389 input => $text,
390 $self->has_user_id ? ( user_id => $self->user_id ) : (),
391 $self->has_session_id ? ( session_id => $self->session_id ) : (),
392 $self->has_tags ? ( tags => $self->tags ) : (),
393 $self->has_metadata ? ( metadata => $self->metadata ) : (),
394 ));
395 }
396  
397 1 $self->_iter_start(_timestamp());
398 1 return $text;
399 2 }
400  
401 1 ▶async sub plugin_after_embedding {
Subroutine plugin_after_embedding: called CC=4 SCAR=14.6
Pod undocumented
402 1 my ( $self, $text, $vector ) = @_;
403 1 ▶ return $vector unless $self->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled 0 1 1
404  
405 1 my $end_time = _timestamp();
406 1 $self->create_generation(
407 trace_id => $self->_trace_id,
408 name => 'embedding',
409 start_time => $self->_iter_start,
410 end_time => $end_time,
411 input => $text,
412 );
413  
414 1 ▶ $self->update_trace(
Branch: 1 branch
BranchTrueFalseTotal
ref $vector eq 'ARRAY' ? : 1 0 1
415 id => $self->_trace_id,
416 output => { dimensions => ref $vector eq 'ARRAY' ? scalar @$vector : undef },
417 );
418  
419 1 ▶ if ($self->auto_flush) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_flush) 0 1 1
420 0 $self->flush;
421 }
422  
423 1 return $vector;
424 2 }
425  
426 2 __PACKAGE__->meta->make_immutable;
427  
428 2 1;
429  
430 __END__
431  
432 =pod
433  
434 =encoding UTF-8
435  
436 =head1 NAME
437  
438 Langertha::Plugin::Langfuse - Langfuse observability plugin for any PluginHost
439  
440 =head1 VERSION
441  
442 version 0.502
443  
444 =head1 SYNOPSIS
445  
446 use Langertha::Chat;
447 use Langertha::Plugin::Langfuse;
448  
449 my $langfuse = Langertha::Plugin::Langfuse->new(
450 public_key => 'pk-lf-...',
451 secret_key => 'sk-lf-...',
452 );
453  
454 my $chat = Langertha::Chat->new(
455 engine => $engine,
456 plugins => [$langfuse],
457 );
458  
459 $chat->simple_chat('Hello!');
460 $langfuse->flush;
461  
462 Or with sugar:
463  
464 my $chat = Langertha::Chat->new(
465 engine => $engine,
466 plugins => [Langfuse => {
467 trace_name => 'my-chat',
468 auto_flush => 1,
469 }],
470 );
471  
472 Environment variables C<LANGFUSE_PUBLIC_KEY>, C<LANGFUSE_SECRET_KEY>, and
473 C<LANGFUSE_URL> are auto-populated when not explicitly set.
474  
475 =head1 DESCRIPTION
476  
477 This plugin integrates any L<Langertha::Role::PluginHost> (L<Langertha::Chat>,
478 L<Langertha::Embedder>, L<Langertha::Raider>) with
479 L<Langfuse|https://langfuse.com/> observability. It hooks into the standard
480 plugin events to automatically create traces, generations, and spans.
481  
482 Unlike L<Langertha::Role::Langfuse> (which lives on the engine), this plugin
483 works on any PluginHost and does not require engine-level configuration.
484  
485 =head2 public_key
486  
487 Langfuse project public key. Defaults to C<LANGFUSE_PUBLIC_KEY> env var.
488  
489 =head2 secret_key
490  
491 Langfuse project secret key. Defaults to C<LANGFUSE_SECRET_KEY> env var.
492  
493 =head2 url
494  
495 Langfuse API URL. Defaults to C<LANGFUSE_URL> env var or
496 C<https://cloud.langfuse.com>.
497  
498 =head2 enabled
499  
500 Whether Langfuse integration is active. Defaults to true when both
501 C<public_key> and C<secret_key> are non-empty.
502  
503 =head2 trace_name
504  
505 Name for the Langfuse trace created per chat session. Defaults to
506 C<'llm-call'>.
507  
508 =head2 user_id
509  
510 Optional user ID passed to the Langfuse trace.
511  
512 =head2 session_id
513  
514 Optional session ID passed to the Langfuse trace.
515  
516 =head2 tags
517  
518 Optional tags passed to the Langfuse trace.
519  
520 =head2 metadata
521  
522 Optional metadata HashRef merged into the Langfuse trace.
523  
524 =head2 auto_flush
525  
526 When true, automatically flushes events after each C<plugin_after_llm_response>.
527 Defaults to false.
528  
529 =head2 create_trace
530  
531 my $trace_id = $plugin->create_trace(name => 'my-trace', input => {...});
532  
533 Creates a trace event. Returns the trace ID. Called automatically by
534 the plugin hooks, but can also be used manually.
535  
536 =head2 create_generation
537  
538 $plugin->create_generation(trace_id => $id, model => 'gpt-4o', ...);
539  
540 Creates a generation event linked to a trace.
541  
542 =head2 create_span
543  
544 $plugin->create_span(trace_id => $id, name => 'tool-call', ...);
545  
546 Creates a span event within a trace.
547  
548 =head2 update_trace
549  
550 $plugin->update_trace(id => $trace_id, output => 'result');
551  
552 Updates a trace by upserting with the same ID.
553  
554 =head2 flush
555  
556 $plugin->flush;
557  
558 Sends all batched events to the Langfuse ingestion API. Clears the
559 batch after sending.
560  
561 =head2 reset_trace
562  
563 $plugin->reset_trace;
564  
565 Resets the current trace state. Call this between independent chat
566 sessions to start a new trace.
567  
568 =head1 SEE ALSO
569  
570 =over
571  
572 =item * L<Langertha::Plugin> - Base class with all hook method signatures
573  
574 =item * L<Langertha::Role::PluginHost> - Plugin system consumed by hosts
575  
576 =item * L<Langertha::Chat> - Chat host this plugin attaches to
577  
578 =item * L<Langertha::Embedder> - Embedder host this plugin attaches to
579  
580 =item * L<Langertha::ImageGen> - Image generation host this plugin attaches to
581  
582 =item * L<Langertha::Raider> - Autonomous agent host this plugin attaches to
583  
584 =item * L<https://langfuse.com/> - Langfuse observability platform
585  
586 =back
587  
588 =head1 SUPPORT
589  
590 =head2 Issues
591  
592 Please report bugs and feature requests on GitHub at
593 L<https://github.com/Getty/langertha/issues>.
594  
595 =head2 IRC
596  
597 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
598  
599 =head1 CONTRIBUTING
600  
601 Contributions are welcome! Please fork the repository and submit a pull request.
602  
603 =head1 AUTHOR
604  
605 Torsten Raudssus <getty@cpan.org>
606  
607 =head1 COPYRIGHT AND LICENSE
608  
609 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
610  
611 This is free software; you can redistribute it and/or modify it under
612 the same terms as the Perl 5 programming language system itself.
613  
614 =cut
« Langertha/Usage.pm ↑ Summary Langertha/Role/HTTP.pm »