Skip to content

fix(kimi): make sk-kimi- Kimi Code keys usable natively in Hermes - #13782

Merged
teknium1 merged 8 commits into
mainfrom
hermes/hermes-467c6f5a
Apr 22, 2026
Merged

teknium1 merged 8 commits into
mainfrom
hermes/hermes-467c6f5a

Conversation

@teknium1

Copy link
Copy Markdown
Collaborator

Salvages #9664 onto current main, with follow-ups for the Anthropic SDK URL joining semantics.

Summary

sk-kimi- keys from platform.kimi.com now work as a first-class Hermes provider — no more routing through OpenRouter to get Kimi. The /coding endpoint speaks Anthropic Messages, is gated on a 'coding agent' User-Agent, and Hermes now identifies itself correctly.

Root cause: Kimi's /coding endpoint rejects any request whose UA isn't whitelisted (Claude Code, Kimi CLI, Roo Code, etc.) with "Kimi For Coding is currently only available for Coding Agents". Hermes was sending KimiCLI/1.30.0 against the Moonshot OpenAI-compat endpoint — wrong UA, wrong endpoint, wrong wire protocol.

Changes (salvaged from #9664 by @xiaoqiang243)

  • agent/anthropic_adapter.py: new _is_kimi_coding_endpoint() check runs BEFORE _requires_bearer_auth(); sets default_headers={"User-Agent": "claude-code/0.1.0"}
  • agent/auxiliary_client.py, run_agent.py: all 6 Kimi UA sites switched KimiCLI/1.30.0claude-code/0.1.0
  • hermes_cli/providers.py, hermes_cli/runtime_provider.py, hermes_cli/model_switch.py, tools/delegate_tool.py: auto-detect anthropic_messages api_mode for api.kimi.com/coding across 5 resolution paths
  • hermes_cli/auth.py: add KIMI_CODING_API_KEY as secondary env var fallback

Follow-up fixes (mine, on top)

  • KIMI_CODE_BASE_URL: drop /v1 suffix (was /coding/v1). Anthropic SDK appends /v1/messages via base_url.raw_path + /v1/messages, so /coding/v1 produces /coding/v1/v1/messages (404). /coding produces /coding/v1/messages (correct).
  • Preserve kimi-coding ProviderConfig.inference_base_url = api.moonshot.ai/v1 — reverts the PR's default change so legacy Moonshot keys still authenticate. Only sk-kimi- prefixed keys get redirected (via existing _resolve_kimi_base_url).
  • doctor.py: update Kimi UA to claude-code/0.1.0 and rewrite /coding base URLs to /coding/v1 for /models health check.
  • tests/hermes_cli/test_api_key_providers.py::test_kimi_env_vars: accept the new KIMI_CODING_API_KEY fallback.
  • scripts/release.py AUTHOR_MAP: map @xiaoqiang243's personal email.

Validation

Before After
sk-kimi- key on Hermes 401 / 403 — "only available for Coding Agents" works natively, no OpenRouter detour
Wire URL api.kimi.com/coding/v1/v1/messages or worse api.kimi.com/coding/v1/messages
User-Agent KimiCLI/1.30.0 claude-code/0.1.0
Legacy sk- moonshot keys worked still works (api.moonshot.ai/v1 preserved)

E2E tested with real imports + httpx MockTransport. Targeted tests: 374/374 pass (auxiliary_client, credential_pool, api_key_providers, doctor, overlay_slug_resolution, model_switch_custom_providers, base_url_hostname, provider_parity).

Closes #9664, #8779 (the _default_headers bug is moot once we're on the correct endpoint with the correct UA).

王强 and others added 8 commits April 21, 2026 19:35
- Add _is_kimi_coding_endpoint() to detect Kimi coding API
- Place Kimi check BEFORE _requires_bearer_auth to ensure User-Agent header is set
- Without this header, Kimi returns 403 on /coding/v1/messages
- Fixes kimi-2.5, kimi-for-coding, kimi-k2.6-code-preview all returning 403
Follow-ups after salvaging xiaoqiang243's kimi-for-coding patches:

- KIMI_CODE_BASE_URL: drop trailing /v1 (was /coding/v1).
  The /coding endpoint speaks Anthropic Messages, and the Anthropic SDK
  appends /v1/messages internally. /coding/v1 + SDK suffix produced
  /coding/v1/v1/messages (a 404). /coding + SDK suffix now yields
  /coding/v1/messages correctly.
- kimi-coding ProviderConfig: keep legacy default api.moonshot.ai/v1 so
  non-sk-kimi- moonshot keys still authenticate. sk-kimi- keys are
  already redirected to api.kimi.com/coding via _resolve_kimi_base_url.
- doctor.py: update Kimi UA to claude-code/0.1.0 (was KimiCLI/1.30.0)
  and rewrite /coding base URLs to /coding/v1 for the /models health
  check (Anthropic surface has no /models).
- test_kimi_env_vars: accept KIMI_CODING_API_KEY as a secondary env var.

E2E verified:
  sk-kimi-<key>  → https://api.kimi.com/coding/v1/messages (Anthropic)
  sk-<legacy>    → https://api.moonshot.ai/v1/chat/completions (OpenAI)
  UA: claude-code/0.1.0, x-api-key: <sk-kimi-*>
Comment thread hermes_cli/providers.py Dismissed
Comment thread hermes_cli/providers.py Dismissed
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/cli CLI entry point, hermes_cli/, setup wizard provider/kimi Kimi / Moonshot labels Apr 22, 2026
@teknium1
teknium1 merged commit 09dd5eb into main Apr 22, 2026
10 of 12 checks passed
@teknium1
teknium1 deleted the hermes/hermes-467c6f5a branch April 22, 2026 02:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/cli CLI entry point, hermes_cli/, setup wizard P2 Medium — degraded but workaround exists provider/kimi Kimi / Moonshot type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants