Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
feat(credits): Allow to configure model-credit mapping (#13274)
-LAN-04d13a81168d
diff --git a/api/configs/feature/hosted_service/__init__.py b/api/configs/feature/hosted_service/__init__.pyindex 7dd47e36581..63aec890383 100644--- a/api/configs/feature/hosted_service/__init__.py+++ b/api/configs/feature/hosted_service/__init__.py@@ -1,9 +1,40 @@from typing import Optional-from pydantic import Field, NonNegativeInt+from pydantic import Field, NonNegativeInt, computed_fieldfrom pydantic_settings import BaseSettings+class HostedCreditConfig(BaseSettings):+ HOSTED_MODEL_CREDIT_CONFIG: str = Field(+ description="Model credit configuration in format 'model:credits,model:credits', e.g., 'gpt-4:20,gpt-4o:10'",+ default="",+ )++ def get_model_credits(self, model_name: str) -> int:+ """+ Get credit value for a specific model name.+ Returns 1 if model is not found in configuration (default credit).++ :param model_name: The name of the model to search for+ :return: The credit value for the model+ """+ if not self.HOSTED_MODEL_CREDIT_CONFIG:+ return 1++ try:+ credit_map = dict(+ item.strip().split(":", 1) for item in self.HOSTED_MODEL_CREDIT_CONFIG.split(",") if ":" in item+ )++ # Search for matching model pattern+ for pattern, credit in credit_map.items():+ if pattern.strip() in model_name:+ return int(credit)+ return 1 # Default quota if no match found+ except (ValueError, AttributeError):+ return 1 # Return default quota if parsing fails++class HostedOpenAiConfig(BaseSettings):"""Configuration for hosted OpenAI service@@ -202,5 +233,7 @@ class HostedServiceConfig(HostedZhipuAIConfig,# moderationHostedModerationConfig,+ # credit config+ HostedCreditConfig,):passdiff --git a/api/core/workflow/nodes/llm/node.py b/api/core/workflow/nodes/llm/node.pyindex 6a4f8c4e207..7e28aa7a3ff 100644--- a/api/core/workflow/nodes/llm/node.py+++ b/api/core/workflow/nodes/llm/node.py@@ -3,6 +3,7 @@ import loggingfrom collections.abc import Generator, Mapping, Sequencefrom typing import TYPE_CHECKING, Any, Optional, cast+from configs import dify_configfrom core.app.entities.app_invoke_entities import ModelConfigWithCredentialsEntityfrom core.entities.model_entities import ModelStatusfrom core.entities.provider_entities import QuotaUnit@@ -732,10 +733,7 @@ class LLMNode(BaseNode[LLMNodeData]):if quota_unit == QuotaUnit.TOKENS:used_quota = usage.total_tokenselif quota_unit == QuotaUnit.CREDITS:- used_quota = 1-- if "gpt-4" in model_instance.model:- used_quota = 20+ used_quota = dify_config.get_model_credits(model_instance.model)else:used_quota = 1diff --git a/api/events/event_handlers/deduct_quota_when_message_created.py b/api/events/event_handlers/deduct_quota_when_message_created.pyindex 1ed37efba0b..d196a486201 100644--- a/api/events/event_handlers/deduct_quota_when_message_created.py+++ b/api/events/event_handlers/deduct_quota_when_message_created.py@@ -1,3 +1,4 @@+from configs import dify_configfrom core.app.entities.app_invoke_entities import AgentChatAppGenerateEntity, ChatAppGenerateEntityfrom core.entities.provider_entities import QuotaUnitfrom events.message_event import message_was_created@@ -37,10 +38,7 @@ def handle(sender, **kwargs):if quota_unit == QuotaUnit.TOKENS:used_quota = message.message_tokens + message.answer_tokenselif quota_unit == QuotaUnit.CREDITS:- used_quota = 1-- if "gpt-4" in model_config.model:- used_quota = 20+ used_quota = dify_config.get_model_credits(model_config.model)else:used_quota = 1