Skip to content

Replace hardcoded subtraction chain with registry-driven decomposition#321

Draft
alexmojaki wants to merge 80 commits intomainfrom
feat/token-unit-registry
Draft

Replace hardcoded subtraction chain with registry-driven decomposition#321
alexmojaki wants to merge 80 commits intomainfrom
feat/token-unit-registry

Conversation

@alexmojaki
Copy link
Copy Markdown
Contributor

@alexmojaki alexmojaki commented Mar 23, 2026

Summary

  • Adds a YAML-defined unit registry (prices/units.yml) as the single source of truth for token pricing units
  • Build step generates JSON from YAML for both Python and JS packages
  • Implements Möbius inversion on the containment poset to compute leaf values (replacing hardcoded subtraction chains)
  • Ports the full decomposition engine to both Python and TypeScript
  • No breaking public API changesUsage, ModelPrice, calc_price/calcPrice signatures unchanged

Key behavior change

When a unit (e.g. input_audio_mtok) is NOT priced on a model, its tokens now stay in the catch-all ancestor (input_mtok) instead of being subtracted and priced at $0. This is a bugfix — one test dataset price changes (Google caching scenario).

Architecture

  • prices/units.ymlmake package-dataunits_data.json (Python) + units-data.json (JS)
  • UnitDef has dimensions (direction, modality, cache); containment = dimension superset
  • computeLeafValues() uses (-1)^depth_diff Möbius coefficients to decompose overlapping usage
  • calc_price / calcPrice builds the priced unit set from ModelPrice fields, decomposes, sums

Test plan

  • 465 Python tests pass (including decomposition + equivalence tests)
  • 634 JS tests pass (including dataset equivalence tests)
  • Typecheck clean (basedpyright strict + TypeScript strict)
  • Lint clean (ruff + ESLint)
  • CI green across Python 3.9-3.13 + JS

🤖 Generated with Claude Code

@alexmojaki alexmojaki marked this pull request as ready for review March 23, 2026 15:20
@alexmojaki alexmojaki changed the title Unit registry Replace hardcoded subtraction chain with registry-driven decomposition Mar 23, 2026
devin-ai-integration[bot]

This comment was marked as resolved.

@alexmojaki alexmojaki marked this pull request as draft March 23, 2026 15:49
@alexmojaki alexmojaki marked this pull request as ready for review March 23, 2026 16:26
…scope, no new codegen, per-family wording, unit_families key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant