From f69ef94b98bdfbf0692fd0f265a69ddf08f154a4 Mon Sep 17 00:00:00 2001 From: haripriyarao26 Date: Mon, 29 Dec 2025 16:52:36 -0800 Subject: [PATCH 01/11] add cost estimation and health monitoring notebook --- ...ost_Estimation_And_Health_Monitoring.ipynb | 263 ++++++++++++++++++ 1 file changed, 263 insertions(+) create mode 100644 examples/Cost_Estimation_And_Health_Monitoring.ipynb diff --git a/examples/Cost_Estimation_And_Health_Monitoring.ipynb b/examples/Cost_Estimation_And_Health_Monitoring.ipynb new file mode 100644 index 000000000..121ae46f0 --- /dev/null +++ b/examples/Cost_Estimation_And_Health_Monitoring.ipynb @@ -0,0 +1,263 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "##### Copyright 2025 Google LLC." + ], + "metadata": { + "id": "LWWFilxLgFvH" + } + }, + { + "cell_type": "markdown", + "source": [ + "\n", + "\n", + "```\n", + "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n", + "# you may not use this file except in compliance with the License.\n", + "# You may obtain a copy of the License at\n", + "#\n", + "# https://www.apache.org/licenses/LICENSE-2.0\n", + "#\n", + "# Unless required by applicable law or agreed to in writing, software\n", + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", + "# See the License for the specific language governing permissions and\n", + "# limitations under the License.\n", + "```\n", + "\n" + ], + "metadata": { + "id": "zKZnDihChHk4" + } + }, + { + "cell_type": "markdown", + "source": [ + "# Cost Estimation and Health Monitoring with Gemini\n", + "\n", + "" + ], + "metadata": { + "id": "XS_CBAC0gNsd" + } + }, + { + "cell_type": "markdown", + "source": [ + "## Overview\n", + "\n", + "Cost observability is key for scaling Gemini API applications. Without tracking token usage and costs, you can't optimize your spending or identify expensive operations.\n", + "\n", + "This notebook demonstrates how to build an observability layer for Gemini API applications. You will learn how to:\n", + "\n", + "1. Extract token usage metadata from API responses.\n", + "2. Calculate real-time USD costs based on model pricing.\n", + "3. Perform health checks to verify API availability and quota.\n", + "\n", + "By the end of this notebook, you'll have a reusable class that you can integrate into any Gemini API application." + ], + "metadata": { + "id": "Vk5Chb-XgPpf" + } + }, + { + "cell_type": "code", + "source": [ + "%pip install -U -q \"google-genai\"" + ], + "metadata": { + "id": "zcEAmXVLgRNc" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "### Grab an API Key\n", + "\n", + "Before you can use the Gemini API, you must obtain an API key.\n", + "\n", + "**In Colab:** Add the key to the secrets manager under the \"🔑\" in the left panel. Give it the name `GOOGLE_API_KEY`." + ], + "metadata": { + "id": "qR32cW4ugSyv" + } + }, + { + "cell_type": "code", + "source": [ + "from google import genai\n", + "from google.genai import types\n", + "from typing import Dict, Optional\n", + "from google.colab import userdata\n", + "\n", + "# Get API key from Colab secrets\n", + "api_key = userdata.get('GOOGLE_API_KEY')\n", + "client = genai.Client(api_key=api_key)\n", + "print(\"✅ Gemini Client configured successfully!\")" + ], + "metadata": { + "id": "G0i9yHPogUa6" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "class GeminiObservability:\n", + " \"\"\"A class to monitor costs and health of Gemini API usage.\"\"\"\n", + "\n", + " def __init__(self, client):\n", + " self.client = client\n", + " # 2025 Standard Pricing (USD per 1 Million tokens)\n", + " self.prices = {\n", + " \"gemini-2.0-flash\": {\"input\": 0.10, \"output\": 0.40},\n", + " \"gemini-2.5-flash\": {\"input\": 0.075, \"output\": 0.30},\n", + " \"gemini-2.5-pro\": {\"input\": 3.50, \"output\": 10.50},\n", + " \"gemini-3.0-flash-preview\": {\"input\": 0.15, \"output\": 0.60},\n", + " }\n", + "\n", + " def check_health(self, model_name: str = \"gemini-2.0-flash\") -> bool:\n", + " \"\"\"Verifies API connectivity with a small ping.\"\"\"\n", + " try:\n", + " self.client.models.generate_content(\n", + " model=model_name,\n", + " contents=\"ping\",\n", + " config=types.GenerateContentConfig(max_output_tokens=1)\n", + " )\n", + " return True\n", + " except Exception as e:\n", + " print(f\"Health Check Failed for {model_name}: {e}\")\n", + " return False\n", + "\n", + " def estimate_cost(self, usage_metadata, model_name: str) -> float:\n", + " \"\"\"Calculates cost based on 2025 token rates.\"\"\"\n", + " if model_name not in self.prices:\n", + " print(f\"Warning: No pricing data for {model_name}.\")\n", + " return 0.0\n", + "\n", + " rates = self.prices[model_name]\n", + " in_cost = (usage_metadata.input_token_count / 1_000_000) * rates[\"input\"]\n", + " out_cost = (usage_metadata.output_token_count / 1_000_000) * rates[\"output\"]\n", + " return in_cost + out_cost\n", + "\n", + " def get_usage_summary(self, usage_metadata, model_name: str) -> Dict:\n", + " \"\"\"Returns a scannable dictionary of usage and cost.\"\"\"\n", + " cost = self.estimate_cost(usage_metadata, model_name)\n", + " return {\n", + " \"model\": model_name,\n", + " \"input_tokens\": usage_metadata.input_token_count,\n", + " \"output_tokens\": usage_metadata.output_token_count,\n", + " \"total_tokens\": usage_metadata.input_token_count + usage_metadata.output_token_count,\n", + " \"estimated_cost_usd\": round(cost, 6)\n", + " }" + ], + "metadata": { + "id": "vnfyGQ_IgV3W" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Initialize the class\n", + "observability = GeminiObservability(client)\n", + "\n", + "# Select a 2025 model (avoiding older retired IDs)\n", + "active_model = \"gemini-2.0-flash\"\n", + "\n", + "if observability.check_health(active_model):\n", + " print(f\"API Health: ✅ Healthy ({active_model})\")\n", + "\n", + " # Execute a standard content generation\n", + " response = client.models.generate_content(\n", + " model=active_model,\n", + " contents=\"Briefly explain why token tracking is important for software scaling.\"\n", + " )\n", + "\n", + " # Generate and print the summary\n", + " report = observability.get_usage_summary(response.usage_metadata, active_model)\n", + " print(\"\\n--- Observability Report ---\")\n", + " for key, val in report.items():\n", + " print(f\"{key.replace('_', ' ').title()}: {val}\")\n", + "else:\n", + " print(\"❌ API Unhealthy. Please check your API key and model permissions.\")" + ], + "metadata": { + "id": "pinhZ6S8gXz2" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# 1. Initialize report to avoid NameError\n", + "report = None\n", + "active_model = \"gemini-2.0-flash\"\n", + "\n", + "# 2. Run the health check and content generation\n", + "status = observability.check_health(active_model)\n", + "\n", + "if status == \"HEALTHY\":\n", + " response = client.models.generate_content(\n", + " model=active_model,\n", + " contents=\"Give me a 1-sentence tip for saving cloud costs.\"\n", + " )\n", + "\n", + " # Define the report variable here\n", + " report = observability.get_usage_summary(response.usage_metadata, active_model)\n", + "\n", + " print(\"\\n--- Usage Summary ---\")\n", + " print(report)\n", + "\n", + " # 3. Log to file only if report was successfully created\n", + " if report:\n", + " log_usage_to_file(report)\n", + "\n", + "elif status == \"RATE_LIMITED\":\n", + " print(\"❌ API is currently rate limited. Wait 60 seconds and try again.\")\n", + "else:\n", + " print(\"❌ API is unhealthy. Check your credentials.\")" + ], + "metadata": { + "id": "29M5u1SuuTW9" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Next Steps\n", + "\n", + "1. **Production Integration**: Use the `GeminiObservability` class as a wrapper around your generation calls.\n", + "2. **Alerting**: Store the output of `estimate_cost` in a database to set budget alerts.\n", + "3. **Pricing Updates**: Update the `prices` dictionary via the constructor to match the latest [official pricing](https://ai.google.dev/pricing)." + ], + "metadata": { + "id": "PG6VQiyigeIQ" + } + } + ] +} \ No newline at end of file From c6ad717ff35c7ee074b9fd3f14ef3cf374ef3e20 Mon Sep 17 00:00:00 2001 From: haripriyarao26 Date: Mon, 29 Dec 2025 16:55:08 -0800 Subject: [PATCH 02/11] update notebook to follow rules --- ...ost_Estimation_And_Health_Monitoring.ipynb | 382 +++++++++++------- 1 file changed, 244 insertions(+), 138 deletions(-) diff --git a/examples/Cost_Estimation_And_Health_Monitoring.ipynb b/examples/Cost_Estimation_And_Health_Monitoring.ipynb index 121ae46f0..48a90478a 100644 --- a/examples/Cost_Estimation_And_Health_Monitoring.ipynb +++ b/examples/Cost_Estimation_And_Health_Monitoring.ipynb @@ -1,34 +1,20 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, "cells": [ { "cell_type": "markdown", - "source": [ - "##### Copyright 2025 Google LLC." - ], "metadata": { "id": "LWWFilxLgFvH" - } + }, + "source": [ + "##### Copyright 2025 Google LLC." + ] }, { "cell_type": "markdown", + "metadata": { + "id": "zKZnDihChHk4" + }, "source": [ - "\n", - "\n", - "```\n", "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", @@ -40,26 +26,25 @@ "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License.\n", - "```\n", "\n" - ], - "metadata": { - "id": "zKZnDihChHk4" - } + ] }, { "cell_type": "markdown", + "metadata": { + "id": "XS_CBAC0gNsd" + }, "source": [ "# Cost Estimation and Health Monitoring with Gemini\n", "\n", "" - ], - "metadata": { - "id": "XS_CBAC0gNsd" - } + ] }, { "cell_type": "markdown", + "metadata": { + "id": "Vk5Chb-XgPpf" + }, "source": [ "## Overview\n", "\n", @@ -67,101 +52,169 @@ "\n", "This notebook demonstrates how to build an observability layer for Gemini API applications. You will learn how to:\n", "\n", - "1. Extract token usage metadata from API responses.\n", - "2. Calculate real-time USD costs based on model pricing.\n", - "3. Perform health checks to verify API availability and quota.\n", + "1. Extract token usage metadata from API responses\n", + "2. Calculate real-time USD costs based on model pricing\n", + "3. Perform health checks to verify API availability and quota\n", "\n", - "By the end of this notebook, you'll have a reusable class that you can integrate into any Gemini API application." - ], - "metadata": { - "id": "Vk5Chb-XgPpf" - } + "By the end of this notebook, you'll have a reusable class that you can integrate into any Gemini API application to monitor costs and health." + ] }, { "cell_type": "code", - "source": [ - "%pip install -U -q \"google-genai\"" - ], + "execution_count": null, "metadata": { "id": "zcEAmXVLgRNc" }, - "execution_count": null, - "outputs": [] + "outputs": [], + "source": [ + "## Setup\n", + "\n", + "First, install the Gemini API Python library." + ] }, { "cell_type": "markdown", + "metadata": { + "id": "qR32cW4ugSyv" + }, + "source": [ + "%pip install -U -q \"google-genai\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "G0i9yHPogUa6" + }, + "outputs": [], "source": [ "### Grab an API Key\n", "\n", - "Before you can use the Gemini API, you must obtain an API key.\n", + "Before you can use the Gemini API, you must first obtain an API key. If you don't already have one, create a key with one click in Google AI Studio.\n", "\n", - "**In Colab:** Add the key to the secrets manager under the \"🔑\" in the left panel. Give it the name `GOOGLE_API_KEY`." - ], - "metadata": { - "id": "qR32cW4ugSyv" - } + "Get an API key\n", + "\n", + "In Colab, add the key to the secrets manager under the \"🔑\" in the left panel. Give it the name `GOOGLE_API_KEY`." + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "vnfyGQ_IgV3W" + }, + "outputs": [], "source": [ "from google import genai\n", "from google.genai import types\n", - "from typing import Dict, Optional\n", + "from typing import Dict\n", "from google.colab import userdata\n", "\n", "# Get API key from Colab secrets\n", "api_key = userdata.get('GOOGLE_API_KEY')\n", "client = genai.Client(api_key=api_key)\n", - "print(\"✅ Gemini Client configured successfully!\")" - ], + "print(\"✅ API key configured successfully!\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": { - "id": "G0i9yHPogUa6" + "id": "pinhZ6S8gXz2" }, - "execution_count": null, - "outputs": [] + "outputs": [], + "source": [ + "## Cost Estimator Class\n", + "\n", + "The `GeminiObservability` class provides a simple way to track costs and monitor the health of your Gemini API usage. It handles different pricing for different models automatically." + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "29M5u1SuuTW9" + }, + "outputs": [], "source": [ + "# @title\n", "class GeminiObservability:\n", " \"\"\"A class to monitor costs and health of Gemini API usage.\"\"\"\n", "\n", " def __init__(self, client):\n", + " \"\"\"\n", + " Initialize the observability class.\n", + "\n", + " Args:\n", + " client: The genai.Client instance\n", + " \"\"\"\n", " self.client = client\n", - " # 2025 Standard Pricing (USD per 1 Million tokens)\n", + " # Prices per 1 million tokens (Example 2025 rates)\n", + " # Update these prices based on current pricing at https://ai.google.dev/pricing\n", " self.prices = {\n", - " \"gemini-2.0-flash\": {\"input\": 0.10, \"output\": 0.40},\n", " \"gemini-2.5-flash\": {\"input\": 0.075, \"output\": 0.30},\n", - " \"gemini-2.5-pro\": {\"input\": 3.50, \"output\": 10.50},\n", - " \"gemini-3.0-flash-preview\": {\"input\": 0.15, \"output\": 0.60},\n", + " \"gemini-2.5-pro\": {\"input\": 3.50, \"output\": 10.50},\n", + " \"gemini-2.5-flash-lite\": {\"input\": 0.0375, \"output\": 0.15},\n", + " \"gemini-3-flash-preview\": {\"input\": 0.10, \"output\": 0.40},\n", + " \"gemini-3-pro-preview\": {\"input\": 3.50, \"output\": 10.50},\n", " }\n", "\n", - " def check_health(self, model_name: str = \"gemini-2.0-flash\") -> bool:\n", - " \"\"\"Verifies API connectivity with a small ping.\"\"\"\n", + " def estimate_cost(self, usage_metadata, model_name: str) -> float:\n", + " \"\"\"\n", + " Calculate the cost in USD based on token usage.\n", + "\n", + " Args:\n", + " usage_metadata: The usage_metadata object from a Gemini API response\n", + " model_name: The name of the model used (e.g., \"gemini-2.5-flash\")\n", + "\n", + " Returns:\n", + " The estimated cost in USD\n", + " \"\"\"\n", + " if model_name not in self.prices:\n", + " print(f\"Warning: No pricing data for model {model_name}. Returning 0.0\")\n", + " return 0.0\n", + "\n", + " # Calculate input cost (prompt tokens)\n", + " input_cost = (usage_metadata.input_token_count / 1_000_000) * self.prices[model_name][\"input\"]\n", + "\n", + " # Calculate output cost (response tokens)\n", + " output_cost = (usage_metadata.output_token_count / 1_000_000) * self.prices[model_name][\"output\"]\n", + "\n", + " return input_cost + output_cost\n", + "\n", + " def check_health(self, model_name: str = \"gemini-2.5-flash\") -> bool:\n", + " \"\"\"\n", + " Perform a simple health check by making a minimal API call.\n", + "\n", + " Args:\n", + " model_name: The model to test (default: \"gemini-2.5-flash\")\n", + "\n", + " Returns:\n", + " True if the API is healthy, False otherwise\n", + " \"\"\"\n", " try:\n", - " self.client.models.generate_content(\n", + " response = self.client.models.generate_content(\n", " model=model_name,\n", " contents=\"ping\",\n", " config=types.GenerateContentConfig(max_output_tokens=1)\n", " )\n", " return True\n", " except Exception as e:\n", - " print(f\"Health Check Failed for {model_name}: {e}\")\n", + " print(f\"Health Check Failed: {e}\")\n", " return False\n", "\n", - " def estimate_cost(self, usage_metadata, model_name: str) -> float:\n", - " \"\"\"Calculates cost based on 2025 token rates.\"\"\"\n", - " if model_name not in self.prices:\n", - " print(f\"Warning: No pricing data for {model_name}.\")\n", - " return 0.0\n", + " def get_usage_summary(self, usage_metadata, model_name: str) -> Dict:\n", + " \"\"\"\n", + " Get a summary of token usage and cost.\n", "\n", - " rates = self.prices[model_name]\n", - " in_cost = (usage_metadata.input_token_count / 1_000_000) * rates[\"input\"]\n", - " out_cost = (usage_metadata.output_token_count / 1_000_000) * rates[\"output\"]\n", - " return in_cost + out_cost\n", + " Args:\n", + " usage_metadata: The usage_metadata object from a Gemini API response\n", + " model_name: The name of the model used\n", "\n", - " def get_usage_summary(self, usage_metadata, model_name: str) -> Dict:\n", - " \"\"\"Returns a scannable dictionary of usage and cost.\"\"\"\n", + " Returns:\n", + " A dictionary with usage details and cost\n", + " \"\"\"\n", " cost = self.estimate_cost(usage_metadata, model_name)\n", " return {\n", " \"model\": model_name,\n", @@ -170,94 +223,147 @@ " \"total_tokens\": usage_metadata.input_token_count + usage_metadata.output_token_count,\n", " \"estimated_cost_usd\": round(cost, 6)\n", " }" - ], + ] + }, + { + "cell_type": "markdown", "metadata": { - "id": "vnfyGQ_IgV3W" + "id": "PG6VQiyigeIQ" }, + "source": [ + "## Example Usage\n", + "\n", + "Let's see the observability class in action. First, create an instance of the class." + ] + }, + { + "cell_type": "code", "execution_count": null, - "outputs": [] + "metadata": {}, + "outputs": [], + "source": [ + "# Create an instance of the observability class\n", + "observability = GeminiObservability(client)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] }, { "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ - "# Initialize the class\n", - "observability = GeminiObservability(client)\n", + "MODEL_ID = \"gemini-2.5-flash\" # @param [\"gemini-2.5-flash-lite\", \"gemini-2.5-flash\", \"gemini-2.5-pro\", \"gemini-3-flash-preview\", \"gemini-3-pro-preview\"] {\"allow-input\": true, \"isTemplate\": true}\n", "\n", - "# Select a 2025 model (avoiding older retired IDs)\n", - "active_model = \"gemini-2.0-flash\"\n", + "# Check if the API is healthy\n", + "is_healthy = observability.check_health(MODEL_ID)\n", + "print(f\"API Health Status: {'✅ Healthy' if is_healthy else '❌ Unhealthy'}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Cost Estimation Example\n", "\n", - "if observability.check_health(active_model):\n", - " print(f\"API Health: ✅ Healthy ({active_model})\")\n", + "Make a simple API call and track the cost.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Make a simple API call\n", + "response = client.models.generate_content(\n", + " model=MODEL_ID,\n", + " contents=\"Explain quantum computing in one sentence.\"\n", + ")\n", "\n", - " # Execute a standard content generation\n", - " response = client.models.generate_content(\n", - " model=active_model,\n", - " contents=\"Briefly explain why token tracking is important for software scaling.\"\n", - " )\n", + "# Get usage metadata from the response\n", + "usage_metadata = response.usage_metadata\n", "\n", - " # Generate and print the summary\n", - " report = observability.get_usage_summary(response.usage_metadata, active_model)\n", - " print(\"\\n--- Observability Report ---\")\n", - " for key, val in report.items():\n", - " print(f\"{key.replace('_', ' ').title()}: {val}\")\n", - "else:\n", - " print(\"❌ API Unhealthy. Please check your API key and model permissions.\")" - ], - "metadata": { - "id": "pinhZ6S8gXz2" - }, - "execution_count": null, - "outputs": [] + "# Calculate and display the cost\n", + "summary = observability.get_usage_summary(usage_metadata, MODEL_ID)\n", + "print(\"Usage Summary:\")\n", + "for key, value in summary.items():\n", + " print(f\" {key}: {value}\")\n" + ] }, { - "cell_type": "code", + "cell_type": "markdown", + "metadata": {}, "source": [ - "# 1. Initialize report to avoid NameError\n", - "report = None\n", - "active_model = \"gemini-2.0-flash\"\n", + "### Tracking Multiple Requests\n", "\n", - "# 2. Run the health check and content generation\n", - "status = observability.check_health(active_model)\n", + "You can track costs across multiple API calls to monitor your total spending.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Example: Track costs across multiple requests\n", + "total_cost = 0.0\n", + "queries = [\n", + " \"What is machine learning?\",\n", + " \"Explain neural networks briefly.\",\n", + " \"What is the difference between AI and ML?\"\n", + "]\n", "\n", - "if status == \"HEALTHY\":\n", + "print(\"Tracking costs for multiple queries:\\n\")\n", + "for i, query in enumerate(queries, 1):\n", " response = client.models.generate_content(\n", - " model=active_model,\n", - " contents=\"Give me a 1-sentence tip for saving cloud costs.\"\n", + " model=MODEL_ID,\n", + " contents=query\n", " )\n", + " usage_metadata = response.usage_metadata\n", + " cost = observability.estimate_cost(usage_metadata, MODEL_ID)\n", + " total_cost += cost\n", "\n", - " # Define the report variable here\n", - " report = observability.get_usage_summary(response.usage_metadata, active_model)\n", - "\n", - " print(\"\\n--- Usage Summary ---\")\n", - " print(report)\n", + " total_tokens = usage_metadata.input_token_count + usage_metadata.output_token_count\n", + " print(f\"Query {i}: {query[:50]}...\")\n", + " print(f\" Tokens: {total_tokens} | Cost: ${cost:.6f}\\n\")\n", "\n", - " # 3. Log to file only if report was successfully created\n", - " if report:\n", - " log_usage_to_file(report)\n", - "\n", - "elif status == \"RATE_LIMITED\":\n", - " print(\"❌ API is currently rate limited. Wait 60 seconds and try again.\")\n", - "else:\n", - " print(\"❌ API is unhealthy. Check your credentials.\")" - ], - "metadata": { - "id": "29M5u1SuuTW9" - }, - "execution_count": null, - "outputs": [] + "print(f\"Total cost for all queries: ${total_cost:.6f}\")\n" + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Next Steps\n", "\n", - "1. **Production Integration**: Use the `GeminiObservability` class as a wrapper around your generation calls.\n", - "2. **Alerting**: Store the output of `estimate_cost` in a database to set budget alerts.\n", - "3. **Pricing Updates**: Update the `prices` dictionary via the constructor to match the latest [official pricing](https://ai.google.dev/pricing)." - ], - "metadata": { - "id": "PG6VQiyigeIQ" - } + "Now that you have a working cost estimation and health monitoring system, you can:\n", + "\n", + "1. **Integrate into your applications**: Add the `GeminiObservability` class to your production code to track costs in real-time\n", + "2. **Set up alerts**: Monitor total costs and set up alerts when spending exceeds thresholds\n", + "3. **Optimize usage**: Use the token counts to identify expensive operations and optimize your prompts\n", + "4. **Update pricing**: Keep the pricing dictionary up-to-date with current rates from [Google AI Pricing](https://ai.google.dev/pricing)\n", + "\n", + "For more information about the Gemini API, check out the [quickstarts](https://github.com/google-gemini/cookbook/tree/main/quickstarts) and other [examples](https://github.com/google-gemini/cookbook/tree/main/examples).\n" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python" } - ] -} \ No newline at end of file + }, + "nbformat": 4, + "nbformat_minor": 0 +} From e0edb2d2b87cd26200f3ebc482b3fae6cdd4ce35 Mon Sep 17 00:00:00 2001 From: haripriyarao26 Date: Mon, 29 Dec 2025 17:11:22 -0800 Subject: [PATCH 03/11] update readme --- examples/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/README.md b/examples/README.md index ac974f429..9e711871c 100644 --- a/examples/README.md +++ b/examples/README.md @@ -34,11 +34,10 @@ This is a collection of fun and helpful examples for the Gemini API. | [Talk to documents](./Talk_to_documents_with_embeddings.ipynb) | Use embeddings to search through a custom database. | Embeddings | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Talk_to_documents_with_embeddings.ipynb) | | [Entity extraction](./Entity_Extraction.ipynb) | Use Gemini API to speed up some of your tasks, such as searching through text to extract needed information. Entity extraction with a Gemini model is a simple query, and you can ask it to retrieve its answer in the form that you prefer. | Embeddings | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Entity_Extraction.ipynb) | | [Google I/O 2025 Live coding session](./Google_IO2025_Live_Coding.ipynb) | Play with the notebook used during the Google I/O 2025 live coding session delivered by the Google DeepMind DevRel team. Work with the Gemini API SDK, know and practice with the GenMedia models, the thinking capable models, start using the Gemini API tools and more! | Gemini API and its models and features | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Google_IO2025_Live_Coding.ipynb) | - | [Gemini MultimodalBot Text & Image Tutorial](./Gemini_MultimodalBot_Text_Image_Tutorial.ipynb) | Learn how to use Gemini's multimodal capabilities to process both text and image inputs. | Multimodal | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Gemini_MultimodalBot_Text_Image_Tutorial.ipynb) | - | [EU AI Act Compliance with Agent Module](./Agent_Module_EU_AI_Act_Compliance.ipynb) | Use Gemini function calling with Agent Module API for EU AI Act compliance checking | Function Calling, Tools | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Agent_Module_EU_AI_Act_Compliance.ipynb) | | [GitHub issue analyzer](./GitHub_issue_analyzer.ipynb) | Use Gemini API to interact with PyGitHub API and extract useful insights from this repository | Gemini API, Tools | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/GitHub_issue_analyzer.ipynb) | +| [Cost Estimation and Health Monitoring](./Cost_Estimation_And_Health_Monitoring.ipynb) | Build an observability layer for Gemini API applications to estimate USD costs and monitor health | Observability, Cost Estimation, Health Monitoring | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Cost_Estimation_And_Health_Monitoring.ipynb) | ---
Some old examples are still using the legacy SDK, they should still work and are still worth checking to get ideas: From 12134c1e93fcd8ad0f4639121e8525682f7c87af Mon Sep 17 00:00:00 2001 From: haripriyarao26 Date: Mon, 29 Dec 2025 17:13:52 -0800 Subject: [PATCH 04/11] add exception --- examples/Cost_Estimation_And_Health_Monitoring.ipynb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/Cost_Estimation_And_Health_Monitoring.ipynb b/examples/Cost_Estimation_And_Health_Monitoring.ipynb index 48a90478a..aec817bf9 100644 --- a/examples/Cost_Estimation_And_Health_Monitoring.ipynb +++ b/examples/Cost_Estimation_And_Health_Monitoring.ipynb @@ -200,6 +200,9 @@ " config=types.GenerateContentConfig(max_output_tokens=1)\n", " )\n", " return True\n", + " except exceptions.ResourceExhausted as e:\n", + " print(f\"Health Check Failed: Quota limit reached. {e}\")\n", + " return False\n", " except Exception as e:\n", " print(f\"Health Check Failed: {e}\")\n", " return False\n", @@ -357,11 +360,13 @@ "provenance": [] }, "kernelspec": { - "display_name": "Python 3", + "display_name": ".venv", + "language": "python", "name": "python3" }, "language_info": { - "name": "python" + "name": "python", + "version": "3.14.0" } }, "nbformat": 4, From 641bcc8168b31832ea1af0b196fef92a5fc0c545 Mon Sep 17 00:00:00 2001 From: haripriyarao26 Date: Mon, 29 Dec 2025 17:16:01 -0800 Subject: [PATCH 05/11] update file name in colab --- examples/Cost_Estimation_And_Health_Monitoring.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Cost_Estimation_And_Health_Monitoring.ipynb b/examples/Cost_Estimation_And_Health_Monitoring.ipynb index aec817bf9..6facf82b3 100644 --- a/examples/Cost_Estimation_And_Health_Monitoring.ipynb +++ b/examples/Cost_Estimation_And_Health_Monitoring.ipynb @@ -37,7 +37,7 @@ "source": [ "# Cost Estimation and Health Monitoring with Gemini\n", "\n", - "" + "" ] }, { From e3a9eea53136e48422c55837b1303d8801ea73ec Mon Sep 17 00:00:00 2001 From: haripriyarao26 Date: Mon, 29 Dec 2025 17:18:37 -0800 Subject: [PATCH 06/11] solve the minor grammatical comments --- ...ost_Estimation_And_Health_Monitoring.ipynb | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/examples/Cost_Estimation_And_Health_Monitoring.ipynb b/examples/Cost_Estimation_And_Health_Monitoring.ipynb index 6facf82b3..baa3bbff0 100644 --- a/examples/Cost_Estimation_And_Health_Monitoring.ipynb +++ b/examples/Cost_Estimation_And_Health_Monitoring.ipynb @@ -78,7 +78,7 @@ "id": "qR32cW4ugSyv" }, "source": [ - "%pip install -U -q \"google-genai\"" + "%pip install -U -q \"google-genai>=1.0.0\"\n" ] }, { @@ -95,7 +95,7 @@ "\n", "Get an API key\n", "\n", - "In Colab, add the key to the secrets manager under the \"🔑\" in the left panel. Give it the name `GOOGLE_API_KEY`." + "In Colab, add the key to the secrets manager under the \"\ud83d\udd11\" in the left panel. Give it the name `GOOGLE_API_KEY`." ] }, { @@ -114,7 +114,7 @@ "# Get API key from Colab secrets\n", "api_key = userdata.get('GOOGLE_API_KEY')\n", "client = genai.Client(api_key=api_key)\n", - "print(\"✅ API key configured successfully!\")" + "print(\"\u2705 API key configured successfully!\")" ] }, { @@ -160,7 +160,7 @@ " \"gemini-3-pro-preview\": {\"input\": 3.50, \"output\": 10.50},\n", " }\n", "\n", - " def estimate_cost(self, usage_metadata, model_name: str) -> float:\n", + " def estimate_cost(self, usage_metadata: types.UsageMetadata, model_name: str) -> float:\n", " \"\"\"\n", " Calculate the cost in USD based on token usage.\n", "\n", @@ -176,10 +176,14 @@ " return 0.0\n", "\n", " # Calculate input cost (prompt tokens)\n", - " input_cost = (usage_metadata.input_token_count / 1_000_000) * self.prices[model_name][\"input\"]\n", + " input_cost = (\n", + " usage_metadata.input_token_count / 1_000_000\n", + " ) * self.prices[model_name][\"input\"]\n", "\n", " # Calculate output cost (response tokens)\n", - " output_cost = (usage_metadata.output_token_count / 1_000_000) * self.prices[model_name][\"output\"]\n", + " output_cost = (\n", + " usage_metadata.output_token_count / 1_000_000\n", + " ) * self.prices[model_name][\"output\"]\n", "\n", " return input_cost + output_cost\n", "\n", @@ -207,7 +211,7 @@ " print(f\"Health Check Failed: {e}\")\n", " return False\n", "\n", - " def get_usage_summary(self, usage_metadata, model_name: str) -> Dict:\n", + " def get_usage_summary(self, usage_metadata: types.UsageMetadata, model_name: str) -> Dict:\n", " \"\"\"\n", " Get a summary of token usage and cost.\n", "\n", @@ -223,7 +227,7 @@ " \"model\": model_name,\n", " \"input_tokens\": usage_metadata.input_token_count,\n", " \"output_tokens\": usage_metadata.output_token_count,\n", - " \"total_tokens\": usage_metadata.input_token_count + usage_metadata.output_token_count,\n", + " \"total_tokens\": usage_metadata.total_token_count,\n", " \"estimated_cost_usd\": round(cost, 6)\n", " }" ] @@ -236,7 +240,7 @@ "source": [ "## Example Usage\n", "\n", - "Let's see the observability class in action. First, create an instance of the class." + "You can now see the observability class in action. First, create an instance of the class.\n" ] }, { @@ -264,7 +268,7 @@ "\n", "# Check if the API is healthy\n", "is_healthy = observability.check_health(MODEL_ID)\n", - "print(f\"API Health Status: {'✅ Healthy' if is_healthy else '❌ Unhealthy'}\")\n" + "print(f\"API Health Status: {'\u2705 Healthy' if is_healthy else '\u274c Unhealthy'}\")\n" ] }, { @@ -331,7 +335,7 @@ " cost = observability.estimate_cost(usage_metadata, MODEL_ID)\n", " total_cost += cost\n", "\n", - " total_tokens = usage_metadata.input_token_count + usage_metadata.output_token_count\n", + " total_tokens = usage_metadata.total_token_count\n", " print(f\"Query {i}: {query[:50]}...\")\n", " print(f\" Tokens: {total_tokens} | Cost: ${cost:.6f}\\n\")\n", "\n", @@ -371,4 +375,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file From 45aacb9d1fa6e641615f7f8f5900675fa17506ae Mon Sep 17 00:00:00 2001 From: haripriyarao26 Date: Tue, 19 May 2026 10:06:24 -0700 Subject: [PATCH 07/11] updates : Cell type fixes - Exception fix --- ...ost_Estimation_And_Health_Monitoring.ipynb | 58 +++++++++---------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/examples/Cost_Estimation_And_Health_Monitoring.ipynb b/examples/Cost_Estimation_And_Health_Monitoring.ipynb index baa3bbff0..d97a8aea1 100644 --- a/examples/Cost_Estimation_And_Health_Monitoring.ipynb +++ b/examples/Cost_Estimation_And_Health_Monitoring.ipynb @@ -60,12 +60,10 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": { "id": "zcEAmXVLgRNc" }, - "outputs": [], "source": [ "## Setup\n", "\n", @@ -82,12 +80,10 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": { "id": "G0i9yHPogUa6" }, - "outputs": [], "source": [ "### Grab an API Key\n", "\n", @@ -95,35 +91,33 @@ "\n", "Get an API key\n", "\n", - "In Colab, add the key to the secrets manager under the \"\ud83d\udd11\" in the left panel. Give it the name `GOOGLE_API_KEY`." + "In Colab, add the key to the secrets manager under the \"🔑\" in the left panel. Give it the name `GOOGLE_API_KEY`." ] }, { "cell_type": "code", - "execution_count": null, "metadata": { "id": "vnfyGQ_IgV3W" }, - "outputs": [], "source": [ "from google import genai\n", - "from google.genai import types\n", + "from google.genai import types, errors\n", "from typing import Dict\n", "from google.colab import userdata\n", "\n", "# Get API key from Colab secrets\n", "api_key = userdata.get('GOOGLE_API_KEY')\n", "client = genai.Client(api_key=api_key)\n", - "print(\"\u2705 API key configured successfully!\")" - ] + "print(\"✅ API key configured successfully!\")" + ], + "execution_count": null, + "outputs": [] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": { "id": "pinhZ6S8gXz2" }, - "outputs": [], "source": [ "## Cost Estimator Class\n", "\n", @@ -132,11 +126,9 @@ }, { "cell_type": "code", - "execution_count": null, "metadata": { "id": "29M5u1SuuTW9" }, - "outputs": [], "source": [ "# @title\n", "class GeminiObservability:\n", @@ -204,7 +196,7 @@ " config=types.GenerateContentConfig(max_output_tokens=1)\n", " )\n", " return True\n", - " except exceptions.ResourceExhausted as e:\n", + " except errors.APIError as e:\n", " print(f\"Health Check Failed: Quota limit reached. {e}\")\n", " return False\n", " except Exception as e:\n", @@ -230,7 +222,9 @@ " \"total_tokens\": usage_metadata.total_token_count,\n", " \"estimated_cost_usd\": round(cost, 6)\n", " }" - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "markdown", @@ -245,13 +239,13 @@ }, { "cell_type": "code", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "# Create an instance of the observability class\n", "observability = GeminiObservability(client)\n" - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "markdown", @@ -260,16 +254,16 @@ }, { "cell_type": "code", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "MODEL_ID = \"gemini-2.5-flash\" # @param [\"gemini-2.5-flash-lite\", \"gemini-2.5-flash\", \"gemini-2.5-pro\", \"gemini-3-flash-preview\", \"gemini-3-pro-preview\"] {\"allow-input\": true, \"isTemplate\": true}\n", "\n", "# Check if the API is healthy\n", "is_healthy = observability.check_health(MODEL_ID)\n", - "print(f\"API Health Status: {'\u2705 Healthy' if is_healthy else '\u274c Unhealthy'}\")\n" - ] + "print(f\"API Health Status: {'✅ Healthy' if is_healthy else '❌ Unhealthy'}\")\n" + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "markdown", @@ -282,9 +276,7 @@ }, { "cell_type": "code", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "# Make a simple API call\n", "response = client.models.generate_content(\n", @@ -300,7 +292,9 @@ "print(\"Usage Summary:\")\n", "for key, value in summary.items():\n", " print(f\" {key}: {value}\")\n" - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "markdown", @@ -313,9 +307,7 @@ }, { "cell_type": "code", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "# Example: Track costs across multiple requests\n", "total_cost = 0.0\n", @@ -340,7 +332,9 @@ " print(f\" Tokens: {total_tokens} | Cost: ${cost:.6f}\\n\")\n", "\n", "print(f\"Total cost for all queries: ${total_cost:.6f}\")\n" - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "markdown", From 9e5460284a95ded67dc10e88ab95c7196fe6beac Mon Sep 17 00:00:00 2001 From: haripriyarao26 Date: Tue, 19 May 2026 10:08:59 -0700 Subject: [PATCH 08/11] fix: outdated token attributes --- examples/Cost_Estimation_And_Health_Monitoring.ipynb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/Cost_Estimation_And_Health_Monitoring.ipynb b/examples/Cost_Estimation_And_Health_Monitoring.ipynb index d97a8aea1..3aab3b301 100644 --- a/examples/Cost_Estimation_And_Health_Monitoring.ipynb +++ b/examples/Cost_Estimation_And_Health_Monitoring.ipynb @@ -169,12 +169,12 @@ "\n", " # Calculate input cost (prompt tokens)\n", " input_cost = (\n", - " usage_metadata.input_token_count / 1_000_000\n", + " usage_metadata.prompt_token_count / 1_000_000\n", " ) * self.prices[model_name][\"input\"]\n", "\n", " # Calculate output cost (response tokens)\n", " output_cost = (\n", - " usage_metadata.output_token_count / 1_000_000\n", + " usage_metadata.candidates_token_count / 1_000_000\n", " ) * self.prices[model_name][\"output\"]\n", "\n", " return input_cost + output_cost\n", @@ -217,8 +217,8 @@ " cost = self.estimate_cost(usage_metadata, model_name)\n", " return {\n", " \"model\": model_name,\n", - " \"input_tokens\": usage_metadata.input_token_count,\n", - " \"output_tokens\": usage_metadata.output_token_count,\n", + " \"input_tokens\": usage_metadata.prompt_token_count,\n", + " \"output_tokens\": usage_metadata.candidates_token_count,\n", " \"total_tokens\": usage_metadata.total_token_count,\n", " \"estimated_cost_usd\": round(cost, 6)\n", " }" From 6626a23081f757521184ed0bc19b7672df5ba0dc Mon Sep 17 00:00:00 2001 From: haripriyarao26 Date: Tue, 19 May 2026 10:20:41 -0700 Subject: [PATCH 09/11] update import error --- ...ost_Estimation_And_Health_Monitoring.ipynb | 68 +++++++++++++------ 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/examples/Cost_Estimation_And_Health_Monitoring.ipynb b/examples/Cost_Estimation_And_Health_Monitoring.ipynb index 3aab3b301..6ebc73dcd 100644 --- a/examples/Cost_Estimation_And_Health_Monitoring.ipynb +++ b/examples/Cost_Estimation_And_Health_Monitoring.ipynb @@ -96,22 +96,38 @@ }, { "cell_type": "code", + "execution_count": 1, "metadata": { "id": "vnfyGQ_IgV3W" }, + "outputs": [ + { + "ename": "ImportError", + "evalue": "cannot import name 'genai' from 'google' (unknown location)", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mImportError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mgoogle\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m genai\n\u001b[32m 2\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mgoogle\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mgenai\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m types, errors\n\u001b[32m 3\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mtyping\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Dict\n", + "\u001b[31mImportError\u001b[39m: cannot import name 'genai' from 'google' (unknown location)" + ] + } + ], "source": [ "from google import genai\n", "from google.genai import types, errors\n", "from typing import Dict\n", - "from google.colab import userdata\n", + "import os\n", + "\n", + "try:\n", + " from google.colab import userdata\n", + " api_key = userdata.get('GOOGLE_API_KEY')\n", + "except ImportError:\n", + " api_key = os.environ.get('GOOGLE_API_KEY')\n", "\n", - "# Get API key from Colab secrets\n", - "api_key = userdata.get('GOOGLE_API_KEY')\n", "client = genai.Client(api_key=api_key)\n", "print(\"✅ API key configured successfully!\")" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -126,9 +142,11 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "29M5u1SuuTW9" }, + "outputs": [], "source": [ "# @title\n", "class GeminiObservability:\n", @@ -222,9 +240,7 @@ " \"total_tokens\": usage_metadata.total_token_count,\n", " \"estimated_cost_usd\": round(cost, 6)\n", " }" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -239,13 +255,13 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ "# Create an instance of the observability class\n", "observability = GeminiObservability(client)\n" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -254,16 +270,16 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ "MODEL_ID = \"gemini-2.5-flash\" # @param [\"gemini-2.5-flash-lite\", \"gemini-2.5-flash\", \"gemini-2.5-pro\", \"gemini-3-flash-preview\", \"gemini-3-pro-preview\"] {\"allow-input\": true, \"isTemplate\": true}\n", "\n", "# Check if the API is healthy\n", "is_healthy = observability.check_health(MODEL_ID)\n", "print(f\"API Health Status: {'✅ Healthy' if is_healthy else '❌ Unhealthy'}\")\n" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -276,7 +292,9 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ "# Make a simple API call\n", "response = client.models.generate_content(\n", @@ -292,9 +310,7 @@ "print(\"Usage Summary:\")\n", "for key, value in summary.items():\n", " print(f\" {key}: {value}\")\n" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -307,7 +323,9 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ "# Example: Track costs across multiple requests\n", "total_cost = 0.0\n", @@ -332,9 +350,7 @@ " print(f\" Tokens: {total_tokens} | Cost: ${cost:.6f}\\n\")\n", "\n", "print(f\"Total cost for all queries: ${total_cost:.6f}\")\n" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -363,10 +379,18 @@ "name": "python3" }, "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", "version": "3.14.0" } }, "nbformat": 4, "nbformat_minor": 0 -} \ No newline at end of file +} From eeb79e39aa90ac6388715b60db36077f91522fab Mon Sep 17 00:00:00 2001 From: haripriyarao26 Date: Thu, 21 May 2026 23:45:32 -0700 Subject: [PATCH 10/11] update read me --- examples/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/README.md b/examples/README.md index 9e711871c..3b3d9fcfc 100644 --- a/examples/README.md +++ b/examples/README.md @@ -37,7 +37,8 @@ This is a collection of fun and helpful examples for the Gemini API. | [Gemini MultimodalBot Text & Image Tutorial](./Gemini_MultimodalBot_Text_Image_Tutorial.ipynb) | Learn how to use Gemini's multimodal capabilities to process both text and image inputs. | Multimodal | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Gemini_MultimodalBot_Text_Image_Tutorial.ipynb) | | [EU AI Act Compliance with Agent Module](./Agent_Module_EU_AI_Act_Compliance.ipynb) | Use Gemini function calling with Agent Module API for EU AI Act compliance checking | Function Calling, Tools | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Agent_Module_EU_AI_Act_Compliance.ipynb) | | [GitHub issue analyzer](./GitHub_issue_analyzer.ipynb) | Use Gemini API to interact with PyGitHub API and extract useful insights from this repository | Gemini API, Tools | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/GitHub_issue_analyzer.ipynb) | -| [Cost Estimation and Health Monitoring](./Cost_Estimation_And_Health_Monitoring.ipynb) | Build an observability layer for Gemini API applications to estimate USD costs and monitor health | Observability, Cost Estimation, Health Monitoring | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Cost_Estimation_And_Health_Monitoring.ipynb) | +| [Cost Estimation and Health Monitoring](./Cost_Estimation_And_Health_Monitoring.ipynb) | Learn how to track USD costs and monitor API health using token usage metadata. | Observability, Usage metadata | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Cost_Estimation_And_Health_Monitoring.ipynb) | + ---
Some old examples are still using the legacy SDK, they should still work and are still worth checking to get ideas: From 2943bf5874a1efbfca7f93f268ae75a6491e8c88 Mon Sep 17 00:00:00 2001 From: haripriyarao26 Date: Sat, 6 Jun 2026 23:45:17 -0700 Subject: [PATCH 11/11] Fixed automated check failure cases --- README.md | 1 + ...ost_Estimation_And_Health_Monitoring.ipynb | 61 +++++++++---------- examples/README.md | 1 - 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index f92e5ece9..0cc7ca13f 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ These examples demonstrate how to combine multiple Gemini API features or 3rd-pa * [Animated Story Generation](./examples/Animated_Story_Video_Generation_gemini.ipynb) [![Colab](https://storage.googleapis.com/generativeai-downloads/images/colab_icon16.png)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Animated_Story_Video_Generation_gemini.ipynb): Create animated videos by combining Gemini's story generation, Imagen, and audio synthesis * [Plotting and mapping Live](./examples/LiveAPI_plotting_and_mapping.ipynb) [![Colab](https://storage.googleapis.com/generativeai-downloads/images/colab_icon16.png)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/LiveAPI_plotting_and_mapping.ipynb): Mix *Live API* and *Code execution* to solve complex tasks live * [3D Spatial understanding](./examples/Spatial_understanding_3d.ipynb) [![Colab](https://storage.googleapis.com/generativeai-downloads/images/colab_icon16.png)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Spatial_understanding_3d.ipynb): Use Gemini *3D spatial* abilities to understand 3D scenes +* [Cost Estimation and Health Monitoring](./examples/Cost_Estimation_And_Health_Monitoring.ipynb) [![Colab](https://storage.googleapis.com/generativeai-downloads/images/colab_icon16.png)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Cost_Estimation_And_Health_Monitoring.ipynb): Build an observability layer for Gemini API applications to estimate USD costs and monitor health * [Gradio and live API](./examples/gradio_audio.py): Use Gradio to deploy your own instance of the *Live API* * And [many more](https://github.com/google-gemini/cookbook/tree/main/examples/)

diff --git a/examples/Cost_Estimation_And_Health_Monitoring.ipynb b/examples/Cost_Estimation_And_Health_Monitoring.ipynb index 6ebc73dcd..d40afa7b5 100644 --- a/examples/Cost_Estimation_And_Health_Monitoring.ipynb +++ b/examples/Cost_Estimation_And_Health_Monitoring.ipynb @@ -10,12 +10,15 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "metadata": { + "cellView": "form", "id": "zKZnDihChHk4" }, + "outputs": [], "source": [ - "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n", + "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", @@ -25,8 +28,7 @@ "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", - "# limitations under the License.\n", - "\n" + "# limitations under the License." ] }, { @@ -256,22 +258,21 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "f2c86818357b" + }, "outputs": [], "source": [ "# Create an instance of the observability class\n", "observability = GeminiObservability(client)\n" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [] - }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "eb33a5a5ee9e" + }, "outputs": [], "source": [ "MODEL_ID = \"gemini-2.5-flash\" # @param [\"gemini-2.5-flash-lite\", \"gemini-2.5-flash\", \"gemini-2.5-pro\", \"gemini-3-flash-preview\", \"gemini-3-pro-preview\"] {\"allow-input\": true, \"isTemplate\": true}\n", @@ -283,7 +284,9 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "id": "bb18295b1f89" + }, "source": [ "### Cost Estimation Example\n", "\n", @@ -293,7 +296,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "9b06eb681c94" + }, "outputs": [], "source": [ "# Make a simple API call\n", @@ -314,7 +319,9 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "id": "313f3a9a8677" + }, "source": [ "### Tracking Multiple Requests\n", "\n", @@ -324,7 +331,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "e477c70b731a" + }, "outputs": [], "source": [ "# Example: Track costs across multiple requests\n", @@ -354,7 +363,9 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "id": "a68bb4856bc1" + }, "source": [ "## Next Steps\n", "\n", @@ -371,24 +382,12 @@ ], "metadata": { "colab": { - "provenance": [] + "name": "Cost_Estimation_And_Health_Monitoring.ipynb", + "toc_visible": true }, "kernelspec": { - "display_name": ".venv", - "language": "python", + "display_name": "Python 3", "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.14.0" } }, "nbformat": 4, diff --git a/examples/README.md b/examples/README.md index 3b3d9fcfc..88cecb5c0 100644 --- a/examples/README.md +++ b/examples/README.md @@ -38,7 +38,6 @@ This is a collection of fun and helpful examples for the Gemini API. | [EU AI Act Compliance with Agent Module](./Agent_Module_EU_AI_Act_Compliance.ipynb) | Use Gemini function calling with Agent Module API for EU AI Act compliance checking | Function Calling, Tools | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Agent_Module_EU_AI_Act_Compliance.ipynb) | | [GitHub issue analyzer](./GitHub_issue_analyzer.ipynb) | Use Gemini API to interact with PyGitHub API and extract useful insights from this repository | Gemini API, Tools | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/GitHub_issue_analyzer.ipynb) | | [Cost Estimation and Health Monitoring](./Cost_Estimation_And_Health_Monitoring.ipynb) | Learn how to track USD costs and monitor API health using token usage metadata. | Observability, Usage metadata | [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Cost_Estimation_And_Health_Monitoring.ipynb) | - ---
Some old examples are still using the legacy SDK, they should still work and are still worth checking to get ideas: