Skip to content

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

Open
ajbozarth wants to merge 2 commits intogenerative-computing:mainfrom
ajbozarth:feat/pricing-464
Open

feat: add pricing registry and cost metrics (#464)#882
ajbozarth wants to merge 2 commits intogenerative-computing:mainfrom
ajbozarth:feat/pricing-464

Conversation

@ajbozarth
Copy link
Copy Markdown
Contributor

@ajbozarth ajbozarth commented Apr 17, 2026

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.pyPricingRegistry 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 <ajbozart@us.ibm.com>
@ajbozarth
Copy link
Copy Markdown
Contributor 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
Comment thread mellea/telemetry/pricing.py Outdated
Comment thread mellea/telemetry/pricing.py Outdated
Comment thread mellea/telemetry/pricing.py Outdated
Comment thread mellea/telemetry/metrics_plugins.py Outdated
Comment thread docs/docs/evaluation-and-observability/metrics.md Outdated
Comment thread mellea/telemetry/builtin_pricing.json Outdated
- 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 <ajbozart@us.ibm.com>
@ajbozarth
Copy link
Copy Markdown
Contributor Author

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

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