[PR #174] [CLOSED] feat: Native Claude Endpoint Support & Enhanced Token Calculation #284

Closed
opened 2026-02-27 07:18:49 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/justlovemaki/AIClient-2-API/pull/174
Author: @tickernelz
Created: 1/6/2026
Status: Closed

Base: mainHead: main


📝 Commits (4)

  • a5d9d89 feat: add token logging feature and enhance API request handling
  • ed652b7 fix: correct indentation for customNameDisplay in handleStreamRequest function
  • 6d6c01c fix: remove unnecessary whitespace and improve code readability in toGeminiRequest method
  • 8fc25a8 fix: remove jsonrepair dependency and fix code formatting

📊 Changes

8 files changed (+422 additions, -285 deletions)

View changed files

📝 src/api-manager.js (+8 -0)
📝 src/claude/claude-kiro.js (+358 -257)
📝 src/common.js (+8 -3)
📝 src/converters/strategies/ClaudeConverter.js (+14 -12)
📝 src/ui-manager.js (+2 -0)
📝 static/app/config-manager.js (+3 -0)
📝 static/app/i18n.js (+6 -0)
📝 static/index.html (+23 -13)

📄 Description

Summary

This update introduces full native support for Anthropic Claude API endpoints (/messages and /models), enhances token calculation accuracy for Claude Kiro, and implements a configurable token logging feature. These improvements strengthen Anthropic API compatibility and provide granular visibility into token consumption patterns.

Main Features

1. Native Claude Endpoint Support

  • Implemented /messages endpoint for full Anthropic API compatibility
  • Added /models endpoint to retrieve model information
  • Processes native Claude request/response formats without transformation
  • Maintains backward compatibility with existing /v1/messages endpoint

Implementation Files:

  • src/api-manager.js (lines 34-36, 59-62): Routing configuration for native endpoints
  • src/common.js (lines 54, 329-354): Added CLAUDE_MODEL_LIST endpoint handling logic

2. Enhanced Token Calculation for Claude Kiro

  • Implemented precise token accounting with detailed breakdown:
    • System tokens (prompt context)
    • Message tokens (text, tool interactions, media)
    • Tool definition tokens (specifications and schemas)
  • Added token estimation for multimodal inputs:
    • Images: 1600 tokens per image
    • Documents: Base64 length × 0.75 / 4
  • Differentiated input/output token tracking during streaming

Implementation Files:

  • src/claude/claude-kiro.js (lines 1638-1710): Refactored estimateInputTokens() with breakdown logic
  • src/claude/claude-kiro.js (lines 1421-1593): Enhanced token logging in streaming responses

3. Configurable Token Logging

  • Introduced ENABLE_TOKEN_LOGGING configuration flag
  • Provides detailed token metrics for streaming and non-streaming requests
  • Console output includes breakdown of input, output, and tool call tokens
  • Configuration toggle available in application settings UI

Implementation Files:

  • src/claude/claude-kiro.js (lines 1165-1168, 1395-1397, 1549-1551, 1704-1706): Conditional logging implementation
  • static/app/config-manager.js (lines 70, 84): UI configuration handling
  • static/app/i18n.js: Localization strings for settings
  • static/index.html: Settings panel UI element

4. Model List Conversion Enhancement

  • Optimized model list transformation logic for native Claude endpoints
  • Implemented bypass for native Anthropic endpoints
  • Maintained OpenAI/Gemini format conversion for legacy compatibility

Implementation Files:

  • src/common.js (lines 347-354): Conditional conversion logic
  • src/converters/strategies/ClaudeConverter.js (lines 607-619, 624-637): Model list transformation strategies

5. Code Quality Improvements

  • Standardized formatting and whitespace consistency
  • Enhanced error handling with contextual logging
  • Improved streaming response metadata generation
  • Strengthened input validation at API boundaries

Technical Details

New API Endpoints

GET  /models              → Retrieves native Claude model catalog
POST /messages            → Processes messages using Anthropic API specification

Token Calculation Methodology

Input Tokens = System Tokens + Messages Tokens + Tools Tokens

Breakdown:
- System Tokens: Character-based estimation of system prompt
- Messages Tokens:
  * Text: ~4 characters per token
  * Tool interactions: Function name + JSON-serialized parameters/results
  * Images: Fixed 1600 tokens per image
  * Documents: Base64 length × 0.75 / 4
- Tools Tokens: Function name + description + JSON schema size

Configuration Parameter

{
  "ENABLE_TOKEN_LOGGING": true  // Enables detailed token consumption logging
}

Testing Recommendations

  1. Native Endpoint Validation:

    • Verify /messages endpoint compatibility with Anthropic SDK clients
    • Validate /models response structure against Anthropic specification
    • Confirm backward compatibility of /v1/messages endpoint
  2. Token Accounting Verification:

    • Compare estimated versus actual tokens from Anthropic API responses
    • Test multimodal scenarios (images, documents, tool calls)
    • Validate breakdown accuracy across message types
  3. Conversion Compatibility:

    • Validate OpenAI/Gemini model format conversion
    • Test tool call parameter transformations
    • Verify streaming response metadata consistency

Impact Analysis

  • Breaking Changes: None
  • Security Impact: Positive (enhanced input validation reduces injection risks)
  • Resolves compatibility limitations with native Anthropic API clients
  • Addresses token undercounting in multimodal and tool-using scenarios
  • Provides operational visibility into token consumption patterns

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/justlovemaki/AIClient-2-API/pull/174 **Author:** [@tickernelz](https://github.com/tickernelz) **Created:** 1/6/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (4) - [`a5d9d89`](https://github.com/justlovemaki/AIClient-2-API/commit/a5d9d8925396fa1b140a79f7026cfd04556f525d) feat: add token logging feature and enhance API request handling - [`ed652b7`](https://github.com/justlovemaki/AIClient-2-API/commit/ed652b7939b2bf7307a3de3b0096948058ef44f4) fix: correct indentation for customNameDisplay in handleStreamRequest function - [`6d6c01c`](https://github.com/justlovemaki/AIClient-2-API/commit/6d6c01c850be4bfb6bff68f3c442cbf58c6cb362) fix: remove unnecessary whitespace and improve code readability in toGeminiRequest method - [`8fc25a8`](https://github.com/justlovemaki/AIClient-2-API/commit/8fc25a80446abe1fe47ba4e26f9d870535d6ac1a) fix: remove jsonrepair dependency and fix code formatting ### 📊 Changes **8 files changed** (+422 additions, -285 deletions) <details> <summary>View changed files</summary> 📝 `src/api-manager.js` (+8 -0) 📝 `src/claude/claude-kiro.js` (+358 -257) 📝 `src/common.js` (+8 -3) 📝 `src/converters/strategies/ClaudeConverter.js` (+14 -12) 📝 `src/ui-manager.js` (+2 -0) 📝 `static/app/config-manager.js` (+3 -0) 📝 `static/app/i18n.js` (+6 -0) 📝 `static/index.html` (+23 -13) </details> ### 📄 Description ## Summary This update introduces full native support for Anthropic Claude API endpoints (`/messages` and `/models`), enhances token calculation accuracy for Claude Kiro, and implements a configurable token logging feature. These improvements strengthen Anthropic API compatibility and provide granular visibility into token consumption patterns. ## Main Features ### 1. Native Claude Endpoint Support - Implemented `/messages` endpoint for full Anthropic API compatibility - Added `/models` endpoint to retrieve model information - Processes native Claude request/response formats without transformation - Maintains backward compatibility with existing `/v1/messages` endpoint **Implementation Files:** - `src/api-manager.js` (lines 34-36, 59-62): Routing configuration for native endpoints - `src/common.js` (lines 54, 329-354): Added `CLAUDE_MODEL_LIST` endpoint handling logic ### 2. Enhanced Token Calculation for Claude Kiro - Implemented precise token accounting with detailed breakdown: - System tokens (prompt context) - Message tokens (text, tool interactions, media) - Tool definition tokens (specifications and schemas) - Added token estimation for multimodal inputs: - Images: 1600 tokens per image - Documents: Base64 length × 0.75 / 4 - Differentiated input/output token tracking during streaming **Implementation Files:** - `src/claude/claude-kiro.js` (lines 1638-1710): Refactored `estimateInputTokens()` with breakdown logic - `src/claude/claude-kiro.js` (lines 1421-1593): Enhanced token logging in streaming responses ### 3. Configurable Token Logging - Introduced `ENABLE_TOKEN_LOGGING` configuration flag - Provides detailed token metrics for streaming and non-streaming requests - Console output includes breakdown of input, output, and tool call tokens - Configuration toggle available in application settings UI **Implementation Files:** - `src/claude/claude-kiro.js` (lines 1165-1168, 1395-1397, 1549-1551, 1704-1706): Conditional logging implementation - `static/app/config-manager.js` (lines 70, 84): UI configuration handling - `static/app/i18n.js`: Localization strings for settings - `static/index.html`: Settings panel UI element ### 4. Model List Conversion Enhancement - Optimized model list transformation logic for native Claude endpoints - Implemented bypass for native Anthropic endpoints - Maintained OpenAI/Gemini format conversion for legacy compatibility **Implementation Files:** - `src/common.js` (lines 347-354): Conditional conversion logic - `src/converters/strategies/ClaudeConverter.js` (lines 607-619, 624-637): Model list transformation strategies ### 5. Code Quality Improvements - Standardized formatting and whitespace consistency - Enhanced error handling with contextual logging - Improved streaming response metadata generation - Strengthened input validation at API boundaries ## Technical Details ### New API Endpoints ``` GET /models → Retrieves native Claude model catalog POST /messages → Processes messages using Anthropic API specification ``` ### Token Calculation Methodology ``` Input Tokens = System Tokens + Messages Tokens + Tools Tokens Breakdown: - System Tokens: Character-based estimation of system prompt - Messages Tokens: * Text: ~4 characters per token * Tool interactions: Function name + JSON-serialized parameters/results * Images: Fixed 1600 tokens per image * Documents: Base64 length × 0.75 / 4 - Tools Tokens: Function name + description + JSON schema size ``` ### Configuration Parameter ```json { "ENABLE_TOKEN_LOGGING": true // Enables detailed token consumption logging } ``` ## Testing Recommendations 1. **Native Endpoint Validation:** - Verify `/messages` endpoint compatibility with Anthropic SDK clients - Validate `/models` response structure against Anthropic specification - Confirm backward compatibility of `/v1/messages` endpoint 2. **Token Accounting Verification:** - Compare estimated versus actual tokens from Anthropic API responses - Test multimodal scenarios (images, documents, tool calls) - Validate breakdown accuracy across message types 3. **Conversion Compatibility:** - Validate OpenAI/Gemini model format conversion - Test tool call parameter transformations - Verify streaming response metadata consistency ## Impact Analysis - **Breaking Changes:** None - **Security Impact:** Positive (enhanced input validation reduces injection risks) ## Related Issues - Resolves compatibility limitations with native Anthropic API clients - Addresses token undercounting in multimodal and tool-using scenarios - Provides operational visibility into token consumption patterns --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-27 07:18:49 +03:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/AIClient-2-API-justlovemaki#284
No description provided.