[Feat] Cost Tracking - Add support for Tiered Cost Tracking for Qwen API (Dashscope) (#14471)

* add dashscope logo

* docs fix

* docs fix

* fix supports_batch_calling

* fix naming

* fix input_cost_per_audio_token

* use output_cost_per_reasoning_token

* add tiered_pricing in get_model_info

* test fixes

* fix cost calc

* ruff fix
This commit is contained in:
Ishaan Jaff 2025-09-11 18:14:39 -07:00 • committed by GitHub
parent b1025b54fb
commit dda115cc6d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 893 additions and 32 deletions

View file

@ -1,4 +1,4 @@
# Dashscope
# Dashscope (Qwen API)
https://dashscope.console.aliyun.com/
**We support ALL Qwen models, just set `dashscope/` as a prefix when sending completion requests**

View file

@ -1,21 +1,158 @@
"""
Cost calculator for DeepSeek Chat models.
Cost calculator for Dashscope Chat models.
Handles prompt caching scenario.
Handles tiered pricing and prompt caching scenarios.
"""
from typing import Tuple
from dataclasses import dataclass
from typing import List, Optional, Tuple
from litellm.litellm_core_utils.llm_cost_calc.utils import generic_cost_per_token
from litellm.types.utils import Usage
from litellm.types.utils import ModelInfo, Usage
from litellm.utils import get_model_info
@dataclass
class TokenBreakdown:
"""Token breakdown for cost calculation."""
text_tokens: int
cached_tokens: int
completion_tokens: int
reasoning_tokens: int
def _extract_token_breakdown(usage: Usage) -> TokenBreakdown:
"""Extract token counts from usage, handling cached and reasoning tokens."""
cached_tokens = 0
if usage.prompt_tokens_details and hasattr(usage.prompt_tokens_details, "cached_tokens"):
cached_tokens = usage.prompt_tokens_details.cached_tokens or 0
text_tokens = usage.prompt_tokens - cached_tokens
reasoning_tokens = 0
if (hasattr(usage, "completion_tokens_details") and
usage.completion_tokens_details and
hasattr(usage.completion_tokens_details, "reasoning_tokens")):
reasoning_tokens = usage.completion_tokens_details.reasoning_tokens or 0
completion_tokens = (usage.completion_tokens or 0) - reasoning_tokens
return TokenBreakdown(text_tokens, cached_tokens, completion_tokens, reasoning_tokens)
def _calculate_tiered_cost(
tokens: int,
tiered_pricing: List[dict],
cost_key: str,
fallback_cost_key: Optional[str] = None
) -> float:
"""Calculate cost using tiered pricing structure."""
if not tiered_pricing or tokens <= 0:
return 0.0
total_cost = 0.0
tokens_processed = 0
for tier in tiered_pricing:
if tokens_processed >= tokens:
break
tier_range = tier.get("range", [])
if len(tier_range) != 2:
continue
range_start, range_end = tier_range
if tokens <= range_start:
break
tier_start = max(range_start, tokens_processed)
tier_end = min(range_end, tokens)
if tier_end > tier_start:
tokens_in_tier = tier_end - tier_start
cost_per_token = tier.get(cost_key) or tier.get(fallback_cost_key, 0)
total_cost += tokens_in_tier * cost_per_token
tokens_processed = tier_end
return total_cost
def _calculate_flat_cost(tokens: int, cost_per_token: float) -> float:
"""Calculate cost using flat pricing."""
return tokens * cost_per_token
def _calculate_prompt_cost(breakdown: TokenBreakdown, model_info: ModelInfo, tiered_pricing: Optional[List[dict]]) -> float:
"""Calculate total prompt cost including cached tokens."""
if tiered_pricing:
text_cost = _calculate_tiered_cost(
tokens=breakdown.text_tokens,
tiered_pricing=tiered_pricing,
cost_key="input_cost_per_token"
)
cache_cost = _calculate_tiered_cost(
tokens=breakdown.cached_tokens,
tiered_pricing=tiered_pricing,
cost_key="cache_read_input_token_cost"
)
return text_cost + cache_cost
input_cost = model_info.get("input_cost_per_token", 0.0)
cache_cost = model_info.get("cache_read_input_token_cost", input_cost) or input_cost
return (_calculate_flat_cost(tokens=breakdown.text_tokens, cost_per_token=input_cost) +
_calculate_flat_cost(tokens=breakdown.cached_tokens, cost_per_token=cache_cost))
def _calculate_completion_cost(breakdown: TokenBreakdown, model_info: ModelInfo, tiered_pricing: Optional[List[dict]]) -> float:
"""Calculate total completion cost including reasoning tokens."""
if tiered_pricing:
completion_cost = _calculate_tiered_cost(
tokens=breakdown.completion_tokens,
tiered_pricing=tiered_pricing,
cost_key="output_cost_per_token"
)
reasoning_cost = _calculate_tiered_cost(
tokens=breakdown.reasoning_tokens,
tiered_pricing=tiered_pricing,
cost_key="output_cost_per_reasoning_token",
fallback_cost_key="output_cost_per_token"
)
return completion_cost + reasoning_cost
output_cost = model_info.get("output_cost_per_token", 0.0)
reasoning_cost = model_info.get("output_cost_per_reasoning_token", output_cost) or output_cost
return (_calculate_flat_cost(tokens=breakdown.completion_tokens, cost_per_token=output_cost) +
_calculate_flat_cost(tokens=breakdown.reasoning_tokens, cost_per_token=reasoning_cost))
def cost_per_token(model: str, usage: Usage) -> Tuple[float, float]:
"""
Calculates the cost per token for a given model, prompt tokens, and completion tokens.
Follows the same logic as Anthropic's cost per token calculation.
Calculate cost per token for Dashscope models.
Supports both tiered and flat pricing with cached and reasoning tokens.
Args:
model: Model name without provider prefix
usage: LiteLLM Usage block
Returns:
Tuple[float, float] - (prompt_cost_in_usd, completion_cost_in_usd)
"""
return generic_cost_per_token(
model=model, usage=usage, custom_llm_provider="deepseek"
model_info = get_model_info(model=model, custom_llm_provider="dashscope")
breakdown = _extract_token_breakdown(usage)
tiered_pricing = model_info.get("tiered_pricing") if isinstance(model_info.get("tiered_pricing"), list) else None
prompt_cost = _calculate_prompt_cost(
breakdown=breakdown,
model_info=model_info,
tiered_pricing=tiered_pricing
)
completion_cost = _calculate_completion_cost(
breakdown=breakdown,
model_info=model_info,
tiered_pricing=tiered_pricing
)
return prompt_cost, completion_cost

View file

@ -6,6 +6,7 @@
"input_cost_per_token": 0.0,
"output_cost_per_token": 0.0,
"output_cost_per_reasoning_token": 0.0,
"input_cost_per_audio_token": 0.0,
"litellm_provider": "one of https://docs.litellm.ai/docs/providers",
"mode": "one of: chat, embedding, completion, image_generation, audio_transcription, audio_speech, image_generation, moderation, rerank",
"supports_function_calling": true,
@ -19045,34 +19046,43 @@
"max_tokens": 32768,
"max_input_tokens": 30720,
"max_output_tokens": 8192,
"input_cost_per_token": 1.6e-06,
"output_cost_per_token": 6.4e-06,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://bailian.console.alibabacloud.com/?spm=a2c63.p38356.0.0.4a615d7bjSUCb4&tab=doc#/doc/?type=model&url=https%3A%2F%2Fwww.alibabacloud.com%2Fhelp%2Fen%2Fdoc-detail%2F2840914.html"
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-plus-latest": {
"max_tokens": 131072,
"max_input_tokens": 129024,
"max_output_tokens": 16384,
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 32768,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://bailian.console.alibabacloud.com/?spm=a2c63.p38356.0.0.4a615d7bjSUCb4&tab=doc#/doc/?type=model&url=https%3A%2F%2Fwww.alibabacloud.com%2Fhelp%2Fen%2Fdoc-detail%2F2840914.html"
"tiered_pricing": [
{"range": [0, 256e3], "input_cost_per_token": 4e-07, "output_cost_per_token": 1.2e-06, "output_cost_per_reasoning_token": 4e-06},
{"range": [256e3, 1e6], "input_cost_per_token": 1.2e-06, "output_cost_per_token": 3.6e-06, "output_cost_per_reasoning_token": 1.2e-05}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-turbo-latest": {
"max_tokens": 131072,
"max_input_tokens": 129024,
"max_tokens": 1000000,
"max_input_tokens": 1000000,
"max_output_tokens": 16384,
"input_cost_per_token": 5e-08,
"output_cost_per_token": 2e-07,
"output_cost_per_reasoning_token": 5e-07,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://bailian.console.alibabacloud.com/?spm=a2c63.p38356.0.0.4a615d7bjSUCb4&tab=doc#/doc/?type=model&url=https%3A%2F%2Fwww.alibabacloud.com%2Fhelp%2Fen%2Fdoc-detail%2F2840914.html"
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen3-30b-a3b": {
"max_tokens": 131072,
@ -19083,7 +19093,277 @@
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://bailian.console.alibabacloud.com/?spm=a2c63.p38356.0.0.4a615d7bjSUCb4&tab=doc#/doc/?type=model&url=https%3A%2F%2Fwww.alibabacloud.com%2Fhelp%2Fen%2Fdoc-detail%2F2840914.html"
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen3-max-preview": {
"max_tokens": 262144,
"max_input_tokens": 258048,
"max_output_tokens": 65536,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 32e3], "input_cost_per_token": 1.2e-06, "output_cost_per_token": 6e-06},
{"range": [32e3, 128e3], "input_cost_per_token": 2.4e-06, "output_cost_per_token": 1.2e-05},
{"range": [128e3, 252e3], "input_cost_per_token": 3.0e-06, "output_cost_per_token": 1.5e-05}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-plus": {
"max_tokens": 131072,
"max_input_tokens": 129024,
"max_output_tokens": 16384,
"input_cost_per_token": 4e-07,
"output_cost_per_token": 1.2e-06,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-flash": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 32768,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"supports_caching": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 256e3], "input_cost_per_token": 5e-08, "output_cost_per_token": 4e-07},
{"range": [256e3, 1e6], "input_cost_per_token": 2.5e-07, "output_cost_per_token": 2e-06}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-coder": {
"max_tokens": 1000000,
"max_input_tokens": 1000000,
"max_output_tokens": 16384,
"input_cost_per_token": 3e-07,
"output_cost_per_token": 1.5e-06,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen3-coder-plus": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 65536,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"supports_caching": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 32e3], "input_cost_per_token": 1e-06, "output_cost_per_token": 5e-06, "cache_read_input_token_cost": 1e-07},
{"range": [32e3, 128e3], "input_cost_per_token": 1.8e-06, "output_cost_per_token": 9e-06, "cache_read_input_token_cost": 1.8e-07},
{"range": [128e3, 256e3], "input_cost_per_token": 3e-06, "output_cost_per_token": 1.5e-05, "cache_read_input_token_cost": 3e-07},
{"range": [256e3, 1e6], "input_cost_per_token": 6e-06, "output_cost_per_token": 6e-05, "cache_read_input_token_cost": 6e-07}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen3-coder-plus-2025-07-22": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 65536,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 32e3], "input_cost_per_token": 1e-06, "output_cost_per_token": 5e-06},
{"range": [32e3, 128e3], "input_cost_per_token": 1.8e-06, "output_cost_per_token": 9e-06},
{"range": [128e3, 256e3], "input_cost_per_token": 3e-06, "output_cost_per_token": 1.5e-05},
{"range": [256e3, 1e6], "input_cost_per_token": 6e-06, "output_cost_per_token": 6e-05}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen3-coder-flash": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 65536,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"supports_caching": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 32e3], "input_cost_per_token": 3e-07, "output_cost_per_token": 1.5e-06, "cache_read_input_token_cost": 8e-08},
{"range": [32e3, 128e3], "input_cost_per_token": 5e-07, "output_cost_per_token": 2.5e-06, "cache_read_input_token_cost": 1.2e-07},
{"range": [128e3, 256e3], "input_cost_per_token": 8e-07, "output_cost_per_token": 4e-06, "cache_read_input_token_cost": 2e-07},
{"range": [256e3, 1e6], "input_cost_per_token": 1.6e-06, "output_cost_per_token": 9.6e-06, "cache_read_input_token_cost": 4e-07}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen3-coder-flash-2025-07-28": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 65536,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 32e3], "input_cost_per_token": 3e-07, "output_cost_per_token": 1.5e-06},
{"range": [32e3, 128e3], "input_cost_per_token": 5e-07, "output_cost_per_token": 2.5e-06},
{"range": [128e3, 256e3], "input_cost_per_token": 8e-07, "output_cost_per_token": 4e-06},
{"range": [256e3, 1e6], "input_cost_per_token": 1.6e-06, "output_cost_per_token": 9.6e-06}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-plus-2025-09-11": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 32768,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 256e3], "input_cost_per_token": 4e-07, "output_cost_per_token": 1.2e-06, "output_cost_per_reasoning_token": 4e-06},
{"range": [256e3, 1e6], "input_cost_per_token": 1.2e-06, "output_cost_per_token": 3.6e-06, "output_cost_per_reasoning_token": 1.2e-05}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-plus-2025-07-28": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 32768,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 256e3], "input_cost_per_token": 4e-07, "output_cost_per_token": 1.2e-06, "output_cost_per_reasoning_token": 4e-06},
{"range": [256e3, 1e6], "input_cost_per_token": 1.2e-06, "output_cost_per_token": 3.6e-06, "output_cost_per_reasoning_token": 1.2e-05}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-plus-2025-07-14": {
"max_tokens": 131072,
"max_input_tokens": 129024,
"max_output_tokens": 16384,
"input_cost_per_token": 4e-07,
"output_cost_per_token": 1.2e-06,
"output_cost_per_reasoning_token": 4e-06,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-plus-2025-04-28": {
"max_tokens": 131072,
"max_input_tokens": 129024,
"max_output_tokens": 16384,
"input_cost_per_token": 4e-07,
"output_cost_per_token": 1.2e-06,
"output_cost_per_reasoning_token": 4e-06,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-plus-2025-01-25": {
"max_tokens": 131072,
"max_input_tokens": 129024,
"max_output_tokens": 8192,
"input_cost_per_token": 4e-07,
"output_cost_per_token": 1.2e-06,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-flash-2025-07-28": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 32768,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"supports_caching": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 256e3], "input_cost_per_token": 5e-08, "output_cost_per_token": 4e-07},
{"range": [256e3, 1e6], "input_cost_per_token": 2.5e-07, "output_cost_per_token": 2e-06}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-turbo": {
"max_tokens": 131072,
"max_input_tokens": 129024,
"max_output_tokens": 16384,
"input_cost_per_token": 5e-08,
"output_cost_per_token": 2e-07,
"output_cost_per_reasoning_token": 5e-07,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-turbo-2025-04-28": {
"max_tokens": 1000000,
"max_input_tokens": 1000000,
"max_output_tokens": 16384,
"input_cost_per_token": 5e-08,
"output_cost_per_token": 2e-07,
"output_cost_per_reasoning_token": 5e-07,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-turbo-2024-11-01": {
"max_tokens": 1000000,
"max_input_tokens": 1000000,
"max_output_tokens": 8192,
"input_cost_per_token": 5e-08,
"output_cost_per_token": 2e-07,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwq-plus": {
"max_tokens": 131072,
"max_input_tokens": 98304,
"max_output_tokens": 8192,
"max_chain_of_thought_tokens": 32768,
"input_cost_per_token": 8e-07,
"output_cost_per_token": 2.4e-06,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"moonshot/moonshot-v1-8k": {
"max_tokens": 8192,

View file

@ -162,6 +162,7 @@ class ModelInfoBase(ProviderSpecificModelInfo, total=False):
SearchContextCostPerQuery
] # Cost for using web search tool
citation_cost_per_token: Optional[float] # Cost per citation token for Perplexity
tiered_pricing: Optional[List[Dict[str, Any]]] # Tiered pricing structure for models like Dashscope
litellm_provider: Required[str]
mode: Required[
Literal[

View file

@ -4902,6 +4902,7 @@ def _get_model_info_helper( # noqa: PLR0915
citation_cost_per_token=_model_info.get(
"citation_cost_per_token", None
),
tiered_pricing=_model_info.get("tiered_pricing", None),
litellm_provider=_model_info.get(
"litellm_provider", custom_llm_provider
),

View file

@ -6,6 +6,7 @@
"input_cost_per_token": 0.0,
"output_cost_per_token": 0.0,
"output_cost_per_reasoning_token": 0.0,
"input_cost_per_audio_token": 0.0,
"litellm_provider": "one of https://docs.litellm.ai/docs/providers",
"mode": "one of: chat, embedding, completion, image_generation, audio_transcription, audio_speech, image_generation, moderation, rerank",
"supports_function_calling": true,
@ -19045,34 +19046,43 @@
"max_tokens": 32768,
"max_input_tokens": 30720,
"max_output_tokens": 8192,
"input_cost_per_token": 1.6e-06,
"output_cost_per_token": 6.4e-06,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://bailian.console.alibabacloud.com/?spm=a2c63.p38356.0.0.4a615d7bjSUCb4&tab=doc#/doc/?type=model&url=https%3A%2F%2Fwww.alibabacloud.com%2Fhelp%2Fen%2Fdoc-detail%2F2840914.html"
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-plus-latest": {
"max_tokens": 131072,
"max_input_tokens": 129024,
"max_output_tokens": 16384,
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 32768,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://bailian.console.alibabacloud.com/?spm=a2c63.p38356.0.0.4a615d7bjSUCb4&tab=doc#/doc/?type=model&url=https%3A%2F%2Fwww.alibabacloud.com%2Fhelp%2Fen%2Fdoc-detail%2F2840914.html"
"tiered_pricing": [
{"range": [0, 256e3], "input_cost_per_token": 4e-07, "output_cost_per_token": 1.2e-06, "output_cost_per_reasoning_token": 4e-06},
{"range": [256e3, 1e6], "input_cost_per_token": 1.2e-06, "output_cost_per_token": 3.6e-06, "output_cost_per_reasoning_token": 1.2e-05}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-turbo-latest": {
"max_tokens": 131072,
"max_input_tokens": 129024,
"max_tokens": 1000000,
"max_input_tokens": 1000000,
"max_output_tokens": 16384,
"input_cost_per_token": 5e-08,
"output_cost_per_token": 2e-07,
"output_cost_per_reasoning_token": 5e-07,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://bailian.console.alibabacloud.com/?spm=a2c63.p38356.0.0.4a615d7bjSUCb4&tab=doc#/doc/?type=model&url=https%3A%2F%2Fwww.alibabacloud.com%2Fhelp%2Fen%2Fdoc-detail%2F2840914.html"
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen3-30b-a3b": {
"max_tokens": 131072,
@ -19083,7 +19093,277 @@
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://bailian.console.alibabacloud.com/?spm=a2c63.p38356.0.0.4a615d7bjSUCb4&tab=doc#/doc/?type=model&url=https%3A%2F%2Fwww.alibabacloud.com%2Fhelp%2Fen%2Fdoc-detail%2F2840914.html"
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen3-max-preview": {
"max_tokens": 262144,
"max_input_tokens": 258048,
"max_output_tokens": 65536,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 32e3], "input_cost_per_token": 1.2e-06, "output_cost_per_token": 6e-06},
{"range": [32e3, 128e3], "input_cost_per_token": 2.4e-06, "output_cost_per_token": 1.2e-05},
{"range": [128e3, 252e3], "input_cost_per_token": 3.0e-06, "output_cost_per_token": 1.5e-05}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-plus": {
"max_tokens": 131072,
"max_input_tokens": 129024,
"max_output_tokens": 16384,
"input_cost_per_token": 4e-07,
"output_cost_per_token": 1.2e-06,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-flash": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 32768,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"supports_caching": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 256e3], "input_cost_per_token": 5e-08, "output_cost_per_token": 4e-07},
{"range": [256e3, 1e6], "input_cost_per_token": 2.5e-07, "output_cost_per_token": 2e-06}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-coder": {
"max_tokens": 1000000,
"max_input_tokens": 1000000,
"max_output_tokens": 16384,
"input_cost_per_token": 3e-07,
"output_cost_per_token": 1.5e-06,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen3-coder-plus": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 65536,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"supports_caching": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 32e3], "input_cost_per_token": 1e-06, "output_cost_per_token": 5e-06, "cache_read_input_token_cost": 1e-07},
{"range": [32e3, 128e3], "input_cost_per_token": 1.8e-06, "output_cost_per_token": 9e-06, "cache_read_input_token_cost": 1.8e-07},
{"range": [128e3, 256e3], "input_cost_per_token": 3e-06, "output_cost_per_token": 1.5e-05, "cache_read_input_token_cost": 3e-07},
{"range": [256e3, 1e6], "input_cost_per_token": 6e-06, "output_cost_per_token": 6e-05, "cache_read_input_token_cost": 6e-07}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen3-coder-plus-2025-07-22": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 65536,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 32e3], "input_cost_per_token": 1e-06, "output_cost_per_token": 5e-06},
{"range": [32e3, 128e3], "input_cost_per_token": 1.8e-06, "output_cost_per_token": 9e-06},
{"range": [128e3, 256e3], "input_cost_per_token": 3e-06, "output_cost_per_token": 1.5e-05},
{"range": [256e3, 1e6], "input_cost_per_token": 6e-06, "output_cost_per_token": 6e-05}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen3-coder-flash": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 65536,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"supports_caching": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 32e3], "input_cost_per_token": 3e-07, "output_cost_per_token": 1.5e-06, "cache_read_input_token_cost": 8e-08},
{"range": [32e3, 128e3], "input_cost_per_token": 5e-07, "output_cost_per_token": 2.5e-06, "cache_read_input_token_cost": 1.2e-07},
{"range": [128e3, 256e3], "input_cost_per_token": 8e-07, "output_cost_per_token": 4e-06, "cache_read_input_token_cost": 2e-07},
{"range": [256e3, 1e6], "input_cost_per_token": 1.6e-06, "output_cost_per_token": 9.6e-06, "cache_read_input_token_cost": 4e-07}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen3-coder-flash-2025-07-28": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 65536,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 32e3], "input_cost_per_token": 3e-07, "output_cost_per_token": 1.5e-06},
{"range": [32e3, 128e3], "input_cost_per_token": 5e-07, "output_cost_per_token": 2.5e-06},
{"range": [128e3, 256e3], "input_cost_per_token": 8e-07, "output_cost_per_token": 4e-06},
{"range": [256e3, 1e6], "input_cost_per_token": 1.6e-06, "output_cost_per_token": 9.6e-06}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-plus-2025-09-11": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 32768,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 256e3], "input_cost_per_token": 4e-07, "output_cost_per_token": 1.2e-06, "output_cost_per_reasoning_token": 4e-06},
{"range": [256e3, 1e6], "input_cost_per_token": 1.2e-06, "output_cost_per_token": 3.6e-06, "output_cost_per_reasoning_token": 1.2e-05}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-plus-2025-07-28": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 32768,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 256e3], "input_cost_per_token": 4e-07, "output_cost_per_token": 1.2e-06, "output_cost_per_reasoning_token": 4e-06},
{"range": [256e3, 1e6], "input_cost_per_token": 1.2e-06, "output_cost_per_token": 3.6e-06, "output_cost_per_reasoning_token": 1.2e-05}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-plus-2025-07-14": {
"max_tokens": 131072,
"max_input_tokens": 129024,
"max_output_tokens": 16384,
"input_cost_per_token": 4e-07,
"output_cost_per_token": 1.2e-06,
"output_cost_per_reasoning_token": 4e-06,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-plus-2025-04-28": {
"max_tokens": 131072,
"max_input_tokens": 129024,
"max_output_tokens": 16384,
"input_cost_per_token": 4e-07,
"output_cost_per_token": 1.2e-06,
"output_cost_per_reasoning_token": 4e-06,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-plus-2025-01-25": {
"max_tokens": 131072,
"max_input_tokens": 129024,
"max_output_tokens": 8192,
"input_cost_per_token": 4e-07,
"output_cost_per_token": 1.2e-06,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-flash-2025-07-28": {
"max_tokens": 1000000,
"max_input_tokens": 997952,
"max_output_tokens": 32768,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"supports_caching": true,
"mode": "chat",
"tiered_pricing": [
{"range": [0, 256e3], "input_cost_per_token": 5e-08, "output_cost_per_token": 4e-07},
{"range": [256e3, 1e6], "input_cost_per_token": 2.5e-07, "output_cost_per_token": 2e-06}
],
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-turbo": {
"max_tokens": 131072,
"max_input_tokens": 129024,
"max_output_tokens": 16384,
"input_cost_per_token": 5e-08,
"output_cost_per_token": 2e-07,
"output_cost_per_reasoning_token": 5e-07,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-turbo-2025-04-28": {
"max_tokens": 1000000,
"max_input_tokens": 1000000,
"max_output_tokens": 16384,
"input_cost_per_token": 5e-08,
"output_cost_per_token": 2e-07,
"output_cost_per_reasoning_token": 5e-07,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwen-turbo-2024-11-01": {
"max_tokens": 1000000,
"max_input_tokens": 1000000,
"max_output_tokens": 8192,
"input_cost_per_token": 5e-08,
"output_cost_per_token": 2e-07,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"dashscope/qwq-plus": {
"max_tokens": 131072,
"max_input_tokens": 98304,
"max_output_tokens": 8192,
"max_chain_of_thought_tokens": 32768,
"input_cost_per_token": 8e-07,
"output_cost_per_token": 2.4e-06,
"litellm_provider": "dashscope",
"supports_function_calling": true,
"supports_tool_choice": true,
"supports_reasoning": true,
"mode": "chat",
"source": "https://www.alibabacloud.com/help/en/model-studio/models"
},
"moonshot/moonshot-v1-8k": {
"max_tokens": 8192,

View file

@ -0,0 +1,159 @@
"""
Test suite for Dashscope cost calculation functionality.
Tests the cost calculation for Dashscope models including:
- Tiered pricing based on input token ranges
- Caching discounts
- Reasoning tokens
- Standard flat pricing fallback
"""
import json
import math
import os
import sys
import pytest
# Add the project root to Python path
sys.path.insert(0, os.path.abspath("../../../.."))
import litellm
from litellm.llms.dashscope.cost_calculator import (
cost_per_token as dashscope_cost_per_token,
)
from litellm.types.utils import (
CompletionTokensDetailsWrapper,
PromptTokensDetailsWrapper,
Usage,
)
class TestDashscopeCostCalculator:
"""Test suite for Dashscope cost calculation functionality."""
@pytest.fixture(autouse=True)
def setup_model_cost_map(self):
"""Set up the model cost map for testing."""
# Ensure we use local model cost map for consistent testing
os.environ["LITELLM_LOCAL_MODEL_COST_MAP"] = "True"
# Find the project root directory and load model cost map
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = current_dir
while not os.path.exists(os.path.join(project_root, "model_prices_and_context_window.json")):
parent = os.path.dirname(project_root)
if parent == project_root: # Reached filesystem root
break
project_root = parent
model_cost_path = os.path.join(project_root, "model_prices_and_context_window.json")
with open(model_cost_path, "r") as f:
model_cost_map = json.load(f)
litellm.model_cost = model_cost_map
def test_flat_pricing_basic_cost_calculation(self):
"""Test basic cost calculation for flat pricing models (qwen-max)."""
usage = Usage(
prompt_tokens=1000,
completion_tokens=500,
total_tokens=1500
)
prompt_cost, completion_cost = dashscope_cost_per_token(
model="qwen-max",
usage=usage
)
# Expected costs for qwen-max:
# Input: 1000 tokens * $1.6e-6 = $0.0016
# Output: 500 tokens * $6.4e-6 = $0.0032
expected_prompt_cost = 1000 * 1.6e-6
expected_completion_cost = 500 * 6.4e-6
assert math.isclose(prompt_cost, expected_prompt_cost, rel_tol=1e-10)
assert math.isclose(completion_cost, expected_completion_cost, rel_tol=1e-10)
def test_tiered_pricing_single_tier(self):
"""Test tiered pricing when all tokens fall within first tier."""
usage = Usage(
prompt_tokens=20000, # Within first tier (0-32K)
completion_tokens=1000,
total_tokens=21000
)
prompt_cost, completion_cost = dashscope_cost_per_token(
model="qwen3-coder-plus",
usage=usage
)
# Expected costs for qwen3-coder-plus (tier 1):
# Input: 20,000 tokens * $1e-6 = $0.02
# Output: 1,000 tokens * $5e-6 = $0.005
expected_prompt_cost = 20000 * 1e-6
expected_completion_cost = 1000 * 5e-6
assert math.isclose(prompt_cost, expected_prompt_cost, rel_tol=1e-10)
assert math.isclose(completion_cost, expected_completion_cost, rel_tol=1e-10)
def test_tiered_pricing_multiple_tiers(self):
"""Test tiered pricing when tokens span multiple tiers."""
usage = Usage(
prompt_tokens=150000, # Spans tiers 1 (0-32K), 2 (32K-128K), 3 (128K-256K)
completion_tokens=2000,
total_tokens=152000
)
prompt_cost, completion_cost = dashscope_cost_per_token(
model="qwen3-coder-plus",
usage=usage
)
# Expected input cost calculation:
# Tier 1 (0-32K): 32,000 tokens * $1e-6 = $0.032
# Tier 2 (32K-128K): 96,000 tokens * $1.8e-6 = $0.1728
# Tier 3 (128K-256K): 22,000 tokens * $3e-6 = $0.066
# Total input cost = $0.032 + $0.1728 + $0.066 = $0.2708
expected_prompt_cost = (32000 * 1e-6) + (96000 * 1.8e-6) + (22000 * 3e-6)
expected_completion_cost = 2000 * 5e-6 # All in tier 1 for output
assert math.isclose(prompt_cost, expected_prompt_cost, rel_tol=1e-10)
assert math.isclose(completion_cost, expected_completion_cost, rel_tol=1e-10)
def test_tiered_pricing_with_caching(self):
"""Test tiered pricing with cached tokens."""
prompt_tokens_details = PromptTokensDetailsWrapper(
cached_tokens=10000 # 10K cached tokens
)
usage = Usage(
prompt_tokens=50000, # 40K regular + 10K cached = 50K total
completion_tokens=1000,
total_tokens=51000,
prompt_tokens_details=prompt_tokens_details
)
prompt_cost, completion_cost = dashscope_cost_per_token(
model="qwen3-coder-plus",
usage=usage
)
# Expected cost calculation:
# Regular tokens: 40,000 (32K in tier 1 + 8K in tier 2)
# - Tier 1: 32,000 * $1e-6 = $0.032
# - Tier 2: 8,000 * $1.8e-6 = $0.0144
# Cached tokens: 10,000 in tier 1 at discounted rate
# - Tier 1 cached: 10,000 * $1e-7 = $0.001
# Total input cost = $0.032 + $0.0144 + $0.001 = $0.0474
regular_tokens = 40000
cached_tokens = 10000
expected_regular_cost = (32000 * 1e-6) + (8000 * 1.8e-6)
expected_cached_cost = cached_tokens * 1e-7 # Tier 1 cached rate
expected_prompt_cost = expected_regular_cost + expected_cached_cost
expected_completion_cost = 1000 * 5e-6
assert math.isclose(prompt_cost, expected_prompt_cost, rel_tol=1e-10)
assert math.isclose(completion_cost, expected_completion_cost, rel_tol=1e-10)

View file

@ -11,8 +11,9 @@ export enum Providers {
Azure = "Azure",
Azure_AI_Studio = "Azure AI Foundry (Studio)",
Cerebras = "Cerebras",
Cohere = "Cohere",
Databricks = "Databricks",
Cohere = "Cohere",
Dashscope = "Dashscope",
Databricks = "Databricks (Qwen API)",
DeepInfra = "DeepInfra",
Deepgram = "Deepgram",
Deepseek = "Deepseek",
@ -37,7 +38,7 @@ export enum Providers {
Vertex_AI = "Vertex AI (Anthropic, Gemini, etc.)",
VolcEngine = "VolcEngine",
Voyage = "Voyage AI",
xAI = "xAI",
xAI = "xAI",
}
export const provider_map: Record<string, string> = {
@ -56,6 +57,7 @@ export const provider_map: Record<string, string> = {
OpenAI_Text_Compatible: "text-completion-openai",
Vertex_AI: "vertex_ai",
Databricks: "databricks",
Dashscope: "dashscope",
xAI: "xai",
Deepseek: "deepseek",
Ollama: "ollama",
@ -91,6 +93,7 @@ export const providerLogoMap: Record<string, string> = {
[Providers.Cerebras]: `${asset_logos_folder}cerebras.svg`,
[Providers.Cohere]: `${asset_logos_folder}cohere.svg`,
[Providers.Databricks]: `${asset_logos_folder}databricks.svg`,
[Providers.Dashscope]: `${asset_logos_folder}dashscope.svg`,
[Providers.Deepseek]: `${asset_logos_folder}deepseek.svg`,
[Providers.FireworksAI]: `${asset_logos_folder}fireworks.svg`,
[Providers.Groq]: `${asset_logos_folder}groq.svg`,