Skip to content
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

Merged
merged 6 commits into from
Sep 23, 2024

Conversation

lifeizhou-ap
Copy link
Collaborator

@lifeizhou-ap lifeizhou-ap commented Sep 19, 2024

Why
Users can view the cost and token usage in the log file

What

  • Created file logger with default "INFO" level.
  • Added option to specify the log level when start the session. eg: uv run goose session start --log-level DEBUG (Thank @lamchau for your suggestion)
  • Log the cost and token usage after the user saves the session. The usage is rendered from exchange [PR]
    (feat: collect total token usage square/exchange#32)
Screenshot 2024-09-20 at 10 44 09 AM

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)
Copy link
Collaborator

@lamchau lamchau Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about bucketing the logs by time (e.g. goose.yyyy-mm-dd.log or something)? not sure how verbose the logging could be, but we sorta also get rolling logs for free too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 logging.Filter to some intelligent routing [or segment] (e.g. api-costs.log vs goose.log)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, like tags


def setup_logging(log_file_directory: Path, level: int = logging.INFO) -> None:
logger = logging.getLogger(_LOGGER_NAME)
logger.setLevel(level)
Copy link
Collaborator

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 rebuilding goose

@lifeizhou-ap lifeizhou-ap changed the title track cost in log file feat: track cost in log file Sep 20, 2024
@lifeizhou-ap lifeizhou-ap changed the title feat: track cost in log file feat: track cost and token usage in log file Sep 20, 2024
@michaelneale
Copy link
Collaborator

I like this - and has been asked for a bit

@codefromthecrypt
Copy link
Collaborator

fyi since this is a common feature in Q/A or eval tools (langtrace, openlit, traceloop etc) you can have a look to validate approach vs what they are doing. Not suggesting change, just for context:

e.g. from openlit
https://github.com/openlit/openlit/blob/b4d36f80fe1b2c6ea5faaddc2882f75db0a613af/sdk/python/src/openlit/__helpers.py
https://github.com/openlit/openlit/blob/b4d36f80fe1b2c6ea5faaddc2882f75db0a613af/assets/pricing.json

@lifeizhou-ap lifeizhou-ap marked this pull request as ready for review September 20, 2024 03:39
Copy link
Contributor

@lukealvoeiro lukealvoeiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@lifeizhou-ap lifeizhou-ap merged commit 9dbc0d9 into main Sep 23, 2024
2 checks passed
Kvadratni added a commit to Kvadratni/goose that referenced this pull request Sep 23, 2024
* origin/main:
  feat: track cost and token usage in log file (block#80)
  chore: add link to docs in read me (block#85)
@lifeizhou-ap lifeizhou-ap deleted the lifei/cost_calculation branch October 4, 2024 01:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants