Calculate Google GenAI costs with genai-prices
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
- Domain
- backend, observability
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
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-priceswhen 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.costunset 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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pydantic/logfire
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Docs: update the Playground cost estimate note for Gateway custom pricesPossibly taken @henricop claimed this 13 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
`logfire clean --logs` fails on Windows with `PermissionError` and leaves the credentials file in placePossibly taken @AdityaSreevatsaK claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
Maintainers usually reply within 1 day
-
`force_flush()` deadlocks when a span exporter reads `get_tracer_provider().resource`Possibly taken @prassu017 claimed this 2 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 35/100
Maintainers usually reply within 1 day
All issues in pydantic/logfire
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
OCA/account-invoicing#2529 ·
Maintainers usually reply within 1 day
-
area/ci
Difficulty 1/5 Under an hour Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
ImageGen generate-batch aborts valid JSONL jobs containing a Unicode line separator in poster textOpenbug imagen skills
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Email validation accepts a trailing newlinePossibly taken @marcello-pichini claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day