gptme-usage

v0.1.0 Cross-backend usage, cost, and quota surface for gptme agents (model registry, cost math, quota checks) packages/gptme-usage View on GitHub

gptme-usage

Cost estimation and model/quota registry for agents that run on several LLM backends (Claude Code, Codex, gptme via OpenRouter or local models): turn a session's token counts — or just its duration — into an estimated USD cost, and map each model to the quota pool it draws from.

Status: alpha, library only (no CLI yet). The package ships the generic math and an empty registry; each agent supplies its own prices, throughput and routes in a TOML file.

When to use it

Related packages:

Install

pip install "git+https://github.com/gptme/gptme-contrib#subdirectory=packages/gptme-usage"

Or, from a gptme-contrib checkout: uv sync --package gptme-usage.

The only runtime dependency is tomli on Python 3.10.

Configure

Copy harness-quota.example.toml to ~/.config/gptme/harness-quota.toml and replace the placeholder values. The default path is fixed ($XDG_CONFIG_HOME and gptme's own config are not consulted); pass load_quota_config(path=...) to read another file.

claude_plan_tier = "max-5x"      # optional

[prices.claude-code]             # USD per 1M tokens: [input, output]
opus   = [5.0, 25.0]
sonnet = [3.0, 15.0]

[cache_read_prices.claude-code]  # optional absolute USD per 1M cached tokens
opus = 0.50                     # overrides the provider multiplier; 0 means free

[tps.claude-code]                # tokens/second, for duration-based estimates
opus = 18000

[quota_sources]                  # gptme model -> "openrouter" | "chatgpt" | "local"
"example-openrouter-model" = "openrouter"

[model_routes]                   # short name -> provider-qualified gptme model
"example-openrouter-model" = "openrouter/vendor/example-model@vendor"

[openrouter_key_contexts]
default = "autonomous"

A missing or unreadable file (or Python 3.10 without tomli) yields an empty config; cost functions then return None instead of guessing. A non-empty table in your config replaces the module default table rather than merging with it. Use merge_with_module_defaults() if you want merge semantics.

Optional [cache_read_prices.<harness>] entries use the same canonical model keys as [prices.<harness>]. They override provider cache-read multipliers, including with an explicit zero. Omitted entries retain the provider heuristic (or regular-input pricing where no heuristic is known); negative/non-finite rates are ignored. Cache creation pricing and each harness's token-counter conventions are unchanged. These are API-equivalent estimates, not invoices or subscription marginal costs; a session's reported cost remains authoritative.

Quickstart

from gptme_usage import load_quota_config, estimate_session_cost, estimate_tokens_from_duration

cfg = load_quota_config()  # ~/.config/gptme/harness-quota.toml, or empty

cost = estimate_session_cost(
    "claude-code", "opus",
    input_tokens=20_000, output_tokens=5_000, cache_read_tokens=1_000_000,
    config=cfg,
)  # float USD, or None if the model has no configured price

tokens = estimate_tokens_from_duration("claude-code", "opus", 600, config=cfg)

API

Exported from gptme_usage:

Name Purpose
load_quota_config(path=None) Load HarnessQuotaConfig from TOML (empty config on any failure)
HarnessQuotaConfig Dataclass: price_table, tps_table, quota_sources, model_routes, openrouter_key_contexts, claude_plan_tier, cache_read_price_table
estimate_session_cost(harness, model, *, input_tokens, output_tokens, cache_creation_tokens, cache_read_tokens, token_count, config) Cache-aware USD estimate
estimate_tokens_from_duration(harness, model, duration_seconds, *, config) Tokens from duration × configured TPS
pricing_key_for_model(harness, model, config=None) Normalize model names (versioned Claude IDs, provider-qualified gptme routes, @provider pins) to a pricing-table key
merge_with_module_defaults(config) Overlay your config on the module defaults

More helpers live in gptme_usage.harness_models, including quota_pool_label(), resolve_gptme_model(), openrouter_models(), local_models(), and the Claude Code model-ID helpers resolve_cc_version() / cc_dispatch_model_id() (e.g. cc_dispatch_model_id("opus-5-5") -> "claude-opus-5-5"; floating aliases map to a pinned, validated version).

gptme_usage.stale measures realized stale capacity at each quota reset: feed it periodic QuotaReading(account, observed_at, utilization, resets_in_seconds) rows and windows_at_reset() returns one ResetWindow per ended window, with stale = 1 - final utilization. A window whose newest reading is too far before its reset is reported unobserved (stale=None) instead of guessed, and summarize() counts it against coverage, never toward the target. Label readings by the account whose quota resets, not by the credential slot that read them.

Roadmap