Repository navigation
(feat) add cost tracking for OpenAI prompt caching #6055
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2c18066
add cache_read_input_token_cost for prompt caching models
ishaan-jaff d3c9284
add prompt caching for latest models
ishaan-jaff 44997c3
add openai cost calculator
ishaan-jaff 85ee9be
add openai prompt caching test
ishaan-jaff 032bf75
fix lint check
ishaan-jaff 18255c1
add not on how usage._cache_read_input_tokens is used
ishaan-jaff d2b0b05
fix cost calc whisper openai
ishaan-jaff b8a737a
use output_cost_per_second
ishaan-jaff 5ebc9af
add input_cost_per_second
ishaan-jaff 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,57 @@ | ||
| """ | ||
| Helper util for handling openai-specific cost calculation | ||
| - e.g.: prompt caching | ||
| """ | ||
|
|
||
| from typing import Optional, Tuple | ||
|
|
||
| from litellm._logging import verbose_logger | ||
| from litellm.types.utils import Usage | ||
| from litellm.utils import get_model_info | ||
|
|
||
|
|
||
| def cost_per_token( | ||
| model: str, usage: Usage, response_time_ms: Optional[float] = 0.0 | ||
| ) -> Tuple[float, float]: | ||
| """ | ||
| Calculates the cost per token for a given model, prompt tokens, and completion tokens. | ||
|
|
||
| Input: | ||
| - model: str, the model name without provider prefix | ||
| - usage: LiteLLM Usage block, containing anthropic caching information | ||
|
|
||
| Returns: | ||
| Tuple[float, float] - prompt_cost_in_usd, completion_cost_in_usd | ||
| """ | ||
| ## GET MODEL INFO | ||
| model_info = get_model_info(model=model, custom_llm_provider="openai") | ||
|
|
||
| ## CALCULATE INPUT COST | ||
| prompt_cost: float = usage["prompt_tokens"] * model_info["input_cost_per_token"] | ||
|
|
||
| ## CALCULATE OUTPUT COST | ||
| completion_cost: float = ( | ||
| usage["completion_tokens"] * model_info["output_cost_per_token"] | ||
| ) | ||
|
|
||
| ## Prompt Caching cost calculation | ||
| if model_info.get("cache_read_input_token_cost") is not None: | ||
| # Note: We read ._cache_read_input_tokens from the Usage - since cost_calculator.py standardizes the cache read tokens on usage._cache_read_input_tokens | ||
| prompt_cost += usage._cache_read_input_tokens * ( | ||
| model_info.get("cache_read_input_token_cost", 0) or 0 | ||
| ) | ||
|
|
||
| ## Speech / Audio cost calculation | ||
| if ( | ||
| "output_cost_per_second" in model_info | ||
| and model_info["output_cost_per_second"] is not None | ||
| and response_time_ms is not None | ||
| ): | ||
| verbose_logger.debug( | ||
| f"For model={model} - output_cost_per_second: {model_info.get('output_cost_per_second')}; response time: {response_time_ms}" | ||
| ) | ||
| ## COST PER SECOND ## | ||
| prompt_cost = 0 | ||
| completion_cost = model_info["output_cost_per_second"] * response_time_ms / 1000 | ||
|
|
||
| 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
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.
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.
Check failure
Code scanning / CodeQL
Clear-text logging of sensitive information
Copilot Autofix
AI about 2 years ago
To fix the problem, we should avoid logging sensitive information directly. Instead, we can log non-sensitive metadata or anonymized information. In this case, we can remove the
modelfrom the log statement or replace it with a non-sensitive identifier.modelvariable in the log statement on line 51 oflitellm/llms/OpenAI/cost_calculation.py.