Skip to content

Conversation

tykimseoul
Copy link

Summary

This PR introduces a GeminiCallbackHandler, a new callback handler for monitoring token usage and costs associated with Google's Gemini models. It provides a straightforward way to track token consumption and estimate expenses when using the Gemini API, helping you stay on top of your spending without any extra hassle.

Motivation

I wanted a way to track my token usage and costs when working with Gemini models. I saw that a callback already existed for OpenAI, which was super helpful, and I thought it would be great to have a similar one for Gemini. My goal was to create a simple tool to help developers get visibility into their API usage and manage costs effectively.

Key Changes

  • langchain_community/callbacks/gemini_info.py: This new file contains the GeminiCallbackHandler class, which is responsible for calculating token usage (prompt, completion, and total) and estimating costs based on the specific Gemini model being used. The file also includes a comprehensive dictionary of Gemini model costs.
  • langchain_community/callbacks/__init__.py: The GeminiCallbackHandler has been added to the __all__ list, making it easily accessible for import within the LangChain ecosystem.
  • langchain_community/callbacks/manager.py: A new context manager, get_gemini_callback, has been introduced to provide a convenient and user-friendly way to utilize the GeminiCallbackHandler.
  • libs/community/tests/unit_tests/callbacks/test_gemini_info.py: A suite of unit tests has been added to ensure the proper functioning of the GeminiCallbackHandler. These tests cover token counting, cost calculation for various models, and the handling of different response formats.

tykimseoul and others added 2 commits September 11, 2025 11:26
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
tykimseoul and others added 3 commits September 12, 2025 08:25
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
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.

1 participant