| 1 | package Langertha::Engine::MoonshotAnthropic; | ||||||||||||||||||||
| 2 | # ABSTRACT: Moonshot AI Kimi API via Anthropic-compatible endpoint | ||||||||||||||||||||
| 3 | 13 | our $VERSION = '0.503'; | |||||||||||||||||||
| 4 | 13 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 13 | ▶ | use Carp qw( croak ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 13 | extends 'Langertha::Engine::AnthropicBase'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 13 | with 'Langertha::Role::StaticModels'; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | # AnthropicBase->chat_request appends '/v1/messages' to url; the default must | ||||||||||||||||||||
| 13 | # therefore stop at '/anthropic' so the composed endpoint is a single | ||||||||||||||||||||
| 14 | # '/anthropic/v1/messages' (a '/anthropic/v1' default would double-stack to | ||||||||||||||||||||
| 15 | # '/anthropic/v1/v1/messages' -> HTTP 404). | ||||||||||||||||||||
| 16 | has '+url' => ( | ||||||||||||||||||||
| 17 | lazy => 1, | ||||||||||||||||||||
| 18 | 123 | ▶ | default => sub { 'https://api.moonshot.ai/anthropic' }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 13 | ); | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 0 | ▶ | sub _build_api_key { | ||||||||||||||||||
|
Subroutine
_build_api_key: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 22 | 0 | my ( $self ) = @_; | |||||||||||||||||||
| 23 | return $ENV{LANGERTHA_MOONSHOT_API_KEY} | ||||||||||||||||||||
| 24 | 0 | ▶ | || croak "".(ref $self)." requires LANGERTHA_MOONSHOT_API_KEY or api_key set"; | ||||||||||||||||||
|
Condition0%0/3
$ENV{'LANGERTHA_MOONSHOT_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_MOONSHOT_API_KEY or api_key set')
MC/DC0%0/2
$ENV{'LANGERTHA_MOONSHOT_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_MOONSHOT_API_KEY or api_key set')
$ENV{'LANGERTHA_MOONSHOT_API_KEY'}
croak('' . ref($self) . ' requires LANGERTHA_MOONSHOT_API_KEY or api_key set')
Truth table0%0/3
A || B
A= $ENV{'LANGERTHA_MOONSHOT_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_MOONSHOT_API_KEY or api_key set')
| |||||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 32 | ▶ | sub default_model { 'kimi-k3' } | ||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 1 | ▶ | sub api_key_env { 'LANGERTHA_MOONSHOT_API_KEY' } | ||||||||||||||||||
|
Subroutine
api_key_env: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 13 | ▶ | sub default_response_size { 4096 } | ||||||||||||||||||
|
Subroutine
default_response_size: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # Kimi counts reasoning_content against max_tokens and recommends | ||||||||||||||||||||
| 34 | # max_tokens >= 16000 while thinking is on (karr k225; platform.kimi.ai, advisor | ||||||||||||||||||||
| 35 | # 2026-09-25 on k219, docs only). kimi-k3 and kimi-k2.7-code(-highspeed) always | ||||||||||||||||||||
| 36 | # think, kimi-k2.6 thinks by default, so they default to 16000 instead of 4096. | ||||||||||||||||||||
| 37 | # Only when no response_size is set; max_tokens is a ceiling, billing follows | ||||||||||||||||||||
| 38 | # the tokens produced (ADR 0019 k225 Update). Same rows as | ||||||||||||||||||||
| 39 | # Engine::Moonshot: the same models think on this face (k215). | ||||||||||||||||||||
| 40 | 186 | ▶ | sub model_response_size_defaults { | ||||||||||||||||||
|
Subroutine
model_response_size_defaults: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 41 | return ( | ||||||||||||||||||||
| 42 | 186 | qr/\Akimi-k3(?!\d)/ => 16000, | |||||||||||||||||||
| 43 | qr/\Akimi-k2\.(?:6|7-code(?:-highspeed)?)\z/ => 16000, | ||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 0 | ▶ | sub _build_static_models {[ | ||||||||||||||||||
|
Subroutine
_build_static_models: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 48 | 0 | { id => 'kimi-k3' }, | |||||||||||||||||||
| 49 | { id => 'kimi-k2.7-code' }, | ||||||||||||||||||||
| 50 | { id => 'kimi-k2.7-code-highspeed' }, | ||||||||||||||||||||
| 51 | { id => 'kimi-k2.6' }, | ||||||||||||||||||||
| 52 | ]} | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | # Temperature is fixed server-side on every current Kimi id (karr k214; see | ||||||||||||||||||||
| 55 | # Engine::Moonshot for the sources). Kimi's Messages-API schema has no | ||||||||||||||||||||
| 56 | # temperature property at all, and whether /anthropic 400s on one or ignores it | ||||||||||||||||||||
| 57 | # is undocumented; the value is fixed either way, so omitting it is always safe | ||||||||||||||||||||
| 58 | # (ADR 0025 k214 Update). Same rows as the OpenAI face. | ||||||||||||||||||||
| 59 | # | ||||||||||||||||||||
| 60 | # Reasoning on K2.x (karr k215, docs/api/messages.md + the Claude Code guide, | ||||||||||||||||||||
| 61 | # advisor 2026-09-25, docs only): output_config.effort is K3-only, so the K2 | ||||||||||||||||||||
| 62 | # line takes no effort -- the flag is cleared for every K2 id. The two | ||||||||||||||||||||
| 63 | # documented ids opt back in, because this face parses a thinking toggle for | ||||||||||||||||||||
| 64 | # them (kimi-k2.6 enabled|disabled, kimi-k2.7-code enabled only); their | ||||||||||||||||||||
| 65 | # Reasoning::Profile rows send it instead of an effort (ADR 0023 k215 Update). | ||||||||||||||||||||
| 66 | # | ||||||||||||||||||||
| 67 | # image_input (k266, k359, ADR 0019): the same rows as Engine::Moonshot -- this | ||||||||||||||||||||
| 68 | # face serves the same Kimi models, and its Messages schema carries image | ||||||||||||||||||||
| 69 | # blocks (docs/api/messages.md: tool_result content is "a string or an array | ||||||||||||||||||||
| 70 | # of text / image blocks"; llm-advisor 2026-09-30, docs only). The catch-all | ||||||||||||||||||||
| 71 | # first row clears the flag for unchecked ids. Until k359 the flag was deleted | ||||||||||||||||||||
| 72 | # engine-wide here; it now also picks the tool-result image form, and an | ||||||||||||||||||||
| 73 | # engine-wide no-claim would have kept kimi-k3 from seeing a tool's image. | ||||||||||||||||||||
| 74 | 484 | ▶ | sub model_capability_corrections { | ||||||||||||||||||
|
Subroutine
model_capability_corrections: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 75 | return ( | ||||||||||||||||||||
| 76 | 484 | qr/\A/ => { image_input => 0 }, | |||||||||||||||||||
| 77 | qr/\Akimi-k3(?!\d)/ => { temperature => 0 }, | ||||||||||||||||||||
| 78 | qr/\Akimi-k2(?!\d)/ => { temperature => 0, reasoning_effort => 0 }, | ||||||||||||||||||||
| 79 | qr/\Akimi-k2\.(?:6|7-code(?:-highspeed)?)\z/ => { reasoning_effort => 1 }, | ||||||||||||||||||||
| 80 | qr/\Akimi-k(?:3|2\.6|2\.7-code)(?!\d)/ => { image_input => 1 }, | ||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | # Source blocks in a tool_result (karr k364, k366): Kimi's Messages OpenAPI | ||||||||||||||||||||
| 85 | # schema (platform.kimi.ai/docs/api/messages.md, llm-advisor 2026-09-30, docs | ||||||||||||||||||||
| 86 | # only, not live) lists tool_result content as string | [text | image] -- no | ||||||||||||||||||||
| 87 | # document, no search_result -- and Kimi checks block types strictly (a 400 | ||||||||||||||||||||
| 88 | # "Input tag 'document' found using 'type' does not match any of the expected | ||||||||||||||||||||
| 89 | # tags" is reported in github.com/MoonshotAI/Kimi-K2.5/issues/27). A text | ||||||||||||||||||||
| 90 | # resource goes out as a text block, a PDF as the placeholder, a native | ||||||||||||||||||||
| 91 | # document or search_result as its text, rather than a rejected tool-loop turn. | ||||||||||||||||||||
| 92 | 5 | ▶ | sub _tool_result_source_blocks_on_wire { 0 } | ||||||||||||||||||
|
Subroutine
_tool_result_source_blocks_on_wire: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | # Kimi's Messages API documents native output_config.format {type: json_schema, | ||||||||||||||||||||
| 95 | # schema} for kimi-k3 (docs/api/messages.md, advisor 2026-09-25, docs only; | ||||||||||||||||||||
| 96 | # karr k218), so K3 takes the first-party native path (ADR 0005 k218 Update) | ||||||||||||||||||||
| 97 | # instead of the shim's synthetic tool + forced named tool_choice. K2.x is | ||||||||||||||||||||
| 98 | # undocumented on this face and keeps the synthetic tool. Per model, so the | ||||||||||||||||||||
| 99 | # endpoint predicate _native_structured_output stays 0: this is still a shim, | ||||||||||||||||||||
| 100 | # and its manifest dialect stays anthropic-compat. | ||||||||||||||||||||
| 101 | 212 | ▶ | sub _native_structured_output_for_model { | ||||||||||||||||||
|
Subroutine
_native_structured_output_for_model: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 102 | 212 | my ( $self ) = @_; | |||||||||||||||||||
| 103 | 212 | ▶ | return ( $self->chat_model // '' ) =~ /\Akimi-k3(?!\d)/ ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->chat_model // ""
MC/DC0%0/1
$self->chat_model // ""
$self->chat_model
Truth table50%1/2
A
A= $self->chat_model
| |||||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | # This endpoint speaks the `thinking` on/off toggle (ADR 0023 k209 Update): the | ||||||||||||||||||||
| 107 | # thinking-toggle Reasoning::Profile rows serialize as the toggle only on an | ||||||||||||||||||||
| 108 | # engine that opts in here; the same model id elsewhere keeps its effort wire. | ||||||||||||||||||||
| 109 | 126 | ▶ | sub _reasoning_thinking_toggle { 1 } | ||||||||||||||||||
|
Subroutine
_reasoning_thinking_toggle: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 13 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 13 | 1; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =pod | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =encoding UTF-8 | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =head1 NAME | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | Langertha::Engine::MoonshotAnthropic - Moonshot AI Kimi API via Anthropic-compatible endpoint | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =head1 VERSION | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | version 0.503 | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =head1 SYNOPSIS | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | use Langertha::Engine::MoonshotAnthropic; | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | my $moonshot = Langertha::Engine::MoonshotAnthropic->new( | ||||||||||||||||||||
| 135 | api_key => $ENV{MOONSHOT_API_KEY}, | ||||||||||||||||||||
| 136 | model => 'kimi-k3', | ||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | print $moonshot->simple_chat('Hello from Perl!'); | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =head1 DESCRIPTION | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | Provides access to L<Moonshot AI|https://www.moonshot.ai/>'s Kimi models via | ||||||||||||||||||||
| 144 | their Anthropic-compatible endpoint at C<https://api.moonshot.ai/anthropic> | ||||||||||||||||||||
| 145 | (the shared L<Langertha::Engine::AnthropicBase> appends the C</v1/messages> | ||||||||||||||||||||
| 146 | path). This is the endpoint Moonshot documents for Claude Code / Anthropic-SDK | ||||||||||||||||||||
| 147 | clients. | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | For new code prefer L<Langertha::Engine::Moonshot>, which talks to Moonshot's | ||||||||||||||||||||
| 150 | native OpenAI-compatible endpoint. This class is retained for callers that need | ||||||||||||||||||||
| 151 | the Anthropic wire format specifically. | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | See L<Langertha::Engine::Moonshot> for the available models list. | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | On C<kimi-k3>, C<reasoning_effort> goes out as C<output_config.effort> when it | ||||||||||||||||||||
| 156 | is C<low>, C<high> or C<max>; any other level is dropped and the server default | ||||||||||||||||||||
| 157 | (C<max>) applies. K3 always reasons and Kimi's Messages API has no C<thinking> | ||||||||||||||||||||
| 158 | request field for it, so none is sent (C<thinking_display> has no effect on | ||||||||||||||||||||
| 159 | K3). | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | On the K2.x line Kimi takes no effort here, only a C<thinking> toggle, so | ||||||||||||||||||||
| 162 | C<reasoning_effort> becomes C<< thinking =E<gt> { type =E<gt> 'enabled' } >> | ||||||||||||||||||||
| 163 | for any level; C<none> sends C<< { type =E<gt> 'disabled' } >> on | ||||||||||||||||||||
| 164 | C<kimi-k2.6> and nothing on C<kimi-k2.7-code>, which cannot turn thinking off. | ||||||||||||||||||||
| 165 | Every level gives the same depth. A C<thinking_display> adds C<display> to an | ||||||||||||||||||||
| 166 | on toggle. Not verified against the live API: whether Kimi requires | ||||||||||||||||||||
| 167 | C<budget_tokens> with C<enabled> (none is sent), whether it accepts | ||||||||||||||||||||
| 168 | C<display> there, and whether C<kimi-k2.7-code> accepts a request with no | ||||||||||||||||||||
| 169 | C<thinking> field. Other K2 ids take no reasoning control on this endpoint. | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | On C<kimi-k3> a C<json_schema> C<response_format> goes out natively as | ||||||||||||||||||||
| 172 | C<output_config.format> (with the schema closed, as on first-party Anthropic), | ||||||||||||||||||||
| 173 | so it also streams; a C<json_object>, and every C<response_format> on the K2.x | ||||||||||||||||||||
| 174 | models, still use the synthesized-tool rewrite of the C</anthropic> shims. | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | C<max_tokens> defaults to 16000 on the thinking Kimi models, as on | ||||||||||||||||||||
| 177 | L<Langertha::Engine::Moonshot>; an explicit C<response_size> is sent as given. | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | Kimi fixes C<temperature> server-side on every current model, so none is sent; | ||||||||||||||||||||
| 180 | a C<temperature> other than C<1> is dropped with a warning. | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | C<image_input> is claimed for the same vision models as on | ||||||||||||||||||||
| 183 | L<Langertha::Engine::Moonshot> (C<kimi-k3>, C<kimi-k2.6>, C<kimi-k2.7-code>), | ||||||||||||||||||||
| 184 | so an image a tool returns reaches them as an C<image> block in the | ||||||||||||||||||||
| 185 | C<tool_result>. Kimi's schema takes no C<document> or C<search_result> there, | ||||||||||||||||||||
| 186 | so an embedded text resource goes out as a C<text> block, a PDF as a text | ||||||||||||||||||||
| 187 | placeholder, and a native C<document> / C<search_result> block as its text | ||||||||||||||||||||
| 188 | (with a warning). | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | Get your API key at L<https://platform.kimi.ai/> and set | ||||||||||||||||||||
| 191 | C<LANGERTHA_MOONSHOT_API_KEY> in your environment. | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | =head1 SEE ALSO | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | =over | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | =item * L<Langertha::Engine::Moonshot> - Recommended Moonshot engine (OpenAI-compatible endpoint) | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | =item * L<https://platform.kimi.ai/docs/api/overview> - Kimi API docs | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | =item * L<Langertha::Engine::AnthropicBase> - Anthropic-compatible base class | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | =back | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =head1 SUPPORT | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | =head2 Issues | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||
| 210 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | =head2 IRC | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | =head1 CONTRIBUTING | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | =head1 AUTHOR | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 229 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | =cut | ||||||||||||||||||||