Calculate Google GenAI costs with genai-prices

Open
#2,806 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
56/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
python

Research direction

Start by reading the Google GenAI instrumentation in logfire.instrument_google_genai() and follow how gateway-reported costs and token usage are recorded for streamed and non-streamed sync and async calls. Check the existing tests for those call paths and the genai-prices API; done means local estimates populate operation.cost only when no valid gateway cost is available, without double-counting or changing responses or token usage.

Written by the indexing model from the issue text.

Description

Feature Request
Description

Add cost estimation with genai-prices to logfire.instrument_google_genai() so Google GenAI calls record operation.cost when no AI Gateway cost is available. Storing the cost on the span makes it available in SQL queries, rather than only as an estimate displayed by the Logfire UI.

Expected behavior:

  • Prefer a valid gateway-reported cost, including 0.
  • Otherwise, use genai-prices when installed to calculate the cost from the model and usage, including thinking tokens and cached tokens.
  • Support streamed and non-streamed calls, both sync and async, without double-counting streamed usage.
  • Leave operation.cost unset when pricing is unavailable, while preserving the response and token usage.

Follow-up to #2702, which adds gateway-reported costs for Google GenAI. Local cost estimation is outside that PR's scope.

Dominant language
Python
Stars
4.5k
Forks
365
Avg merge
1d 8h
Merged PRs (30d)
153

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pydantic/logfire

All issues in pydantic/logfire

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.