Skip to content

Conversation

@haripriyarao26
Copy link

Fixes #1087

Key Features:

  1. Dynamic Pricing Engine: A reusable GeminiObservability class that calculates USD costs for 2025 Gemini models (2.0 Flash, 2.5 Pro, etc.).
  2. Resilient Health Checks: Implements logic to distinguish between system errors and 429 RESOURCE_EXHAUSTED quota limits.
  3. Local Usage Logging: Demonstrates how to persist usage metadata to JSON for internal budgeting and audit trails.
  4. Modern SDK: Built using the latest google-genai Python SDK.

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@github-actions github-actions bot added status:awaiting review PR awaiting review from a maintainer component:examples Issues/PR referencing examples folder labels Dec 30, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @haripriyarao26, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a comprehensive example for integrating cost estimation and health monitoring into Gemini API applications. It provides a GeminiObservability class that allows developers to track token usage, calculate real-time USD costs based on model-specific pricing, and perform robust health checks to ensure API availability and manage quota limits. This new example aims to empower users with better control and understanding of their Gemini API consumption, facilitating optimized spending and improved application resilience.

Highlights

  • New Example Notebook: Introduced a new Jupyter Notebook, Cost_Estimation_And_Health_Monitoring.ipynb, demonstrating how to implement cost observability and health checks for Gemini API applications.
  • GeminiObservability Class: Added a reusable Python class, GeminiObservability, designed to track token usage, estimate costs in USD for various Gemini models (e.g., 2.5 Flash, 2.5 Pro, 3-preview models), and perform API health checks.
  • Dynamic Pricing Engine: The GeminiObservability class includes a dynamic pricing engine with example 2025 rates for different Gemini models, allowing for real-time cost calculation based on input and output token counts.
  • API Health Checks: Implemented a check_health method within the GeminiObservability class to verify API availability and distinguish between general system errors and resource exhaustion (e.g., 429 quota limits).
  • Usage Logging and Summary: The notebook demonstrates how to extract usage metadata from API responses and generate a summary including model, token counts, and estimated cost, which can be used for internal budgeting and audit trails.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds a valuable example notebook for cost estimation and health monitoring with the Gemini API. The GeminiObservability class is a well-structured and reusable component. I have a few suggestions to align the notebook with the repository's style guide and improve the code. Specifically, the new notebook should be added to examples/README.md, and its outputs should be saved as per the style guide. There are also opportunities to improve the code by adding missing type hints, using more specific exception handling, and avoiding redundant calculations.

@haripriyarao26
Copy link
Author

@Giom-V @PaulTR

Just following up on this PR! I've implemented a Cost Observability & Health Utility that standardizes USD cost estimation and integration health checks directly from usage_metadata.

I’ve ensured the code is clean and follows the cookbook's standards. I'd love to get this merged to help other SDEs building cost-sensitive apps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component:examples Issues/PR referencing examples folder status:awaiting review PR awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding a new example - Token Usage Tracking with Health Monitoring Utility

1 participant