blib/lib/Langertha/Engine/OpenRouter.pm

filter: Statementstmt 100% 28 / 28 Branchbran 50.0% 2 / 4 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 11 / 11 Podpod 25.0% 1 / 4 totaltotal 73.7% 45 / 61 CC 6 SCAR 10.7
CC
6
Cov
80%
_wire_usage
6.6
_build_api_key
2.3
BEGIN
1.0
CRAP 2.9
« blib/lib/Langertha/Engine/Cerebras.pm ↑ Summary blib/lib/Langertha/Engine/SGLang.pm »
1 package Langertha::Engine::OpenRouter;
2 # ABSTRACT: OpenRouter API
3 17 our $VERSION = '0.503';
4 17 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 extends 'Langertha::Engine::OpenAIBase';
9  
10 17 with 'Langertha::Role::Tools';
11  
12  
13 has '+url' => (
14 lazy => 1,
15 5 ▶ default => sub { 'https://openrouter.ai/api/v1' },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 17 );
17  
18 1 ▶sub _build_api_key {
Subroutine _build_api_key: called CC=2 SCAR=8.1
19 1 my ( $self ) = @_;
20 return $ENV{LANGERTHA_OPENROUTER_API_KEY}
21 1 ▶ || croak "".(ref $self)." requires LANGERTHA_OPENROUTER_API_KEY or api_key set";
Condition33%1/3
$ENV{'LANGERTHA_OPENROUTER_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_OPENROUTER_API_KEY or api_key set')
l!l&&r!l&&!r
100
MC/DC0%0/2
$ENV{'LANGERTHA_OPENROUTER_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_OPENROUTER_API_KEY or api_key set')
$ENV{'LANGERTHA_OPENROUTER_API_KEY'} croak('' . ref($self) . ' requires LANGERTHA_OPENROUTER_API_KEY or api_key set')
Truth table33%1/3
A || B
A= $ENV{'LANGERTHA_OPENROUTER_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_OPENROUTER_API_KEY or api_key set')
ABresult
1X1
011
000
22 }
23  
24 4 ▶sub default_model { croak "".(ref $_[0])." requires model to be set" }
Subroutine default_model: called CC=1 SCAR=0.0
Pod documented
25  
26 17 ▶sub _build_supported_operations {[qw(
Subroutine _build_supported_operations: called CC=1 SCAR=0.0
27 createChatCompletion
28 )]}
29  
30 # image_input (k266, ADR 0019): a gateway: the model behind it is unknown to
31 # the client, so no static claim. The catch-all is a layer-3 row, not a
32 # layer-2 delete, so a fact probed from /models (architecture.input_modalities)
33 # can answer per model (ADR 0032).
34 119 ▶sub model_capability_corrections {
Subroutine model_capability_corrections: called CC=1 SCAR=0.0
Pod undocumented
35 119 return ( qr/\A/ => { image_input => 0 } );
36 }
37  
38 # usage.cost is what OpenRouter charged, in its credits, and "OpenRouter uses a
39 # credit system where the base currency is US dollars" (openrouter.ai/docs/faq,
40 # read 2026-09-30). The wire number names no unit and "cost" is too generic to
41 # read as USD from every OpenAI-compatible server, so this engine states it:
42 # the copy of the usage block gets the canonical cost_usd key that
43 # Langertha::Usage reads (ADR 0018 tier 3, ADR 0031, k363).
44 # cost_details.upstream_inference_cost (BYOK: what the key's own provider
45 # charged) is not OpenRouter's bill and is not folded in.
46 2 ▶sub _wire_usage {
Subroutine _wire_usage: called CC=5 SCAR=18.9
47 2 my ( $self, $usage ) = @_;
48 2 ▶ return $usage unless ref $usage eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $usage eq "HASH" 0 2 2
49 2 my $cost = $usage->{cost};
50 2 ▶ return $usage unless defined $cost && !ref $cost && looks_like_number($cost);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cost and not ref $cost and &looks_like_number($cost) 0 2 2
Condition33%2/6
defined $cost and not ref $cost and &looks_like_number($cost)
!ll&&!rl&&r
002
defined $cost and not ref $cost
!ll&&!rl&&r
002
MC/DC0%0/3
defined $cost and not ref $cost and &looks_like_number($cost)
defined $cost not ref $cost &looks_like_number($cost)
Truth table25%1/4
A and not(B) and C
A= defined $costB= not ref $costC= &looks_like_number($cost)
ABCresult
0XX0
10X0
1100
1111
51 2 return { %$usage, cost_usd => 0 + $cost };
52 }
53  
54 35 ▶sub model_metadata_format { 'openrouter' }
Subroutine model_metadata_format: called CC=1 SCAR=0.0
Pod undocumented
55 16 ▶sub model_metadata_url { $_[0]->url . $_[0]->list_models_path }
Subroutine model_metadata_url: called CC=1 SCAR=0.0
Pod undocumented
56  
57 17 __PACKAGE__->meta->make_immutable;
58  
59  
60 17 1;
61  
62 __END__
63  
64 =pod
65  
66 =encoding UTF-8
67  
68 =head1 NAME
69  
70 Langertha::Engine::OpenRouter - OpenRouter API
71  
72 =head1 VERSION
73  
74 version 0.503
75  
76 =head1 SYNOPSIS
77  
78 use Langertha::Engine::OpenRouter;
79  
80 my $router = Langertha::Engine::OpenRouter->new(
81 api_key => $ENV{OPENROUTER_API_KEY},
82 model => 'anthropic/claude-sonnet-4-6',
83 );
84  
85 print $router->simple_chat('Hello from Perl!');
86  
87 # Access many providers through one API
88 my $deepseek = Langertha::Engine::OpenRouter->new(
89 api_key => $ENV{OPENROUTER_API_KEY},
90 model => 'deepseek/deepseek-r1',
91 );
92  
93 =head1 DESCRIPTION
94  
95 Provides access to OpenRouter, a unified API gateway for 300+ models from
96 many providers (OpenAI, Anthropic, Google, Meta, Mistral, and more).
97 Composes L<Langertha::Role::OpenAICompatible> with OpenRouter's endpoint
98 (C<https://openrouter.ai/api/v1>).
99  
100 Model names use C<provider/model> format (e.g., C<anthropic/claude-sonnet-4-6>,
101 C<openai/gpt-4o>, C<google/gemini-2.5-flash>). No default model is set;
102 C<model> must be specified explicitly.
103  
104 Supports chat, streaming, and MCP tool calling. Embeddings and transcription
105 are not supported.
106  
107 What OpenRouter charged for a request (C<usage.cost>, in its credits, which
108 are US dollars) is read as L<Langertha::Usage/cost_usd>, on responses and on
109 the stream's usage frame.
110  
111 Get your API key at L<https://openrouter.ai/settings/keys> and set
112 C<LANGERTHA_OPENROUTER_API_KEY> in your environment.
113  
114 B<THIS API IS WORK IN PROGRESS>
115  
116 =head1 SEE ALSO
117  
118 =over
119  
120 =item * L<https://status.openrouter.ai/> - OpenRouter service status
121  
122 =item * L<https://openrouter.ai/docs> - OpenRouter documentation
123  
124 =item * L<https://openrouter.ai/models> - Browse available models
125  
126 =item * L<Langertha::Role::OpenAICompatible> - OpenAI API format role
127  
128 =back
129  
130 =head1 SUPPORT
131  
132 =head2 Issues
133  
134 Please report bugs and feature requests on GitHub at
135 L<https://github.com/Getty/langertha/issues>.
136  
137 =head2 IRC
138  
139 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
140  
141 =head1 CONTRIBUTING
142  
143 Contributions are welcome! Please fork the repository and submit a pull request.
144  
145 =head1 AUTHOR
146  
147 Torsten Raudssus <getty@cpan.org>
148  
149 =head1 COPYRIGHT AND LICENSE
150  
151 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
152  
153 This is free software; you can redistribute it and/or modify it under
154 the same terms as the Perl 5 programming language system itself.
155  
156 =cut
« blib/lib/Langertha/Engine/Cerebras.pm ↑ Summary blib/lib/Langertha/Engine/SGLang.pm »