feat(gateway): add Anthropic Messages API support to openrouter proxy#1122
Merged
chrarnoldus merged 26 commits intomainfrom Mar 18, 2026
Merged
feat(gateway): add Anthropic Messages API support to openrouter proxy#1122chrarnoldus merged 26 commits intomainfrom
chrarnoldus merged 26 commits intomainfrom
Conversation
Contributor
Author
Code Review SummaryStatus: 16 Issues Found | Recommendation: Address before merge Overview
Fix these issues in Kilo Cloud Issue Details (click to expand)CRITICAL
WARNING
Other Observations (not in diff)Issues found in unchanged code that cannot receive inline comments:
Files Reviewed (68 files)
Reviewed by gpt-5.4-20260305 · 1,938,755 tokens |
# Conflicts: # src/app/api/openrouter/[...path]/route.ts # src/lib/processUsage.ts
e8761fe to
7602d6d
Compare
jrf0110
reviewed
Mar 18, 2026
jrf0110
reviewed
Mar 18, 2026
jrf0110
reviewed
Mar 18, 2026
| const inferredUpstream_USD = openrouterCost_USD * OPENROUTER_BYOK_COST_MULTIPLIER; | ||
| const microdollar_error = (inferredUpstream_USD - upstream_inference_cost_USD) * 1000000; | ||
| if ( | ||
| (is_byok == null && (openrouterCost_USD || upstream_inference_cost_USD)) || |
Contributor
There was a problem hiding this comment.
I feel like we should at least a comment on some of this logic. So when the request isn't byok and there is either a openrouter cost reported or upstream cost reported, then it's considered suspicious? Why?
Contributor
There was a problem hiding this comment.
this file is vibe copied, I'll go through it for the next PR (the feature is only accessible for kilo accounts for now)
jrf0110
approved these changes
Mar 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds support for the Anthropic Messages API to the OpenRouter proxy route (
/api/openrouter/messagesand/api/gateway/messages), in addition to the existing OpenAI chat completions (/chat/completions) and Responses API (/responses) support.Key changes:
types.ts: AddedGatewayMessagesRequesttype (Anthropic Messages format withmodel,max_tokens,messages,system,stream,tools, etc.) and extended theGatewayRequestdiscriminated union with themessageskind.route.ts: ExtendedvalidatePathto accept/messages, added body parsing for the messages format, applied the same admin-only guard as the Responses API, handled prompt info extraction and free-model rewriting for the new kind.processUsage.messages.ts(new file): Streaming and non-streaming usage parsing for Anthropic's SSE format (message_start→ input tokens,message_delta→ output tokens + stop reason), with OpenRouter cost field handling mirroring the existing chat completions and responses parsers.processUsage.ts: Wired the newmessagesapi_kind intocountAndStoreUsage.request-helpers.ts:getMaxTokensnow handles the messages kind (returnsmax_tokens).api-metrics.server.ts:getToolsAvailableandgetToolsUsedhandle Anthropic tool format (tools have a top-levelname, tool use appears astool_usecontent blocks in assistant messages).abuse-service.ts:extractFullPromptshandles the messages kind (top-levelsystemfield + user message content extraction).providers/index.ts:openRouterRequestbody parameter type updated to includeGatewayMessagesRequest.The Messages API endpoint is gated behind
is_admin(same as the Responses API) while it's experimental.Verification
message_startcarriesusage.input_tokens,message_deltacarriesusage.output_tokens.Visual Changes
N/A
Reviewer Notes
/messages) is forwarded as-is to OpenRouter at${provider.apiUrl}/messages— OpenRouter supports the native Anthropic Messages API format at this path.wrapInSafeNextResponse.customLlmRequestonly handles chat completions.applyAnthropicModelSettingsare already guarded behindkind === 'chat_completions'; clients using the native Messages API are responsible for their own cache control markup.