1. Core Integration Patterns

1.1 Unified Authentication and Routing

// Core logic: Replace API Key, Base URL, add APP-Code
const config = {
  apiKey: 'your-aihubmix-api-key',  // Replace with aihubmix API Key
  baseURL: 'https://aihubmix.com',   // Replace with aihubmix gateway
  headers: {
    'APP-Code': 'WHVL9885'  // Enjoy discount
  }
};

// Model routing rules
function routeModel(modelName: string) {
  if (modelName.startsWith('claude')) {
    // Claude models: Use Anthropic SDK
    return 'anthropic';
  } else if (modelName.startsWith('gemini') && !modelName.endsWith('-nothink') && !modelName.endsWith('-search')) {
    // Gemini models: Use Google SDK, endpoint https://aihubmix.com/gemini
    return 'gemini';
  } else {
    // Other models: Use OpenAI compatible interface
    return 'openai';
  }
}

1.2 Special Handling Points

  • Empty Tools Fix: When tools=[] and tool_choice exists, automatically remove tool_choice
  • File Extensions: Automatically set correct file extensions based on mediaType
  • Cache Control: Support <cache> tags for cache control

2. Unified Integration Implementation

2.1 Core Client Wrapper

class AihubmixModelClient {
  private config: {
    apiKey: string;
    baseURL: string;
    appCode: string;
  };

  constructor(apiKey: string) {
    this.config = {
      apiKey,
      baseURL: 'https://aihubmix.com',
      appCode: 'WHVL9885'
    };
  }

  async chatCompletion(model: string, messages: any[], options: any = {}) {
    // Automatically route to corresponding SDK based on model name
    if (model.startsWith('claude')) {
      return this.claudeCompletion(model, messages, options);
    } else if (model.startsWith('gemini')) {
      return this.geminiCompletion(model, messages, options);
    } else {
      return this.openaiCompletion(model, messages, options);
    }
  }

  private async claudeCompletion(model: string, messages: any[], options: any) {
    const { Anthropic } = await import('@anthropic-ai/sdk');
    const client = new Anthropic({
      apiKey: this.config.apiKey,
      baseURL: this.config.baseURL,
      defaultHeaders: { 'APP-Code': this.config.appCode }
    });
    return client.messages.create({ model, messages, ...options });
  }

  private async geminiCompletion(model: string, messages: any[], options: any) {
    const { GoogleGenerativeAI } = await import('@google/generative-ai');
    const genAI = new GoogleGenerativeAI(this.config.apiKey, {
      baseURL: `${this.config.baseURL}/gemini/v1beta`,
      defaultHeaders: { 'APP-Code': this.config.appCode }
    });
    const genModel = genAI.getGenerativeModel({ model });
    return genModel.generateContent(messages);
  }

  private async openaiCompletion(model: string, messages: any[], options: any) {
    const OpenAI = await import('openai');
    const client = new OpenAI.default({
      apiKey: this.config.apiKey,
      baseURL: `${this.config.baseURL}/v1`,
      defaultHeaders: { 'APP-Code': this.config.appCode }
    });
    return client.chat.completions.create({ model, messages, ...options });
  }
}

// Usage example
const client = new AihubmixModelClient('your-aihubmix-api-key');
await client.chatCompletion('gpt-4o-mini', messages);
await client.chatCompletion('claude-3-5-sonnet-20241022', messages);
await client.chatCompletion('gemini-2.5-flash', messages);

2.2 Special Handling and Utility Functions

// Empty tools fix
function fixToolChoice(requestBody: any): any {
  if (requestBody.tools?.length === 0 && requestBody.tool_choice) {
    delete requestBody.tool_choice;
  }
  return requestBody;
}

// File extension mapping
function setFileExtension(mediaType: string): string {
  const mimeToExt: Record<string, string> = {
    'audio/mpeg': 'mp3', 'audio/wav': 'wav', 'audio/flac': 'flac'
  };
  return mimeToExt[mediaType] || 'bin';
}

// Cache control
function processCacheTags(content: string): { content: string; cacheControl?: any } {
  if (content.includes('<cache>')) {
    return { content: content.replace('<cache>', ''), cacheControl: { type: 'ephemeral' } };
  }
  return { content };
}

3. Deployment and Configuration

3.1 Environment Variables

const config = {
  apiKey: process.env.AIHUBMIX_API_KEY || '',
  baseURL: process.env.AIHUBMIX_BASE_URL || 'https://aihubmix.com',
  appCode: process.env.AIHUBMIX_APP_CODE || 'WHVL9885'
};

3.2 Error Handling

class AihubmixError extends Error {
  constructor(message: string, public code?: string, public status?: number) {
    super(message);
    this.name = 'AihubmixError';
  }
}

function handleAihubmixErrors(error: any): AihubmixError {
  const message = error.message || 'Unknown error';
  if (message.toLowerCase().includes('rate limit')) {
    return new AihubmixError('Rate limit exceeded', 'RATE_LIMIT', 429);
  } else if (message.toLowerCase().includes('unauthorized')) {
    return new AihubmixError('Authentication failed', 'AUTH_ERROR', 401);
  } else {
    return new AihubmixError(message, error.code, error.status);
  }
}

4. Reference Implementations and Alignment Checklist

4.1 cherry-studio Client Reference (TypeScript)

The following key points from cherry-studio’s AihubmixAPIClient.ts can serve as a landing pattern for third-party frontend/desktop applications integrating with aihubmix on the TypeScript side:
  • Unified Discount Code Addition: Merge extra_headers at Provider level and set APP-Code (using MLTG2087 in the project)
  • Multi-client Routing:
    • claude* → Use Anthropic client
    • gemini*/imagen* that don’t end with -nothink/-search and don’t contain embedding → Use Gemini client (apiHost: https://aihubmix.com/gemini)
    • OpenAI series (excluding gpt-oss) → Use OpenAI compatible response client
    • Others → Fallback to default OpenAI client
  • BaseURL Retrieval: Export from current routed specific client, maintaining endpoint differences for each provider

4.2 dify-plugin-aihubmix Reference (Python)

The following key points from dify-plugin-aihubmix implementation can serve as a landing pattern for third-party Python tools integrating with aihubmix:
  • Unified Discount Code Addition: Merge extra_headers at Provider level and set APP-Code (using Dify2025 in the project)
  • Multi-client Routing:
    • claude* → Use Anthropic client
    • gemini*/imagen* that don’t end with -nothink/-search and don’t contain embedding → Use Gemini client (apiHost: https://aihubmix.com/gemini)
    • OpenAI series (excluding gpt-oss) → Use OpenAI compatible response client
    • Others → Fallback to default OpenAI client
  • BaseURL Retrieval: Export from current routed specific client, maintaining endpoint differences for each provider

4.3 Alignment Checklist

  • Provider entry unifies merging of extra_headers and injects APP-Code
  • Gemini client uses https://aihubmix.com/gemini as apiHost
  • Routing rules consistent with claude*, gemini*/imagen*, OpenAI series (excluding gpt-oss)
  • Default fallback to OpenAI client, maintaining OpenAI compatible interface behavior
  • getBaseURL() always exports from current routed client, avoiding hardcoding

5. Migration Checklist

  • Replace API Key with aihubmix API Key
  • Replace Base URL with https://aihubmix.com
  • Add APP-Code header for discount
  • Implement model routing logic (claude/gemini/openai)
  • Handle tool_choice fix when tools are empty
  • Configure MIME type handling for file uploads
  • Test various model calls