Skip to content

Add token usage metrics to InstrumentedModel #1898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 9, 2025
Merged

Conversation

alexmojaki
Copy link
Contributor

@alexmojaki alexmojaki commented Jun 3, 2025

Implements https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-metrics/#metric-gen_aiclienttokenusage

Example query in logfire:

select sum(histogram_sum)                 as "Total tokens",
       attributes ->> 'gen_ai.token.type' as "Token type"
from metrics
where metric_name = 'gen_ai.client.token.usage'
group by "Token type"
Screenshot 2025-06-06 at 14 51 43

Not in a hurry to add docs since this doesn't feel user friendly, would rather point to a managed dashboard template or metrics being aggregated in spans.

Copy link

github-actions bot commented Jun 3, 2025

Docs Preview

commit: 22b4cb5
Preview URL: https://8a8b4f36-pydantic-ai-previews.pydantic.workers.dev

@alexmojaki alexmojaki marked this pull request as ready for review June 6, 2025 12:58
@alexmojaki alexmojaki requested review from DouweM and Kludex June 6, 2025 12:58
@alexmojaki alexmojaki merged commit 4f257bd into main Jun 9, 2025
32 of 34 checks passed
@alexmojaki alexmojaki deleted the alex/usage-metrics branch June 9, 2025 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants