This guide outlines the full integration process for adding a new LLM model, whether it connects via OpenRouter or directly through SDKs like OpenAI, Claude, Groq, or Mistral.
1

Integration Flow Overview

  1. Choose between integration types:
    • OpenRouter-based: for models like RAG, Simple Tool Chat, CustomGPT, etc.
    • Direct SDK-based: for OpenAI, Claude, Groq, Mistral, etc.
  2. Update relevant services, controllers, prompts, SDKs, and model metadata.
2

OpenRouter Integration (Python stack)

  • Service Layer: src/chatflow_langchain/service/
    • Add folders/files for the model service (e.g., custom_gpt, title, simple_chat)
  • Controller Logic: src/chatflow_langchain/controller/
    • Update controllers to handle logic and registration via OpenRouter
  • Prompt Templates: src/prompt/langchain/
    • Define or update prompt templates and config for the new model
  • Callbacks: src/customlib/langchain/
    • Implement logic for:
      • MongoDB persistence
      • Token usage tracking
      • Streaming response management
3

Direct SDK Integration (Python stack)

  • Dependencies: requirements.txt
    • Add SDKs like openai, anthropic, mistral, etc.
  • ⚙️ Model Config: src/chatflow_langchain/service/model/config.py
    • Add default behavior, token limits, and metadata
  • Optional Caching: chatopenai_cache.py
    • Add caching, usage tracking, refresh/expiry logic (if OpenAI-specific)
  • Controller Updates: src/chatflow_langchain/controller/
    • Add routing and provider logic
  • Prompt Updates: src/prompt/langchain/
    • Align template tone, structure, and behavior for the model
4

Node.js Backend Model Configuration

  • Constants File: src/config/constants/aimodal.js
    • Add new model constant
    • Add to OPENROUTER_PROVIDER, MODAL_NAME
  • Common Constants: src/config/constants/common.js
    • Add model code in MODEL_CODE
  • API Key Logic: src/services/company.js
  • Extend checkApiKey(...) to support the new model
  • Define a checker function for API validation
5

Next.js Frontend Integration

  • Model Icon: Place logo in public/ directory
  • Constant Mapping: src/utils/constant.ts
    • Add model code, display name, image path, credit info, and sequence list
  • Optional Image Feature Support: src/utils/helper.ts
    • If model supports image generation or conversation, update:
      • allowImageGeneration
      • allowImageConversation
6

Database Migration and Model Metadata

Model Collection

Defines global model metadata:
{
  "title": "Model Name",
  "code": "MODEL_CODE",
  "isActive": true
}

CompanyModel Collection

Links model to a company and includes config:
{
  "name": "model-name",
  "bot": {
    "title": "Model Name",
    "code": "MODEL_CODE",
    "id": "ModelCollectionId"
  },
  "company": {
    "name": "Company Name",
    "slug": "company-slug",
    "id": "CompanyId"
  },
  "config": {
    "apikey": "your-api-key"
  },
  "modelType": 2,
  "dimensions": 1536,
  "isActive": true,
  "extraConfig": {
    "stop": [],
    "temperature": 0.7,
    "tools": []
  }
}
7

Summary of Changes by Path

PathPurpose
src/chatflow_langchain/service/Define services per model
src/chatflow_langchain/controller/Register & route models
src/prompt/langchain/Add prompt logic/templates
src/customlib/langchain/Callbacks & token tracking
requirements.txtAdd required SDKs
chatopenai_cache.pyOpenAI-specific cache logic
src/config/constants/Node model constants
src/services/company.jsAPI key checker integration
src/utils/constant.tsModel image and metadata
public/Model logos/icons
Always test end-to-end from model selection to final response and cost tracking. Ensure prompt behavior, tool compatibility, and API key validation are functioning as expected.