Metadata-Version: 2.4
Name: metergraph-core
Version: 0.8.29
Summary: Open, effective-dated LLM pricing catalog and deterministic cost calculator for popular models across major providers and gateways, including OpenAI, Anthropic, Google Gemini, AWS Bedrock, OpenRouter, Vercel AI Gateway, and more.
Author: Metergraph
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/metergraph/metergraph
Project-URL: Documentation, https://github.com/metergraph/metergraph/tree/main/core#readme
Project-URL: Source, https://github.com/metergraph/metergraph/tree/main/core
Project-URL: Issues, https://github.com/metergraph/metergraph/issues
Project-URL: Release notes, https://github.com/metergraph/metergraph/releases
Keywords: llm,pricing,cost,token-pricing,model-catalog,llm-pricing,model-pricing,token-cost,cost-calculation,openai,anthropic,gemini,bedrock,openrouter,vercel-ai-gateway,finops
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: pyyaml>=6.0
Provides-Extra: dev
Requires-Dist: build>=1.2; extra == "dev"
Requires-Dist: pytest>=8; extra == "dev"

# metergraph-core

Calculate the cost of an LLM call from its model, provider, tokens, region, and
date. `metergraph-core` ships an open, effective-dated model pricing catalog and
a deterministic Python cost engine.

## Install

```bash
python -m pip install metergraph-core
```

## Usage

Use `calculate_cost()` when you already have token counts:

```python
from metergraph_core import ModelUsage, load_catalog

catalog = load_catalog(region="global")
result = catalog.calculate_cost(
    model="gpt-5.4-mini",
    provider="openai",
    at="2026-08-17T00:00:00Z",
    usage=ModelUsage(input_tokens=1000, output_tokens=200),
)
print(result.status, result.cost, result.currency)
```

The result identifies the applied model and price. Check `status` before using
its cost as a complete total: missing usage or unsupported rates can make a
result `partial` or `unpriced`.

## Choose the API for your input

| You have… | Start with… |
|---|---|
| Token counts and a model | `catalog.calculate_cost(..., usage=ModelUsage(...))` |
| A provider SDK response | `price_call_from_response(provider, response)` for token costs and supported retrieval fees |
| A normalized SDK or relay record | `price_call_from_record(record)` |
| Request handlers that need ready-to-use pricing data | `Catalogs`, with one instance shared across handlers |
| Model identities, executable routes, or candidate lists | `load_model_registry()` |

For price lookup, token-only response pricing, missing-data rules, gateway
billing evidence, and model metadata, see the
[pricing and model reference](https://github.com/metergraph/metergraph/blob/main/core/docs/usage.md).

## Serving real-time requests

Prepare catalogs once before accepting traffic. Request handlers then call
`catalogs.current()` and use the same pricing methods shown above. Choose one
of the following lifecycle options.

### Let the catalog own its refresh thread

```python
from metergraph_core import Catalogs

catalogs = Catalogs(region="global", refresh_seconds=300)
catalogs.start()
try:
    print(catalogs.current().version)
finally:
    catalogs.stop()
```

### Use your own scheduler or threads

`prepare()` and `refresh()` are synchronous and create no threads. Prepare
before serving; run refreshes outside request handlers. This example schedules
one refresh on an executor owned by the application:

```python
from concurrent.futures import ThreadPoolExecutor
from metergraph_core import Catalogs

catalogs = Catalogs(region="global")
catalogs.prepare()
try:
    with ThreadPoolExecutor(max_workers=1) as executor:
        refresh = executor.submit(catalogs.refresh)
        print(catalogs.current().version)
        refresh.result()
finally:
    catalogs.stop()
```

Both options enable prepared-only reads. Without either option, `current()`
loads and refreshes on demand. The default source is the bundled catalog;
custom loaders can supply changing data. See the
[lifecycle contract](https://github.com/metergraph/metergraph/blob/main/core/docs/usage.md#catalog-lifecycle)
for refresh failures, custom sources, read-only values, and shutdown behavior.

## Reference and contributions

- [Pricing and model reference](https://github.com/metergraph/metergraph/blob/main/core/docs/usage.md):
  loading, regional and dated prices, provider responses, retrieval fees,
  model families, routes, training policy, billing evidence, and reliability.
- [Catalog maintenance](https://github.com/metergraph/metergraph/blob/main/core/docs/catalog-maintenance.md):
  data files and versions, official sources, reviewed deltas, refresh tooling,
  and provider contributions.
- [Public API exports](https://github.com/metergraph/metergraph/blob/main/core/docs/usage.md#public-api).

Core contains the shared datasets and pricing rules for
[Metergraph](https://github.com/metergraph/metergraph). Core does not own HTTP routes, database access,
authentication, ingestion, or dashboards; these belong to the applications using it.
