Guides

Guide

Gemini API and Vertex AI cost tracking by customer, feature and team

Published

Your Google bill shows what Gemini and Vertex AI cost in total. It doesn't show which customer, product feature or team the spend served. This guide shows how to tag Gemini calls with that split, which paths cover the Gemini API and which cover Vertex AI, how to check the figures against your Cloud Billing export, and how to cap the spend.

What does Google's own billing data show, and what's missing?

Google's billing data shows what you spent, not who it was for. The Cloud Billing export lists spend by service, such as Vertex AI, and Vertex AI's Cloud Monitoring metrics hold token usage per model. Neither has a field for your customer, your product feature or the team that owns the code, so no export can add that split later.

A bill can say a model cost a certain amount today. It can't say whether that was one customer running a heavy report or a thousand customers sending one message each. The only moment you know the customer and the feature is when your code makes the call, so that is where tracking has to start.

How do you track Gemini and Vertex AI costs per customer, feature and team?

Tag each call with the customer, feature and team when you make it. For the Gemini API (a key from Google AI Studio), wrap the Google Gen AI client with Token Meter's SDK, or send the calls through Token Meter's router with your own Gemini API key. For your own Vertex AI traffic, use the SDK; the router can't carry it. Pick one path per call; you don't need both.

The SDK (Python or TypeScript). Install it next to Google's SDK (pip install "tokenmeter-sdk[gemini]" or npm install @tokenmeter/sdk @google/genai), wrap the client, then open an action() scope around the calls it covers. Usage is read from Google's reply after it arrives, so there is no proxy and no extra network hop. A failed Google call reaches your code unchanged, and the SDK's own record-keeping can never fail your call.

gemini-call.mjs
import { GoogleGenAI } from "@google/genai";
import { TokenMeterClient, wrapGemini, action } from "@tokenmeter/sdk";

const tl = new TokenMeterClient({ apiKey: process.env.TOKEN_METER_KEY ?? "" });
const ai = wrapGemini(new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }), tl);

const reply = await action("generate-report", { customerRef: "acme", feature: "reports", team: "growth" }, () =>
  ai.models.generateContent({
    model: "gemini-3.6-flash",
    contents: "Say hello in five words.",
  }),
);
console.log(reply.text);

await tl.flush(); // a short script sends its usage before it exits

The wrapper records generateContent and generateContentStream (generate_content and generate_content_stream in Python). Python wraps the regular client only; the async client (client.aio.models) is not recorded yet. A long-running app sends usage every 2 seconds or every 500 calls, and the call shows on Requests within a few seconds. Gemini 2.5 models are no longer offered to new Google API keys, so use a newer model.

The router (Gemini API only). The router accepts the OpenAI Chat Completions and Responses APIs and the Anthropic Messages API. To reach Gemini through it, point an OpenAI client at https://router.timewastevc.com/v1, use your Token Meter ingest key as the API key, and send a model name that contains gemini or gemma; with no routing policy, those go to Google. Attribution goes in the body as top-level fields, which the router removes before forwarding.

report.ts
import OpenAI from "openai";

const openai = new OpenAI({ baseURL: "https://router.timewastevc.com/v1", apiKey: process.env.TOKEN_METER_KEY });

// Built as a variable, so TypeScript accepts the extra fields the OpenAI types don't list.
const body = {
  model: "gemini-3.6-flash",
  messages: [{ role: "user" as const, content: "Say hello in five words." }],
  action: "generate-report",
  customer_ref: "acme",
  feature: "reports",
  team: "growth",
};
await openai.chat.completions.create(body);

Before the first named-model request, an Owner or Admin opens Router › Providers, chooses Add provider key, picks Google Gemini and pastes a Gemini API key from Google AI Studio into Inference API key. Then, under Advanced, they choose Add capability for each model you call. Without the key the router answers 424 provider_not_configured; without the capability record, 400 model_capability_not_configured. Google bills you for calls on your key.

Your trafficDocumented way to tag it per call
Gemini API, Google Gen AI SDKtokenmeter.wrap(genai.Client()) or wrapGemini.
Gemini API, any OpenAI-compatible clientThe router, with your Gemini API key stored under Router › Providers.
Gemini on Vertex AI, Vercel AI SDKtokenmeterMiddleware on a @ai-sdk/google-vertex model, recorded under Google.
Claude on Vertex AIAnthropic's AnthropicVertex client wrapped like any Anthropic client, or @ai-sdk/google-vertex/anthropic with the middleware (SDK 0.2.1 or later). Both are recorded under Google.

The router can't carry your own Vertex AI traffic: Google keys stored for the router must be Gemini API keys from Google AI Studio, and you can't store your own Vertex AI service account. Vertex AI appears in the router only as an endpoint on Token Meter's own accounts, used for auto routing or when a workspace pays from Token Meter credits; a call that names a model otherwise stays on your own key. The Gemini wrapper records the client's models calls under Google without checking how the client was set up, so a Google Gen AI client set up for Vertex AI goes through the same code. The setup steps use an AI Studio key, so make one test call from a Vertex AI client and check Requests before relying on it.

Don't send the same call through the router with a wrapped client: it is metered twice. A call with no tags still counts in every total; it just shows as Unattributed. Details: Providers, Frameworks, Router and Attribution. To turn per-customer cost into margin, see LLM cost per customer.

How do you check the figures against your Google Cloud bill?

Connect your Cloud Billing account. Token Meter reads your Standard usage cost billing export in BigQuery, imports the AI services you name, such as Vertex AI, and compares Google's figures, day by day, with the cost it metered from your calls. A day matches when the drift is at most 2% of the invoice.

  1. In the Google Cloud console, open Billing › Billing export, then the BigQuery export tab. Under Standard usage cost, choose Edit settings, pick a project and a dataset, and Save. This needs a Billing Account Administrator. Google can take a few hours to write the first rows.
  2. Note your billing account ID from Billing › Account management. It looks like 012345-6789AB-CDEF01.
  3. In the export project, create a service account (IAM & Admin › Service accounts, Create service account), for example tokenmeter-billing. Give it BigQuery Job User on the export project and BigQuery Data Viewer on the export dataset.
  4. Optional, for Vertex AI token usage: give it Monitoring Viewer on each project that calls Vertex AI.
  5. Create a JSON key for it (Keys › Add key › Create new key, then JSON).
  6. In Token Meter, open Settings › Connections, choose Connect an account and pick Google. Fill in Cloud Billing account ID, Service-account JSON key (the whole file), BigQuery export project, BigQuery export dataset and AI service names from your billing export (comma-separated). Owners and admins can do this.
  7. Choose Connect and sync. Only the services you listed are imported.

To see the exact service names in your export, run this in BigQuery:

List the service names in your export
SELECT DISTINCT service.description
FROM `your-export-project.your_dataset.gcp_billing_export_v1_XXXXXX_XXXXXX_XXXXXX`
WHERE usage_start_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
ORDER BY 1

On save, Token Meter only checks that the JSON key is valid; access to the export dataset is first tested on the first sync. That sync imports the last 7 days, and the rest of the history, up to 90 days, follows in the background. Google syncs once a day. Days are UTC, today is never requested, and the newest days can lag while Google settles them. The connection only reads billing and usage data and is never used to make AI calls. BigQuery bills the daily query to the export project, which usually stays inside BigQuery's free monthly allowance.

Reconciliation runs at 07:00 UTC each day. For Google, a day is compared at the first run after a 3-day settlement delay. Each provider gets one status for the date range:

StatusMeaning
MatchedEvery reconciled day in the range is within 2%.
Drift detectedInvoice lines exist, and at least one day differs by more than 2%.
Not reconciled yetInvoice lines are imported, but no day in the range has been compared yet.
Awaiting invoiceNo invoice lines are imported for this provider in the range.

Google coverage is partial

With a Google connection, per-model usage comes only from Vertex AI Cloud Monitoring. Gemini API (AI Studio) traffic is not in metered estimates or reconciliation unless you send it through the SDK or the router. The Cloud Billing export may still include its spend.

On Analytics › Bill, once Google invoice lines exist for a range, the invoice is what counts toward your total AI spend, never the invoice plus metered cost. Gemini CLI is billed by Google, so with the invoice imported its spend is disclosed, not added again. Some Google line items are normally shown as unmapped spend, not tied to a model: cache storage, audio, image, video, tuned models and SKUs shared by two models. If you pay a negotiated rate, add a private rate under Settings › Connections, Advanced, so metered cost is priced the way you are billed. See AI Bill and billing imports, and tracking AI spend across providers if Google is one of several bills.

How are Gemini tokens counted, and where do costs go wrong?

Gemini puts cached tokens inside the prompt total but reports thinking tokens apart from the answer. So Token Meter subtracts the cache from the prompt and leaves the output as it is: input = promptTokenCount − cachedContentTokenCount, and candidatesTokenCount is used unchanged, with thoughtsTokenCount kept apart from it.

Every call becomes five separate counts: input, output, cache read, cache write and reasoning tokens. No token is in more than one, so cost is each count times its price, added up. A count Google didn't report stays empty rather than guessed. For Gemini, both cache and reasoning tokens are recorded.

Don't subtract thoughtsTokenCount

OpenAI counts reasoning inside its completion total, so the habit is to subtract it. Gemini's thoughtsTokenCount is already separate. Subtracting it from candidatesTokenCount counts too few output tokens, and reasoning-heavy traffic then looks cheaper than it is.

A made-up reply: a prompt total of 1,000 tokens with 600 cached, 900 candidate tokens and 600 thought tokens. Input is 400, cache read is 600, output stays 900 and the 600 thought tokens are counted on their own. Subtracting thoughts from candidates would record 300 output tokens instead of 900.

How sure is this? The Gemini rule is checked against Google's documentation. That the last chunk of a stream carries the final counts is seen in practice, but Google doesn't document it. A stream that stops before its final chunk is recorded as incomplete, with no token counts. With the Vercel AI SDK, versions 3 and 4 give only a prompt and a completion total, so cache and reasoning counts stay empty; the total cost is still right. Through the router, Gemini prompts are counted with an approximate tokenizer and padded by 20% for budget and limit checks, but the bill always uses Google's own usage.

How do you cap Gemini and Vertex AI spend?

Set a budget: a monthly spend limit on the whole workspace, or on one customer, feature, team, environment, provider or model, with alerts as spend climbs. Budgets are checked every 60 seconds against spend so far this calendar month (UTC), from every connected usage source.

  • Budgets. Thresholds default to 50%, 80% and 100%, each firing once a month, and a projection alert warns when the month's linear burn would end above the budget. Owners and admins create them under Governance › Budgets. On the Free plan you can set one advisory, workspace-wide budget; scoped budgets, router enforcement, alert rules and notification channels need a paid plan.
  • Enforced on the router. One workspace-wide budget can be enforced on router traffic: a call that doesn't fit gets 402 monthly_budget_exceeded. Your Vertex AI traffic reaches Token Meter through the SDK or the billing import, so a budget counts it and alerts on it but can't block it.
  • Per-request caps. Through the router, a request can carry max_cost_usd and is refused when its worst-case cost is above it.
  • Alert rules. A Threshold rule fires when spend for a scope, optionally limited to one provider and model, reaches an amount within the UTC day or month. A Provider spike rule fires when today's spend so far reaches a set multiple of yesterday's, scaled to the part of the day that has passed.

Alerts go to email, Slack or Discord channels set up under Settings › Notifications. See Governance and alerts.

What doesn't this cover?

  • Your own Vertex AI project can't be sent through the router; tag those calls with the SDK paths above.
  • Python's async Gemini client (client.aio.models) is not recorded yet.
  • The router has no batch or async support for Gemini.
  • Gemini calls that aren't metered have no customer or feature; at most they show in the imported Google totals.
  • Invoice figures are not live: Google syncs once a day, today is never imported, and the newest days can lag.

Start tracking Gemini costs

Create a workspace, add an ingest key, wrap your Gemini client and tag one call with a customer and feature. Then connect your Cloud Billing export to check the metered cost against Google's figures.