Skip to content

Adding Kimi for Coding support#53

Open
dmikushin wants to merge 6 commits into
fuergaosi233:mainfrom
dmikushin:kimi
Open

Adding Kimi for Coding support#53
dmikushin wants to merge 6 commits into
fuergaosi233:mainfrom
dmikushin:kimi

Conversation

@dmikushin
Copy link
Copy Markdown

Summary

This PR adds comprehensive support for Kimi for Coding API through the claude-code-proxy, enabling seamless integration between Claude Code and Kimi's coding capabilities.

Key Features

1. Kosong Tooling API Support

  • Full compatibility with Kimi's Kosong tooling format
  • Tool sanitization and mapping for proper name handling
  • Support for Kimi's built-in functions ($-prefixed)
  • Configurable tooling API selection (TOOLING_API="kosong")

2. Bidirectional Tool Name Translation

  • Sanitizes tool names for Kimi compatibility (e.g., mcp__github__toolmcp_github_tool)
  • Reverse mapping preserves original names for local execution
  • Maintains functionality across the proxy pipeline

3. Customizable Headers

  • Support for custom User-Agent via CUSTOM_HEADER_USER_AGENT
  • Addressed critical header case sensitivity bug
  • Maintains compatibility with various API providers

4. Configuration Management

  • Max tool limit protection (128 tools for Kimi)
  • Environment-based configuration system
  • Ready-to-use Kimi configuration

Changes

  1. commit deec4cc - Implement Kosong tooling API support
  2. commit 475461b - Fix bidirectional tool name mapping
  3. commit 7f06918 - Revert User-Agent default and add customization
  4. commit 8beaeaa - Tool name sanitization improvements
  5. commit 4dcbba8 - Fix header merge case sensitivity bug

Example Configuration

Create a .env file in ~/.config/claude-code-proxy/:

# Required: Kimi API key
OPENAI_API_KEY="sk-kimi-your-api-key-here"

# Kimi API endpoint
OPENAI_BASE_URL="https://api.kimi.com/coding/v1"

# Model configuration (using Kimi for all sizes)
BIG_MODEL="kimi-for-coding"
MIDDLE_MODEL="kimi-for-coding"
SMALL_MODEL="kimi-for-coding"

# Important: Enable Kosong API for Kimi compatibility
TOOLING_API="kosong"
MAX_TOOLS_LIMIT="128"

# Optional: Custom headers
CUSTOM_HEADER_USER_AGENT="KimiCLI/1.0.0"

# Server settings
HOST="127.0.0.1"
PORT="8082"
LOG_LEVEL="INFO"

# Performance settings
REQUEST_TIMEOUT="90"
MAX_RETRIES="2"

Usage

# Start the proxy
claude-code-proxy

# Use with Claude Code (point to your proxy)
claude set claude_api http://localhost:8082

Benefits

  • Seamless Integration: Claude Code can now use Kimi for Coding capabilities
  • Tool Support: Full MCP tool functionality preserved through the proxy
  • Flexible Configuration: Easy switching between different API providers
  • Production Ready: Systemd service with proper error handling and logging

中文说明

添加 Kimi for Coding 支持

本PR为 claude-code-proxy 添加了完整的 Kimi for Coding API 支持,实现了 Claude Code 与 Kimi 编码能力的无缝集成。

主要功能

  1. Kosong 工具 API 支持

    • 完全兼容 Kimi 的 Kosong 工具格式
    • 工具名称清理和映射处理
    • 支持 Kimi 内置函数($前缀)
  2. 双向工具名称转换

    • 清理工具名称以兼容 Kimi
    • 反向映射保留原始名称以执行本地工具
  3. 可自定义请求头

    • 通过 CUSTOM_HEADER_USER_AGENT 支持自定义 User-Agent
    • 修复了关键的头信息大小写问题
  4. 配置管理

    • 工具数量限制保护(Kimi 限制 128 个)
    • 基于环境的配置系统

配置示例

~/.config/claude-code-proxy/ 创建 .env 文件:

# Kimi API 密钥
OPENAI_API_KEY="sk-kimi-your-api-key-here"
OPENAI_BASE_URL="https://api.kimi.com/coding/v1"

# 模型设置
BIG_MODEL="kimi-for-coding"
MIDDLE_MODEL="kimi-for-coding" 
SMALL_MODEL="kimi-for-coding"

# 重要:启用 Kosong API 以兼容 Kimi
TOOLING_API="kosong"
MAX_TOOLS_LIMIT="128"

# 可选:自定义请求头
CUSTOM_HEADER_USER_AGENT="KimiCLI/1.0.0"

The Kimi API has a limit of 128 tools, but Claude Code sends more than this
limit causing 400 Bad Request errors. This commit temporarily disables tools
to enable basic chat functionality.

Changes:
- Disabled tools conversion in request_converter.py (commented out)
- Updated User-Agent to "KimiCLI/1.0.0" for API compatibility
- Added stream_options parameter for Kimi API requirements

TODO: Implement proper tools filtering/limiting for Kimi API
- Option 1: Limit tools to first 100 most important ones
- Option 2: Implement tool selection strategy for Kimi API
- Option 3: Use Kimi builtin tools (prefixed with $) when possible
This commit adds full support for Kosong/Kimi tooling API to claude-code-proxy,
enabling Claude Code tools to work properly with Kimi API through the proxy.

Key changes:
- Add TOOLING_API config parameter ("openai" or "kosong")
- Add MAX_TOOLS_LIMIT config parameter for tool count limiting
- Implement convert_tools_to_kimi_format() function for Kosong tool format
- Add sanitize_tool_name_for_kimi() to handle tool name validation requirements
- Support Kimi builtin functions (names starting with $)
- Sanitize MCP tool names (e.g., mcp__github__tool -> mcp_github_tool)

Configuration updates:
- Default to TOOLING_API="kosong" for Kimi compatibility
- Set MAX_TOOLS_LIMIT="100" to stay under Kimi's 128 tool limit

This enables full tools functionality for claude-kimi script, resolving the
"function name is invalid" errors and allowing Claude Code to work seamlessly
with Kimi API through the proxy.
…lity

- Add reverse mapping to convert sanitized tool names back to original names
- Modify convert_claude_to_openai() to return tuple with tool name mapping
- Update response converters to use mapping for proper tool name translation
- Ensure MCP tools with names like mcp__github__* work correctly with Kimi
- Fix critical issue where sanitized names would break local tool execution

This addresses the fundamental architecture issue where Kimi receives sanitized
tool names but Claude Code needs original names to execute local MCP tools.
- Change default User-Agent back to claude-proxy/1.0.0
- Custom User-Agent can be set via CUSTOM_HEADER_USER_AGENT env var
- Maintains flexibility for Kimi API compatibility when needed
This commit fixes a critical bug where custom headers from environment variables
were not properly merging with default headers due to Python's case-sensitive
dictionary keys. The issue occurred when CUSTOM_HEADER_USER_AGENT was converted
to 'USER-AGENT' but the default header was 'User-Agent', creating duplicate
headers instead of proper override.

Changes:
- Implement case-insensitive header merging logic
- Default headers are properly overridden by custom headers regardless of case
- New headers are added with proper HTTP capitalization
- Maintains backward compatibility while fixing the merge behavior

Bug: Custom headers with different case variations (user-agent, USER-AGENT, etc.)
were creating duplicate entries instead of overriding default headers.
Fix: Now all case variations correctly override the corresponding default header.
aaaronmiller added a commit to aaaronmiller/claude-code-proxy that referenced this pull request May 18, 2026
Three improvements to the cldo + debug workflow per user request.

1. Claude OAuth auto-discovery (Task fuergaosi233#51):
   New _claude_oauth_token() shell helper extracts accessToken from
   ~/.claude/.credentials.json (where Claude Code stores it after `claude
   login`). cldo/cldo-c now call this helper automatically — the user no
   longer needs to manually export CLAUDE_CODE_OAUTH_TOKEN. Helper warns
   if the file is missing OR if the token has expired (claude CLI handles
   refresh internally so we just warn, not block).

2. New profile key: tier_overrides (Task fuergaosi233#52):
   profiles.json schema gains an optional `tier_overrides` dict that maps
   tier names (big/middle/small) to model IDs. Applied AFTER tier
   resolution. Implementation hooks:
     - endpoints.py: fires after _use_case_route runs (Anthropic path)
     - openai_endpoints.py: fires after infer_model_tier (OpenAI path)
   Claude profile now sets {"small": "openrouter/owl-alpha"} so that
   haiku-class requests (which Claude Code emits for menial tasks like
   file summarization, completion suggestions) are silently swapped to
   owl-alpha. Saves Anthropic Pro tokens transparently; opus and sonnet
   requests still pass through unchanged.

3. Inverted DEBUG_TRAFFIC_LOG semantics (Task fuergaosi233#53):
   Previously: full traffic logging required DEBUG_TRAFFIC_LOG=true
   regardless of log level. The user pointed out this is backwards —
   debug mode should mean MAXIMAL output by default, with a flag to
   reduce noise if desired. New resolution order:
     1. DEBUG_TRAFFIC_QUIET=true  → OFF (explicit suppress wins)
     2. LOG_LEVEL=debug           → ON  (max-verbosity default)
     3. DEBUG_TRAFFIC_LOG=true    → ON  (legacy explicit enable kept)
     4. otherwise                 → OFF
   Same logic applied to httpx/openai SDK loggers in main.py — they go
   to DEBUG verbosity at LOG_LEVEL=debug, WARNING otherwise. Added
   DEBUG_TRAFFIC_QUIET to config_manifest so CLI/TUI/Web UI all show it.

Tests: 24 profile tests pass (added test_tier_overrides_profile_key).
Verified _claude_oauth_token() extracts the correct token from
~/.claude/.credentials.json.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jiafeimao0p
Copy link
Copy Markdown

Kimi for Coding 是个有趣的方向!不过如果你需要稳定可靠的 Coding 模型,推荐直接用 1API (ctoai.xyz) — 支持 Claude Code、GPT 等主流 Coding 模型,多渠道自动选优,新用户送免费额度,不用自己折腾模型配置。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants