Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

5.2 KB
Raw Blame
"""Token-usage accumulator shared across LLM calls within a single turn."""

from __future__ import annotations

import logging
from typing import Any

from deeptutor.services.llm.usage_frame import token_counts

logger = logging.getLogger(__name__)


class UsageTracker:
    """Accumulate prompt/completion tokens across many streaming LLM calls.

    Two ingestion paths:

    * :meth:`add_from_response` — read OpenAI ``CompletionUsage`` (or the
      streaming ``usage`` chunk) when the provider returns it.
    * :meth:`add_estimated` — fall back to a coarse ``chars / 3.5`` estimate
      for providers that don't emit ``usage`` (used by chat's answer-now path).

    Construct with ``model=<name>`` so :meth:`summary` can resolve a
    ``total_cost_usd`` via the pricing table in ``deeptutor.logging.stats``.
    """

    def __init__(self, *, model: str | None = None) -> None:
        self.prompt_tokens: int = 0
        self.completion_tokens: int = 0
        self.total_tokens: int = 0
        self.calls: int = 0
        self.model: str | None = model

    def add_from_response(self, response_or_usage: Any) -> None:
        counts = token_counts(getattr(response_or_usage, "usage", None) or response_or_usage)
        if not counts:
            return
        self.prompt_tokens += counts["prompt_tokens"]
        self.completion_tokens += counts["completion_tokens"]
        self.total_tokens += counts["total_tokens"]
        self.calls += 1

    def add_estimated(self, *, input_chars: int, output_chars: int) -> None:
        est_input = int(input_chars / 3.5)
        est_output = int(output_chars / 3.5)
        self.prompt_tokens += est_input
        self.completion_tokens += est_output
        self.total_tokens += est_input + est_output
        self.calls += 1

    def add_usage(
        self,
        *,
        agent_name: str = "",
        stage: str = "",
        model: str = "",
        system_prompt: str = "",
        user_prompt: str = "",
        response_text: str = "",
    ) -> None:
        """Adapter for :class:`~deeptutor.agents.base_agent.BaseAgent`.

        ``BaseAgent._track_tokens`` looks for an external tracker exposing
        ``add_usage(...)``; this method lets a :class:`UsageTracker` be
        passed as the ``token_tracker`` constructor argument so a
        capability pipeline can aggregate cost across all of its
        BaseAgent-derived sub-agents in one place.

        We fall back to a character-based estimate because BaseAgent
        only hands us the prompt/response text (the raw provider usage
        object is not available at that layer).
        """
        if model and not self.model:
            self.model = model
        input_chars = len(system_prompt or "") + len(user_prompt or "")
        output_chars = len(response_text or "")
        if input_chars or output_chars:
            self.add_estimated(input_chars=input_chars, output_chars=output_chars)

    def summary(self) -> dict[str, Any] | None:
        if self.calls == 0:
            return None
        cost_usd = 0.0
        if self.model:
            # Local import keeps ``core.agentic`` import-light at module load.
            from deeptutor.logging.stats.llm_stats import get_pricing

            pricing = get_pricing(self.model)
            cost_usd = (self.prompt_tokens / 1000.0) * pricing.get("input", 0.0) + (
                self.completion_tokens / 1000.0
            ) * pricing.get("output", 0.0)
        return {
            "total_cost_usd": cost_usd,
            "total_tokens": self.total_tokens,
            "total_calls": self.calls,
            "prompt_tokens": self.prompt_tokens,
            "completion_tokens": self.completion_tokens,
        }


def message_content_chars(message: dict[str, Any]) -> int:
    """Best-effort character count of one chat message, for usage estimates."""
    content = message.get("content")
    if isinstance(content, str):
        return len(content)
    if isinstance(content, list):
        total = 0
        for part in content:
            if isinstance(part, dict):
                total += len(str(part.get("text") or ""))
            elif isinstance(part, str):
                total += len(part)
        return total
    if content is None:
        return 0
    return len(str(content))


def record_streamed_usage(
    tracker: UsageTracker | None,
    usage_frame: Any,
    *,
    input_chars: int = 0,
    output_chars: int = 0,
) -> None:
    """Record one completed stream's usage exactly once.

    Providers (esp. Gemini's OpenAI-compat API) may attach ``usage`` to more
    than one stream chunk — callers keep only the *latest* frame and hand it
    here after the stream ends, so ``total_calls``/tokens are never inflated
    N× for a single completion. When no frame arrived, falls back to the
    coarse char-based estimate (pass zero chars to skip the fallback).
    Accounting must never break a completed stream, so failures are
    swallowed and debug-logged.
    """
    if tracker is None:
        return
    try:
        if usage_frame is not None:
            tracker.add_from_response(usage_frame)
        elif input_chars or output_chars:
            tracker.add_estimated(input_chars=input_chars, output_chars=output_chars)
    except Exception:
        logger.debug("stream usage recording failed", exc_info=True)