-
-
Notifications
You must be signed in to change notification settings - Fork 11k
fix(volcengine): support tiered Doubao pricing #34902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kunwl123456
wants to merge
2
commits into
BerriAI:litellm_internal_staging
Choose a base branch
from
kunwl123456:fix/volcengine-doubao-tiered-pricing
base: litellm_internal_staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| """ | ||
| Cost calculator for Volcengine chat models. | ||
|
|
||
| Volcengine selects one pricing tier from the request's total input length and | ||
| charges every input/output token at that tier. This is not graduated pricing. | ||
| """ | ||
|
|
||
| from litellm.litellm_core_utils.llm_cost_calc.tiered_pricing import ( | ||
| select_tier_for_input, | ||
| tier_rate, | ||
| ) | ||
| from litellm.types.utils import ModelInfo, Usage | ||
| from litellm.utils import get_model_info | ||
|
|
||
|
|
||
| def _cached_prompt_tokens(usage: Usage) -> int: | ||
| prompt_details = usage.prompt_tokens_details | ||
| if prompt_details is None: | ||
| return 0 | ||
| return int(getattr(prompt_details, "cached_tokens", 0) or 0) | ||
|
|
||
|
|
||
| def _select_pricing_tier( | ||
| tiered_pricing: list[dict] | None, # mutable-ok: shared tier helper accepts JSON-backed lists of dictionaries | ||
| prompt_tokens: int, | ||
| ) -> dict | None: # mutable-ok: returns the JSON-backed tier selected by the shared helper | ||
| if not tiered_pricing: | ||
| return None | ||
|
|
||
| # Output-only synthetic usage blocks have no input length. Use the first | ||
| # tier instead of returning zero cost for their completion tokens. | ||
| return select_tier_for_input( | ||
| tiered_pricing=tiered_pricing, | ||
| input_tokens=max(prompt_tokens, 1), | ||
| ) | ||
|
|
||
|
|
||
| def _output_rate( | ||
| tier: dict, # mutable-ok: tier_rate accepts a JSON-backed dictionary | ||
| completion_tokens: int, | ||
| ) -> float: | ||
| """Read the output rate, including Seed 1.8's short-output discount.""" | ||
| if completion_tokens > 200 and tier.get("output_cost_per_token_above_200_tokens") is not None: | ||
| return tier_rate(tier, "output_cost_per_token_above_200_tokens") | ||
| return tier_rate(tier, "output_cost_per_token") | ||
|
|
||
|
|
||
| def cost_per_token(model: str, usage: Usage) -> tuple[float, float]: | ||
| """ | ||
| Return ``(prompt_cost_usd, completion_cost_usd)`` for a Volcengine request. | ||
|
|
||
| Tier selection is based on total prompt tokens. Cached prompt tokens still | ||
| count toward the tier boundary, but use the cache-read rate when one is | ||
| declared by the model. | ||
| """ | ||
| model_info: ModelInfo = get_model_info( | ||
| model=model, | ||
| custom_llm_provider="volcengine", | ||
| ) | ||
|
|
||
| prompt_tokens = int(usage.prompt_tokens or 0) | ||
| completion_tokens = int(usage.completion_tokens or 0) | ||
| cached_tokens = min(_cached_prompt_tokens(usage), prompt_tokens) | ||
| uncached_tokens = prompt_tokens - cached_tokens | ||
|
|
||
| raw_tiered_pricing = model_info.get("tiered_pricing") | ||
| tiered_pricing = raw_tiered_pricing if isinstance(raw_tiered_pricing, list) else None | ||
| tier = _select_pricing_tier( | ||
| tiered_pricing=tiered_pricing, | ||
| prompt_tokens=prompt_tokens, | ||
| ) | ||
|
|
||
| if tier is not None: | ||
| input_rate = tier_rate(tier, "input_cost_per_token") | ||
| output_rate = _output_rate(tier=tier, completion_tokens=completion_tokens) | ||
| cache_rate_value = model_info.get("cache_read_input_token_cost") | ||
| cache_rate = float(cache_rate_value) if cache_rate_value is not None else input_rate | ||
| else: | ||
| input_rate = float(model_info.get("input_cost_per_token") or 0.0) | ||
| output_rate = float(model_info.get("output_cost_per_token") or 0.0) | ||
| cache_rate_value = model_info.get("cache_read_input_token_cost") | ||
| cache_rate = float(cache_rate_value) if cache_rate_value is not None else input_rate | ||
|
|
||
| prompt_cost = (uncached_tokens * input_rate) + (cached_tokens * cache_rate) | ||
| completion_cost = completion_tokens * output_rate | ||
| return prompt_cost, completion_cost |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Low: Higher output rate bypasses the pre-call budget bound
An authenticated user can request more than 200 output tokens while near their budget limit because
_max_cost_for_cost_info()only considersoutput_cost_per_tokenandoutput_cost_per_reasoning_token. For this tier it reserves at2.8e-07, while post-call accounting charges the entire output at1.1e-06; concurrent requests can therefore incur substantially more provider spend than the atomic budget gate permits. Update the budget reservation calculation to include this conditional rate when estimating maximum output cost.