Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

9.4 KB
Raw Blame
"""
Cost Calculator

Calculates LLM usage costs based on token usage and pricing data.
Integrates with pricing fetcher and cache systems.
"""

from typing import Any, Dict, List, Optional

from loguru import logger

from .pricing_cache import PricingCache
from .pricing_fetcher import PricingFetcher


class CostCalculator:
    """Calculates LLM usage costs."""

    def __init__(self, cache_dir: Optional[str] = None):
        self.cache = PricingCache(cache_dir)
        self.pricing_fetcher = None

    async def __aenter__(self):
        self.pricing_fetcher = PricingFetcher()
        await self.pricing_fetcher.__aenter__()
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        if self.pricing_fetcher:
            await self.pricing_fetcher.__aexit__(exc_type, exc_val, exc_tb)

    async def get_model_pricing(
        self, model_name: str, provider: str = None
    ) -> Dict[str, float]:
        """Get pricing for a model and provider (cached or fetched)."""
        # Create cache key that includes provider
        cache_key = f"{provider}:{model_name}" if provider else model_name

        # Try cache first
        cached_pricing = self.cache.get(f"model:{cache_key}")
        if cached_pricing:
            return cached_pricing

        # Fetch from API
        if self.pricing_fetcher:
            pricing = await self.pricing_fetcher.get_model_pricing(
                model_name, provider
            )
            if pricing:
                self.cache.set(f"model:{cache_key}", pricing)
                return pricing

        # No pricing found
        logger.warning(
            f"No pricing found for {model_name} (provider: {provider})"
        )
        return None

    async def calculate_cost(
        self,
        model_name: str,
        prompt_tokens: int,
        completion_tokens: int,
        provider: str = None,
    ) -> Dict[str, float]:
        """
        Calculate cost for a single LLM call.

        Returns:
            Dict with prompt_cost, completion_cost, total_cost
        """
        pricing = await self.get_model_pricing(model_name, provider)

        # If no pricing found, return zero cost
        if pricing is None:
            return {
                "prompt_cost": 0.0,
                "completion_cost": 0.0,
                "total_cost": 0.0,
                "pricing_used": None,
                "error": "No pricing data available for this model",
            }

        # Convert tokens to thousands for pricing calculation
        prompt_cost = (prompt_tokens / 1000) * pricing["prompt"]
        completion_cost = (completion_tokens / 1000) * pricing["completion"]
        total_cost = prompt_cost + completion_cost

        return {
            "prompt_cost": round(prompt_cost, 6),
            "completion_cost": round(completion_cost, 6),
            "total_cost": round(total_cost, 6),
            "pricing_used": pricing,
        }

    async def calculate_batch_costs(
        self, usage_records: List[Dict[str, Any]]
    ) -> List[Dict[str, Any]]:
        """
        Calculate costs for multiple usage records.

        Expected record format:
        {
            "model_name": str,
            "provider": str (optional),
            "prompt_tokens": int,
            "completion_tokens": int,
            "research_id": int (optional),
            "timestamp": datetime (optional)
        }
        """
        results = []

        for record in usage_records:
            try:
                cost_data = await self.calculate_cost(
                    record["model_name"],
                    record["prompt_tokens"],
                    record["completion_tokens"],
                    record.get("provider"),
                )

                result = {**record, **cost_data}
                results.append(result)

            except Exception:
                logger.exception(
                    f"Failed to calculate cost for record {record}"
                )
                # Add record with zero cost on error
                results.append(
                    {
                        **record,
                        "prompt_cost": 0.0,
                        "completion_cost": 0.0,
                        "total_cost": 0.0,
                        "pricing_used": None,
                        "error": "Cost calculation failed",
                    }
                )

        return results

    def calculate_cost_sync(
        self,
        model_name: str,
        prompt_tokens: int,
        completion_tokens: int,
        provider: Optional[str] = None,
    ) -> Dict[str, float]:
        """
        Synchronous cost calculation using provider-scoped cache entries and
        the same static fallback as the async path. No network I/O is used.
        """
        # Use cached pricing only
        cache_key = f"{provider}:{model_name}" if provider else model_name
        pricing = self.cache.get_model_pricing(cache_key)
        if not pricing:
            fetcher = PricingFetcher()
            pricing = fetcher.get_static_model_pricing(model_name, provider)

        # If no pricing found, return zero cost
        if not pricing:
            return {
                "prompt_cost": 0.0,
                "completion_cost": 0.0,
                "total_cost": 0.0,
                "pricing_used": None,
                "error": "No pricing data available for this model",
            }

        prompt_cost = (prompt_tokens / 1000) * pricing["prompt"]
        completion_cost = (completion_tokens / 1000) * pricing["completion"]
        total_cost = prompt_cost + completion_cost

        return {
            "prompt_cost": round(prompt_cost, 6),
            "completion_cost": round(completion_cost, 6),
            "total_cost": round(total_cost, 6),
            "pricing_used": pricing,
        }

    async def get_research_cost_summary(
        self, usage_records: List[Dict[str, Any]]
    ) -> Dict[str, Any]:
        """Get cost summary for research session(s).

        ``model_breakdown`` retains totals grouped by model name.
        ``provider_breakdown`` lists each distinct model/provider pair;
        separate fields keep names containing separators unambiguous and
        allow the summary to be serialized as JSON.

        Cost totals cover only successfully priced usage; the existing
        averages divide those totals by all calls/tokens. ``unpriced_calls``
        and ``unpriced_tokens`` identify incomplete totals. A known zero
        price (for example, Ollama) is fully priced.
        """
        costs = await self.calculate_batch_costs(usage_records)
        return summarize_costs(costs)


def summarize_costs(costs: List[Dict[str, Any]]) -> Dict[str, Any]:
    """Aggregate calculated usage records from either pricing path."""
    total_cost = sum(c["total_cost"] for c in costs)
    total_prompt_cost = sum(c["prompt_cost"] for c in costs)
    total_completion_cost = sum(c["completion_cost"] for c in costs)

    total_prompt_tokens = sum(c["prompt_tokens"] for c in costs)
    total_completion_tokens = sum(c["completion_tokens"] for c in costs)
    total_tokens = total_prompt_tokens + total_completion_tokens

    # Keep the existing model totals and a separate provider breakdown.
    model_costs = {}
    provider_costs = {}
    unpriced_calls = 0
    unpriced_tokens = 0
    for cost in costs:
        model = cost["model_name"]
        if model not in model_costs:
            model_costs[model] = {
                "total_cost": 0.0,
                "prompt_tokens": 0,
                "completion_tokens": 0,
                "calls": 0,
            }

        provider = cost.get("provider")
        key = (model, provider)
        if key not in provider_costs:
            provider_costs[key] = {
                "model_name": model,
                "provider": provider,
                "total_cost": 0.0,
                "prompt_tokens": 0,
                "completion_tokens": 0,
                "calls": 0,
                "unpriced_calls": 0,
                "unpriced_tokens": 0,
            }

        for bucket in (model_costs[model], provider_costs[key]):
            bucket["total_cost"] += cost["total_cost"]
            bucket["prompt_tokens"] += cost["prompt_tokens"]
            bucket["completion_tokens"] += cost["completion_tokens"]
            bucket["calls"] += 1

        if cost.get("pricing_used") is None:
            tokens = cost["prompt_tokens"] + cost["completion_tokens"]
            unpriced_calls += 1
            unpriced_tokens += tokens
            provider_costs[key]["unpriced_calls"] += 1
            provider_costs[key]["unpriced_tokens"] += tokens

    for bucket in provider_costs.values():
        bucket["total_cost"] = round(bucket["total_cost"], 6)

    return {
        "total_cost": round(total_cost, 6),
        "prompt_cost": round(total_prompt_cost, 6),
        "completion_cost": round(total_completion_cost, 6),
        "total_tokens": total_tokens,
        "prompt_tokens": total_prompt_tokens,
        "completion_tokens": total_completion_tokens,
        "total_calls": len(costs),
        "model_breakdown": model_costs,
        "provider_breakdown": list(provider_costs.values()),
        "pricing_complete": unpriced_calls == 0,
        "unpriced_calls": unpriced_calls,
        "unpriced_tokens": unpriced_tokens,
        "avg_cost_per_call": round(total_cost / len(costs), 6)
        if costs
        else 0.0,
        "cost_per_token": (
            round(total_cost / total_tokens, 8) if total_tokens > 0 else 0.0
        ),
    }