> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mellea.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# mellea.telemetry.pricing

> LLM pricing via litellm's pricing API.

export const SidebarFix = () => <script dangerouslySetInnerHTML={{
  __html: `
        (function () {
          const INTERVAL_MS = 500;

          const upgradeSidebar = () => {
            const links = document.querySelectorAll('a[href^="#"]');

            links.forEach((link) => {
              if (link.dataset.badged === "true") return;

              const rawText = (link.textContent || "").trim();

              // ========== FUNC ==========
              if (rawText.startsWith("FUNC ")) {
                const label = rawText.replace(/^FUNC\\s+/, "").trim();

                while (link.firstChild) link.removeChild(link.firstChild);

                // 👉 Make the whole link a single flex row & prevent wrapping
                link.style.display = "flex";
                link.style.alignItems = "center";
                link.style.whiteSpace = "nowrap";
                link.style.columnGap = "0.5rem";

                const badge = document.createElement("span");
                badge.style.marginRight = "0.5rem";
                badge.style.display = "inline-flex";
                badge.style.alignItems = "center";
                badge.style.borderRadius = "9999px";
                badge.style.padding = "0rem 0.6rem";
                badge.style.fontSize = "0.5rem";
                badge.style.fontWeight = "700";
                badge.style.letterSpacing = "0.05em";
                badge.style.backgroundColor = "rgba(48, 100, 227, 0.20)";
                badge.style.color = "#1D4ED8";

                badge.textContent = "FUNC";

                link.appendChild(badge);
                link.appendChild(document.createTextNode(label));
                link.dataset.badged = "true";
                return;
              }

              // ========== CLASS ==========
              if (rawText.startsWith("CLASS ")) {
                const label = rawText.replace(/^CLASS\\s+/, "").trim();

                while (link.firstChild) link.removeChild(link.firstChild);

                // 👉 Same flex / nowrap treatment for class links
                link.style.display = "flex";
                link.style.alignItems = "center";
                link.style.whiteSpace = "nowrap";
                link.style.columnGap = "0.5rem";

                const badge = document.createElement("span");
                badge.style.marginRight = "0.5rem";
                badge.style.display = "inline-flex";
                badge.style.alignItems = "center";
                badge.style.borderRadius = "9999px";
                badge.style.padding = "0rem 0.6rem";
                badge.style.fontSize = "0.5rem";
                badge.style.fontWeight = "700";
                badge.style.letterSpacing = "0.05em";
                badge.style.backgroundColor = "rgba(74, 222, 128, 0.20)";
                badge.style.color = "#15803D";

                badge.textContent = "CLASS";

                link.appendChild(badge);
                link.appendChild(document.createTextNode(label));
                link.dataset.badged = "true";
                return;
              }
            });
          };

          upgradeSidebar();
          setInterval(upgradeSidebar, INTERVAL_MS);
        })();
      `
}} />;

<SidebarFix />

LLM pricing via litellm's pricing API.

Pricing metrics require the litellm package (`mellea[litellm]`). Pricing is
auto-enabled when litellm is installed and can be explicitly controlled via the
`MELLEA_PRICING_ENABLED` environment variable.

`MELLEA_PRICING_ENABLED` tri-state:

* `"true"`  + litellm installed  → enabled
* `"true"`  + litellm absent     → warning, disabled
* `"false"` (any)                → disabled (silent)
* unset       + litellm installed  → enabled (auto)
* unset       + litellm absent     → disabled (silent)

Pricing is only active when `MELLEA_METRICS_ENABLED` is also set.

Custom pricing:
Set `MELLEA_PRICING_FILE` to a JSON file using litellm's native per-token
schema. Minimal entries with only cost fields are supported::

\{
"my-model": \{
"input\_cost\_per\_token": 0.000003,
"output\_cost\_per\_token": 0.000015
}
}

Optional cache fields: `cache_read_input_token_cost`,
`cache_creation_input_token_cost`.

Environment variables:

* MELLEA\_PRICING\_ENABLED: Tri-state pricing flag (true/false/unset).
* MELLEA\_PRICING\_FILE: Path to a JSON file with custom model pricing.

## Functions

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#3064E3]/20 text-[#1D4ED8]">FUNC</span> `compute_cost` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/telemetry/pricing.py#L100" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
compute_cost(model: str, provider: str | None, prompt_tokens: int | None, completion_tokens: int | None, cached_tokens: int | None = None, cache_creation_tokens: int | None = None) -> float | None
```

Estimate request cost in USD using litellm's pricing data.

**Args:**

* `model`: Model identifier (e.g. `"gpt-5.4"`, `"claude-sonnet-4-6"`).
* `provider`: Provider name from the backend (e.g. `"openai"`, `"watsonx"`).
  Passed to litellm as `custom_llm_provider` to aid model resolution —
  e.g. `"watsonx"` causes litellm to try `watsonx/ibm/granite-4-h-small`.
* `prompt_tokens`: Total prompt tokens including any cached tokens, or `None`.
* `completion_tokens`: Number of completion tokens, or `None`.
* `cached_tokens`: Tokens served from prompt cache, or `None`.
* `cache_creation_tokens`: Tokens written to prompt cache, or `None`.

**Returns:**

* Estimated cost in USD, or `None` if pricing is disabled or no pricing
* data exists for the model.

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#3064E3]/20 text-[#1D4ED8]">FUNC</span> `is_pricing_enabled` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/telemetry/pricing.py#L146" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
is_pricing_enabled() -> bool
```

Return True if pricing metrics are enabled.

**Returns:**

* True if litellm is available and pricing is not explicitly disabled.

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />
