Skip to content

Commit

Permalink
chore: add clear_cache helper
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Jan 3, 2025
1 parent f68f705 commit 31f4a96
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ asyncio.run(main())
You can customize the cache timeout:

```python
from tokonomics import get_model_costs
from tokonomics import get_model_costs, clear_cache

# Get model costs with custom cache duration (e.g., 1 hour)
costs = await get_model_costs("gpt-4", cache_timeout=3600)
if costs:
print(f"Input cost per token: ${costs['input_cost_per_token']}")
print(f"Output cost per token: ${costs['output_cost_per_token']}")

clear_cache()
```

### Getting Model Token Limits
Expand Down
9 changes: 8 additions & 1 deletion src/tokonomics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
__version__ = "0.2.2"

from tokonomics.core import (
get_model_costs,
calculate_token_cost,
get_model_limits,
reset_cache,
)
from tokonomics.toko_types import ModelCosts, TokenCosts, TokenLimits
from tokonomics.core import get_model_costs, calculate_token_cost, get_model_limits
from tokonomics.pydanticai_cost import calculate_pydantic_cost, Usage


__all__ = [
"ModelCosts",
"TokenCosts",
Expand All @@ -13,4 +19,5 @@
"calculate_token_cost",
"get_model_costs",
"get_model_limits",
"reset_cache",
]
5 changes: 5 additions & 0 deletions src/tokonomics/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
LITELLM_PRICES_URL = "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json"


def reset_cache() -> None:
"""Clear all cached pricing and limit data."""
_cost_cache.clear()


def _is_numeric(value: str | int | float) -> bool: # noqa: PYI041
"""Check if a value can be converted to a number."""
if isinstance(value, int | float):
Expand Down

0 comments on commit 31f4a96

Please sign in to comment.