Skip to content

[feat] Cost estimation across providers, models, and sessions #388

Closed

Description

@Sinity

Problem

Polylogue archives enough semantic structure to answer not only "what happened" but also "what did this likely cost." Right now there is no first-class cost model across providers and models, so cost analysis is left to ad hoc scripts and rough mental arithmetic.

That leaves several important questions unsupported:

  • what did a session likely cost across input, output, cache, and tool phases?
  • which providers or models are cheapest or most expensive for a given workload shape?
  • how much budget did a day, week, or task cluster likely consume?
  • what confidence should we have in an estimate when token or pricing data is partial?

Ambitiously strengthened scope

Implement a canonical cost-estimation substrate and expose it through archive product surfaces, not as a CLI-only calculator.

Required outcomes:

  • normalize provider/model identity for pricing lookup across archived conversations and product rows
  • define typed pricing, usage, estimate, uncertainty, and provenance contracts that can represent at least:
    • input token price
    • output token price
    • cache read / cache write prices where a provider exposes them
    • provider-supplied exact costs when archives contain them
    • currency and pricing provenance/effective date
    • missing-token and missing-price reasons without false precision
  • compute estimated costs at useful semantic levels:
    • message
    • conversation/session
    • aggregate rollups by provider, model, and time window
  • make uncertainty explicit:
    • exact when provider/archive cost data is present
    • priced when token counts and catalog prices are known
    • partial when only some messages can be priced
    • unavailable when the archive lacks enough usable data
  • expose at least one user-facing inspection path in CLI and one shared machine-facing path in facade/MCP or product APIs
  • make the implementation intentionally compatible with later work-thread/task rollups by keeping the session estimate records composable

Design requirements

  • Cost semantics must live in shared substrate/product logic, not in a formatter.
  • Incomplete data must remain explicit. Do not silently emit false precision when token counts or historical prices are missing.
  • Historical pricing changes need a real provenance story. If exact price at event time is unknown, that fact should be preserved in the estimate.
  • Prefer reusing a sensible pricing/model catalog library or upstream source where that materially reduces maintenance burden, but keep the archive-side contract typed and provider-agnostic.
  • The implementation should compose cleanly with later work on session clustering, summaries, and operator analytics.
  • The first implementation may use a curated LiteLLM-compatible seed catalog rather than vendoring the full LiteLLM runtime, but it must document that as provenance and avoid pretending the seed is a live historical price oracle.

Acceptance criteria

  • Polylogue can estimate costs for archived sessions across multiple providers and models using one shared contract.
  • Estimates carry provenance and confidence metadata rather than just a naked float.
  • Users can inspect cost rollups by provider/model and at least one per-session view.
  • CLI JSON and MCP/facade product surfaces expose the same typed payload shape.
  • Tests cover exact archive-provided cost, missing-token cases, missing-price cases, provider/model normalization, and at least one mixed-provider aggregate.
  • The chosen pricing source/library and its limits are documented in the issue or implementation notes.

Useful references

Activity

  1. Sinity commented on Apr 24, 2026

    @Sinity
    OwnerAuthor

    Execution start for the strengthened #388 slice.

    Decision: use Polylogue-owned typed estimate models and a small curated LiteLLM-shaped seed catalog for the first implementation, rather than adding the full LiteLLM runtime dependency. The reason is maintenance and trust boundary clarity: Polylogue needs stable archive-side semantics with explicit provenance; a bundled seed can say exactly when it is catalog-derived and incomplete, while provider/archive-reported exact costs still win when present.

    Initial implementation target:

    • substrate: typed pricing/usage/estimate models and deterministic provider/model normalization
    • products: session-level cost estimates plus provider/model rollups
    • surfaces: registry-backed CLI, facade, and MCP product access
    • tests: exact cost, priced estimate, partial/unavailable uncertainty, normalization, and mixed-provider rollup
  2. added a commit that references this issue on Apr 24, 2026
  3. Sinity commented on Apr 24, 2026

    @Sinity
    OwnerAuthor

    Execution log for the expanded #388 slice:

    • PR: feat: add cost estimation products #394
    • Merge commit: f6aad25
    • Scope delivered: typed cost-estimation substrate plus shared product surfaces for session-level estimates and provider/model rollups.
    • Contract details: estimates now carry exact / priced / partial / unavailable status, confidence, usage, components, price provenance, missing reasons, and normalized model identity.
    • Pricing source decision: first implementation uses a curated LiteLLM-shaped seed catalog with explicit provenance/effective date instead of depending on the full LiteLLM runtime. Provider/archive-reported exact costs take precedence.
    • Surfaces: polylogue products costs, polylogue products cost-rollups, facade/sync facade product methods, and MCP registry-backed tools.
    • Verification run locally:
      • focused pricing/product tests: 9 passed in 10.91s
      • targeted strict mypy over touched implementation/tests: passed
      • devtools render-all --check: passed
      • devtools verify: passed
      • pre-push quick verification: passed

    CI was still hosted/in-progress at merge time; merge was based on local full verification and successful push hooks per current quota constraint.

  4. added a commit that references this issue on May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions