LLM API call token usage-based expense tracker
Project description
TrackMyLLM(cost)
Usage
pip install llm-cost-tracker
1. Example usage for class method:
from tracker.cost_tracker import cost_tracker
from openai import OpenAI, AsyncOpenAI
class Agent:
def __init__(self, model_name, api_key = None):
self.model_name = model_name # nessary
self.costs: dict[str, list[float]] = {} # nessary
if api_key:
self._initialize_client(api_key)
def _initialize_client(self, api_key):
self.client = OpenAI(api_key=api_key)
self.aclient = AsyncOpenAI(api_key=api_key)
# just for compatibility
def total_cost(self):
return float(round(sum(sum(lst) for lst in self.costs.values()), 6))
@cost_tracker.track_cost()
def ask(self, prompt: str):
resp = self.client.chat.completions.create(
model=self.model_name,
messages=[{"role":"user","content":prompt}],
)
return resp, {"model_response": resp.choices[0].message.content}
@cost_tracker.track_cost()
async def aask(self, prompt: str):
resp = await self.aclient.chat.completions.create(
model=self.model_name,
messages=[{"role":"user","content":prompt}],
)
return resp, {"model_response": resp.choices[0].message.content}
test_client = Agent(model_name="gpt-4o-mini")
a, b = test_client.ask("Hello, world!")
print("Individual costs: ", test_client.costs)
print("Total cost: ", format(test_client.total_cost(), "f"))
# Individual costs: defaultdict(<class 'list'>, {'gpt-4o-mini': [8.400000000000001e-06]})
# Total cost: 0.000008
a, b = await test_client.aask("Hello, world!")
print("Individual costs: ", test_client.costs)
print("Total cost: ", format(test_client.total_cost(), "f"))
# Individual costs: {'gpt-4o-mini': [7.65e-06, 7.65e-06]}
# Total cost: 0.000015
2. Example usage for single function:
from openai import OpenAI
from tracker.cost_tracker import cost_tracker
model_name = "gpt-4o-mini"
client = OpenAI()
@cost_tracker.track_cost()
def generate(model_name, prompt):
completion = client.chat.completions.create(
model = model_name,
max_tokens=8192,
messages=[
{
"role":"system",
"content":"Your a Great AI"
},
{
"role":"user",
"content":prompt
}
],
)
return completion
response = generate(model_name, "Hello, world!")
print("Individual costs: ", cost_tracker.costs)
print("Total cost: ", format(cost_tracker.total_cost(), "f"))
# Individual costs: defaultdict(<class 'list'>, {'gpt-4o-mini': [8.400000000000001e-06]})
# Total cost: 0.000008
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
llm_cost_tracker-0.1.9.tar.gz
(10.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_cost_tracker-0.1.9.tar.gz.
File metadata
- Download URL: llm_cost_tracker-0.1.9.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f92bebcd7b7494e0f8c9facac37d5732272961f319317d325f7c733a8a6d1972
|
|
| MD5 |
909b8797720e5c901f06ec350664bfd4
|
|
| BLAKE2b-256 |
958a3bd735efc79da4bf0a394d9911d6cdb6b4ec1b3f7309f840af85e4d20e7f
|
File details
Details for the file llm_cost_tracker-0.1.9-py3-none-any.whl.
File metadata
- Download URL: llm_cost_tracker-0.1.9-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dfe30a99f9ae43de5793744bb3ca89d2d4140502e1b856b882c951a93cd6d008
|
|
| MD5 |
87b83b4bbfe08e6f893f7b53cbf4985e
|
|
| BLAKE2b-256 |
b406039f0a5efe9a8c98be0b1c1686f88ee773f62151979524bef069a6b04424
|