DEV Community

Evan Lin for Google Developer Experts

Posted on • Originally published at evanlin.com on

[GCP Billing & Vertex AI] Solving Gemini Cost Allocation in a Single Project: Vertex AI Dynamic Billing Labels in Action

Pain Point: How to Accurately Allocate Gemini API Costs Within the Same Project?

When developing enterprise-level LLM services or operating multi-tenant platforms, the question most frequently asked by finance and operations teams is:

"We have many different business lines and LINE Bots connected within the same GCP project. Every day, the Gemini Key costs all appear under the Gemini API category. Is there a way for us to split the costs based on different Gemini Keys or different users?"

Direct answer to your question: In Google Cloud Billing reports, it is not possible to directly display costs "based on different API Key names." The smallest attribution dimensions for Google Cloud billing reports are "Project," "Service," and "SKU (Product Line Item)." The system does not treat individual API Key strings as independent billing items. For the billing system, whether you create 10 or 100 API Keys within the same project, they will all be lumped together as a single Gemini API total.


A Workaround: Vertex AI "Request Labels" to the Rescue

If architectural constraints force you to stay within the same project, the most recommended approach is: switch to Vertex AI calls and use "Request Labels."

If you are currently using a Google AI Studio API Key, it cannot pass billing labels within a single project. However, if you change your code to call the Vertex AI Gemini API (still within the same project), Vertex AI supports dynamically including custom labels with each request.

Principle and Workflow

When sending each request (e.g., calling generateContent), include specific metadata in the API Request:

{
  "contents": { ... },
  "labels": {
    "client_id": "info_helper",
    "api_key_group": "marketing_team"
  }
}

Enter fullscreen mode Exit fullscreen mode

These custom labels are passed directly to the GCP billing system. Later, when you go to the GCP Billing report and select your set label key (e.g., client_id) in "Group by," you can clearly see the costs for different labels (representing different services, clients, or users) within the same project!


Project Implementation: Full Adoption of the Labels Mechanism

To fulfill this requirement, we audited the current API call architecture of the LINE Bot project and performed the following refactoring.

1. Project API Call Audit

Through scanning, we found that the vast majority of calls in the project use Vertex AI (14 out of 17 clients use vertexai=True), with only a few exceptions:

  • Vertex AI calls: Including GitHub summaries, multiple Google Maps Grounding tools, text summarization, image analysis, speech-to-text, etc. (total of 11 files, 19 call points).
  • Gemini API Key calls: Live API in main.py, Batch service in batch_service.py, and TTS speech synthesis in tts_tool.py.

[!IMPORTANT] The labels parameter is only supported by Vertex AI. If this parameter is included under an API Key (vertexai=False), it will cause the SDK to throw an error. Therefore, we only modified the 11 files that use Vertex AI.

2. Implementation Method

For the google-genai Python SDK, we have two main modification scenarios:

Scenario A: Already contains GenerateContentConfig

If the original call already includes a Config, we just need to pass an additional labels={"client_id": "info_helper"} into the config:

# Before (e.g., loader/gh_tools.py)
response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents=prompt,
    config=types.GenerateContentConfig(
        temperature=0,
        max_output_tokens=2048,
    )
)

# After
response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents=prompt,
    config=types.GenerateContentConfig(
        temperature=0,
        max_output_tokens=2048,
        labels={"client_id": "info_helper"}, # Include billing label
    )
)

Enter fullscreen mode Exit fullscreen mode

Scenario B: No Config parameter

If the original call is very simple (e.g., searchtool.py or youtube_gcp.py), we need to proactively include a GenerateContentConfig containing labels:

# Before (e.g., loader/searchtool.py)
response = client.models.generate_content(
    model="gemini-3.1-flash-lite-preview",
    contents=prompt,
)

# After
response = client.models.generate_content(
    model="gemini-3.1-flash-lite-preview",
    contents=prompt,
    config=types.GenerateContentConfig(
        labels={"client_id": "info_helper"}, # Add config to include label
    ),
)

Enter fullscreen mode Exit fullscreen mode

3. List of Modified Files

We performed precise modifications on a total of 19 call points across the following 11 files, and used Python's AST module (ast.parse) and Flake8 for syntax and formatting checks before submission:

  1. agents/chat_agent.py: Modify _create_chat_config() to add labels to both general Q&A and Grounding conversations.
  2. loader/chat_session.py: Include labels in Chat session config.
  3. loader/gh_tools.py: GitHub summary API.
  4. loader/langtools.py: Text summarization, image JSON generation, social media post generation.
  5. loader/maps_grounding.py: Maps search API.
  6. loader/searchtool.py: Keyword extraction tool.
  7. loader/youtube_gcp.py: YouTube video understanding API.
  8. tools/audio_tool.py: Asynchronous speech-to-text.
  9. tools/maps_tool.py: 5 call points including nearby search, restaurant name extraction, batch and review search, etc.
  10. tools/summarizer.py: Text summarization and Agentic Vision image understanding.
  11. tools/youtube_tool.py: YouTube summary tool.

Pitfall Guide: Watch Out for SDK Module Import Issues

When refactoring calls without Config for youtube_gcp.py and youtube_tool.py, since these two files originally only used named imports for specific types:

from google.genai.types import HttpOptions, Part

Enter fullscreen mode Exit fullscreen mode

When we write types.GenerateContentConfig(...) in the code, the system throws a NameError: name 'types' is not defined error.

Solution: We need to correct the import statement and directly import GenerateContentConfig:

# Before
from google.genai.types import HttpOptions, Part

# After
from google.genai.types import HttpOptions, Part, GenerateContentConfig

Enter fullscreen mode Exit fullscreen mode

And use it directly in the call without the types. prefix:

config=GenerateContentConfig(labels={"client_id": "info_helper"})

Enter fullscreen mode Exit fullscreen mode

Summary and Next Steps

This modification successfully injected the client_id=info_helper label into all Vertex AI API calls within the LINE Bot project.

  1. Billing Delay: Please note that after we start including labels, GCP billing data usually has a 24 to 48-hour delay before taking effect.
  2. Configure in GCP Billing: After two days, you can go to the GCP Console -> Billing -> Reports. In the "Group by" section on the right, select Labels and enter our key client_id.
  3. Mission Accomplished: At this point, the report will draw info_helper as a separate billing row, perfectly solving the problem of separating project costs for reimbursement and statistics!

Top comments (0)