跳到正文
DEV Community· Kantro·· 3 小時前AI 評分35

Kantro 原生協議錯誤格式探測:/v1/messages 與 /v1/responses

Probing native protocol error shapes on /v1/messages and /v1/responses

AI 導讀

Kantro 對 /v1/messages、/v1/messages/count_tokens、/v1/responses 及 /v1/responses/compact 發出未驗證請求,檢查四個原生端點的 401 錯誤格式。

正文

Cover image for Probing native protocol error shapes on /v1/messages and /v1/responses

Kantro

Most LLM proxies advertise "OpenAI-compatible" endpoints and stop at /v1/chat/completions. That works fine for a single chat call, then breaks the moment you point Claude Code or the Codex CLI at the proxy. Claude Code expects Anthropic's /v1/messages and /v1/messages/count_tokens shapes (including type: "error" envelopes on 401s), and newer OpenAI clients call /v1/responses and /v1/responses/compact.

On Kantro, we kept the native endpoints separate instead of translating everything through a single schema. I wanted to verify whether unauthenticated error envelopes and route coverage actually stay native on the live edge, so I probed the four less-common endpoints directly against kantro.dev:

for ep in /v1/messages /v1/messages/count_tokens /v1/responses /v1/responses/compact; do
  echo "=== $ep ==="
  curl -sS -w "\nHTTP:%{http_code}\n" -X POST "https://kantro.dev$ep" \
    -H "Content-Type: application/json" -d '{}'
done

Here is what came back:

=== /v1/messages ===
{"type":"error","error":{"type":"authentication_error","message":"Invalid Kantro API key. Check your Bearer token or generate a key: https://kantro.dev/app/keys"}}
HTTP:401

=== /v1/messages/count_tokens ===
{"type":"error","error":{"type":"authentication_error","message":"Invalid Kantro API key. Check your Bearer token or generate a key: https://kantro.dev/app/keys"}}
HTTP:401

=== /v1/responses ===
{"error":{"message":"Invalid Kantro API key. Check your Bearer token or generate a key: https://kantro.dev/app/keys","type":"invalid_request_error","param":null,"code":"invalid_api_key"}}
HTTP:401

=== /v1/responses/compact ===
{"error":{"message":"Invalid Kantro API key. Check your Bearer token or generate a key: https://kantro.dev/app/keys","type":"invalid_request_error","param":null,"code":"invalid_api_key"}}
HTTP:401

Both Anthropic paths return Anthropic's authentication_error wrapper, while both OpenAI paths return OpenAI's invalid_request_error shape with param and code. That matters because official SDKs parse the error envelope differently before they even reach your retry logic. If a gateway returns an OpenAI error JSON on /v1/messages, the Anthropic SDK's error parser falls back to an untyped error.

Checking the OpenAPI spec also turned up /v1/key, /v1/keys, and /v1/keys/{hash} alongside /v1/models for inspecting per-key spend limits and rotating keys programmatically. On the CLI side, npx kantro-setup writes keys to ~/.config/kantro/key (or reads KANTRO_API_KEY) and prints file diffs before editing local tool configs instead of dropping keys into a repo .env.

This only checks the unauthenticated 401 envelope and documented route surface. I did not run a streaming SSE diff against upstream Anthropic or OpenAI in this pass, so header and chunk parity on 200 responses still needs a keyed run to verify directly.

來源:DEV Community · dev.to