Skip to content

feat: add pricing registry and cost metrics (#464) - #882

Merged
ajbozarth merged 4 commits into
generative-computing:mainfrom
ajbozarth:feat/pricing-464
Apr 21, 2026
Merged

ajbozarth merged 4 commits into
generative-computing:mainfrom
ajbozarth:feat/pricing-464

Conversation

@ajbozarth

@ajbozarth ajbozarth commented Apr 17, 2026 •

Copy link
Copy Markdown
Member

Misc PR

Type of PR

  • Bug Fix
  • New Feature
  • Documentation
  • Other

Description

Adds estimated LLM request cost tracking as part of the telemetry epic (#443).

  • mellea/telemetry/builtin_pricing.json — bundled pricing data for current Anthropic and OpenAI models, loaded via importlib.resources so it works correctly in installed packages and wheels
  • mellea/telemetry/pricing.py — PricingRegistry class that merges built-in prices with optional custom overrides from MELLEA_PRICING_FILE; module-level compute_cost() convenience function
  • mellea/telemetry/metrics.py — new mellea.llm.cost.usd counter and record_cost() following the existing lazy-init pattern
  • mellea/telemetry/metrics_plugins.py — new CostMetricsPlugin (priority 53) that hooks generation_post_call and records cost when pricing data is available
  • docs/docs/evaluation-and-observability/metrics.md — new Cost metrics section covering the instrument, attributes, built-in pricing, and custom pricing file format

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code as added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Attribution

  • AI coding assistants used

@ajbozarth ajbozarth self-assigned this Apr 17, 2026
@github-actions github-actions Bot added the enhancement New feature or request label Apr 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

The PR description has been updated. Please fill out the template for your PR to be reviewed.

@ajbozarth
ajbozarth marked this pull request as ready for review April 17, 2026 22:11
@ajbozarth
ajbozarth requested a review from a team as a code owner April 17, 2026 22:11
@ajbozarth
ajbozarth requested review from nrfulton and psschwei April 17, 2026 22:11
Adds a PricingRegistry backed by builtin_pricing.json for OpenAI and
Anthropic models, a mellea.llm.cost.usd counter via record_cost(), and
a CostMetricsPlugin that records estimated cost on each generation call.
Custom pricing overrides are supported via MELLEA_PRICING_FILE.

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>
@ajbozarth

Copy link
Copy Markdown
Member Author

This is parallel work to #883 which adds the other remaining metrics telemetry and whichever merges second will need a manual merge to deal with the overlapping git diffs.

Comment thread mellea/telemetry/__init__.py Resolved
Comment thread mellea/telemetry/pricing.py Resolved
Comment thread mellea/telemetry/pricing.py Resolved
Comment thread mellea/telemetry/pricing.py Resolved
Comment thread mellea/telemetry/metrics_plugins.py Resolved
Comment thread mellea/telemetry/builtin_pricing.json Resolved
- Use lazy _get_registry() init to avoid eager module-level load
- Warn once per unknown model using _warned_models set
- Use .get() with defaults to avoid KeyError on malformed pricing JSON
- Fix CostMetricsPlugin docstring (cost is not skipped silently)
- Switch pricing format from per-1k to per-1m tokens to match provider docs
- Update tests and docs to reflect new per-1m format

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>
@ajbozarth

Copy link
Copy Markdown
Member Author

@psschwei I've addressed all you comments with either a response or a fix in the last commit

@psschwei psschwei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two minor questions around variable validation

Comment thread mellea/telemetry/pricing.py Resolved
Comment thread mellea/telemetry/metrics.py Resolved
…ing files

- Skip and warn on pricing entries with non-dict, non-numeric, or
  negative values at registry load time
- Add unit tests for all three invalid entry cases

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>

@psschwei psschwei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition — clean design that follows the existing telemetry patterns well. Two small notes below on the validation logic.

Comment thread mellea/telemetry/pricing.py Resolved
Comment thread mellea/telemetry/pricing.py Resolved
…ed keys

- Reject nan and inf values via math.isfinite()
- Skip entries with no recognised pricing keys rather than recording $0.00
- Add tests for nan, inf, and unrecognised key cases

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>

@psschwei psschwei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ajbozarth
ajbozarth added this pull request to the merge queue Apr 21, 2026
Merged via the queue into generative-computing:main with commit 0bbd27f Apr 21, 2026
7 checks passed
@ajbozarth
ajbozarth deleted the feat/pricing-464 branch April 21, 2026 17:25
akihikokuroda pushed a commit to akihikokuroda/mellea that referenced this pull request May 27, 2026
generative-computing#882)

* feat: add pricing registry and cost metrics (generative-computing#464)

Adds a PricingRegistry backed by builtin_pricing.json for OpenAI and
Anthropic models, a mellea.llm.cost.usd counter via record_cost(), and
a CostMetricsPlugin that records estimated cost on each generation call.
Custom pricing overrides are supported via MELLEA_PRICING_FILE.

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>

* fix: address PR review feedback on pricing registry

- Use lazy _get_registry() init to avoid eager module-level load
- Warn once per unknown model using _warned_models set
- Use .get() with defaults to avoid KeyError on malformed pricing JSON
- Fix CostMetricsPlugin docstring (cost is not skipped silently)
- Switch pricing format from per-1k to per-1m tokens to match provider docs
- Update tests and docs to reflect new per-1m format

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>

* fix: validate pricing entries and guard against malformed custom pricing files

- Skip and warn on pricing entries with non-dict, non-numeric, or
  negative values at registry load time
- Add unit tests for all three invalid entry cases

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>

* fix: harden pricing entry validation against nan, inf, and unrecognised keys

- Reject nan and inf values via math.isfinite()
- Skip entries with no recognised pricing keys rather than recording $0.00
- Add tests for nan, inf, and unrecognised key cases

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>

---------

Signed-off-by: Alex Bozarth <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create a pricing registry for major models and compute estimated cost per request based on token usage

2 participants