feat: add native Anthropic Messages API proxy endpoint#859
Merged
simple-agent-manager[bot] merged 5 commits intomainfrom Apr 30, 2026
Merged
feat: add native Anthropic Messages API proxy endpoint#859simple-agent-manager[bot] merged 5 commits intomainfrom
simple-agent-manager[bot] merged 5 commits intomainfrom
Conversation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add `/ai/anthropic/v1/messages` endpoint for Claude Code to use SAM's proxy via ANTHROPIC_BASE_URL. The endpoint receives native Anthropic format and forwards to Cloudflare AI Gateway — no translation needed. - New route: ai-proxy-anthropic.ts with POST /messages and POST /messages/count_tokens - Auth via x-api-key header (Claude Code's auth format) - Forwards anthropic-version and anthropic-beta headers - SSE streaming pass-through without transformation - Shared helpers extracted to ai-proxy-shared.ts (auth, rate limit, metadata, API key resolution) - Existing ai-proxy.ts refactored to use shared helpers - Unit tests for shared helpers, model validation, URL builders - Integration tests in worker-smoke.test.ts Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…g, error sanitization - Change token scope guard from blocklist (!=node) to allowlist (===workspace) - Add rate limiting and token budget to count_tokens endpoint - Parse and validate count_tokens body (model required, must be claude-*) - Sanitize upstream error responses (don't pass raw Anthropic errors to client) - Move isAnthropicModel to ai-proxy-shared.ts (eliminate 3-way duplication) - Add x-api-key, anthropic-version, anthropic-beta to CORS allowHeaders - Remove unnecessary Connection/X-Accel-Buffering headers from streaming response - Use upstream content-type in count_tokens response Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
25 tasks
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
/ai/anthropic/v1/messagesso Claude Code can use SAM's proxy viaANTHROPIC_BASE_URLextractCallbackToken,verifyAIProxyAuth,buildAIGatewayMetadata, etc.) intoai-proxy-shared.tsto avoid duplication between OpenAI and Anthropic proxy endpointsai-proxy.tsto use shared helpers (no behavior change)x-api-keyheader (Claude Code's native auth format) with workspace callback tokenPOST /ai/anthropic/v1/messages/count_tokensendpoint for token countingValidation
pnpm lint— 0 errors (498 pre-existing warnings)pnpm typecheck— all 16 tasks passpnpm test— 4206/4206 API tests pass (pre-existing UI ButtonGroup test failure unrelated)pnpm build— all packages build successfullyStaging Verification (REQUIRED for all code changes — merge-blocking)
/healthreturns healthy,/ai/v1/modelsreturns model list, existing OpenAI proxy unaffectedStaging Verification Evidence
Full end-to-end chat verification requires a configured Anthropic platform credential on staging. The auth pipeline, error formatting, rate limiting, model validation, and route mounting are all verified. Without a platform credential, the endpoint correctly returns 503 "No Anthropic API key configured."
UI Compliance Checklist (Required for UI changes)
N/A: no UI changes
End-to-End Verification (Required for multi-component changes)
Data Flow Trace
POST /ai/anthropic/v1/messageswithx-api-key: <callback-token>→
apps/api/src/index.tsroutes toaiProxyAnthropicRoutes→
apps/api/src/routes/ai-proxy-anthropic.ts:post('/messages')extractCallbackToken()readsx-api-keyheader→
verifyAIProxyAuth()verifies JWT, looks up workspace→userId/projectId→
apps/api/src/services/ai-proxy-shared.ts:50-92checkRateLimit()with sharedai-proxykey→
apps/api/src/middleware/rate-limit.tsisAnthropicModel()— must be claude-*→
apps/api/src/services/ai-proxy-shared.ts:isAnthropicModel()checkTokenBudget()→
apps/api/src/services/ai-token-budget.tsresolveAnthropicApiKey()→ platform credential→
apps/api/src/services/ai-proxy-shared.ts:112-119fetch(buildAnthropicGatewayUrl(env), { headers: { 'x-api-key': key, 'cf-aig-metadata': metadata } })Untested Gaps
Post-Mortem (Required for bug fix PRs)
N/A: not a bug fix
Specialist Review Evidence (Required for agent-authored PRs)
needs-human-reviewlabel added and merge deferred to human — All completedExceptions (If any)
Agent Preflight (Required)
Classification
External References
ai-proxy.tsimplementation as reference for auth, rate limiting, token budget patternsCodebase Impact Analysis
apps/api/src/services/ai-proxy-shared.ts(NEW) — shared helpersapps/api/src/routes/ai-proxy-anthropic.ts(NEW) — new Anthropic proxy routeapps/api/src/routes/ai-proxy.ts(MODIFIED) — refactored to use shared helpersapps/api/src/index.ts(MODIFIED) — route mounting + CORS headersapps/api/tests/— new unit tests + updated smoke testsDocumentation & Specs
CLAUDE.md— Recent Changes section updated with anthropic-proxy-endpoint entryConstitution & Risk Check