-
Notifications
You must be signed in to change notification settings - Fork 51
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
feat: track cost and token usage in log file #80
Changes from 1 commit
2333b26
6ee43be
a7c7ff1
fbd0d8a
d07a45f
0cf326f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import logging | ||
from pathlib import Path | ||
|
||
_LOGGER_NAME = "goose" | ||
_LOGGER_FILE_NAME = "goose.log" | ||
|
||
|
||
def setup_logging(log_file_directory: Path, level: int = logging.INFO) -> None: | ||
logger = logging.getLogger(_LOGGER_NAME) | ||
logger.setLevel(level) | ||
log_file_directory.mkdir(parents=True, exist_ok=True) | ||
file_handler = logging.FileHandler(log_file_directory / _LOGGER_FILE_NAME) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do you think about bucketing the logs by time (e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, definitely we can use the TimedRotatingFileHandler when the log gets verbose. At the moment we only log the cost, but I think it will be helpful to log errors. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense! if we wanna go long-term we could also go one step further extending There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, like tags |
||
logger.addHandler(file_handler) | ||
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") | ||
file_handler.setFormatter(formatter) | ||
return logger | ||
|
||
|
||
def get_logger() -> logging.Logger: | ||
return logging.getLogger(_LOGGER_NAME) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import List, Optional | ||
from exchange.token_usage_collector import TokenUsage | ||
|
||
PRICES = { | ||
"gpt-4o": (5.00, 15.00), | ||
"gpt-4o-2024-08-06": (2.50, 10.00), | ||
"gpt-4o-mini": (0.150, 0.600), | ||
"gpt-4o-mini-2024-07-18": (0.150, 0.600), | ||
"o1-preview": (15.00, 60.00), | ||
"o1-mini": (3.00, 12.00), | ||
"claude-3-5-sonnet-20240620": (3.00, 15.00), | ||
"anthropic.claude-3-5-sonnet-20240620-v1:0": (3.00, 15.00), | ||
"claude-3-opus-20240229": (15.00, 75.00), | ||
"anthropic.claude-3-opus-20240229-v1:0": (15.00, 75.00), | ||
"claude-3-haiku-20240307": (0.25, 1.25), | ||
"anthropic.claude-3-haiku-20240307-v1:0": (0.25, 1.25), | ||
} | ||
|
||
|
||
def _calculate_cost(token_usage: TokenUsage) -> Optional[float]: | ||
model_name = token_usage.model.lower() | ||
if model_name in PRICES: | ||
input_token_price, output_token_price = PRICES[model_name] | ||
return (input_token_price * token_usage.input_tokens + output_token_price * token_usage.output_tokens) / 1000000 | ||
return None | ||
|
||
|
||
def get_total_cost_message(token_usage: List[TokenUsage]) -> str: | ||
total_cost = 0 | ||
message = "" | ||
for usage in token_usage: | ||
cost = _calculate_cost(usage) | ||
if cost is not None: | ||
message += f"Cost for {str(usage)}: ${cost:.2f}\n" | ||
total_cost += cost | ||
else: | ||
message += f"Cost for {str(usage)}: Not available\n" | ||
message += f"Total cost: ${total_cost:.2f}" | ||
return message |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from goose.utils._cost_calculator import _calculate_cost, get_total_cost_message | ||
from exchange.token_usage_collector import TokenUsage | ||
|
||
|
||
def test_calculate_cost(): | ||
cost = _calculate_cost(TokenUsage(model="gpt-4o", input_tokens=10000, output_tokens=600)) | ||
assert cost == 0.059 | ||
|
||
|
||
def test_get_total_cost_message(): | ||
message = get_total_cost_message( | ||
[ | ||
TokenUsage(model="gpt-4o", input_tokens=10000, output_tokens=600), | ||
TokenUsage(model="gpt-4o-mini", input_tokens=3000000, output_tokens=4000000), | ||
] | ||
) | ||
expected_message = ( | ||
"Cost for TokenUsage(model='gpt-4o', input_tokens=10000, output_tokens=600): $0.06\n" | ||
+ "Cost for TokenUsage(model='gpt-4o-mini', input_tokens=3000000, output_tokens=4000000)" | ||
+ ": $2.85\nTotal cost: $2.91" | ||
) | ||
assert message == expected_message |
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.
if we want to control this via the cli , i've been playing with
click
and we could maybe add@click.option("--log-level", ...)
so it's easier to dial up the messages up/down without rebuildinggoose