Repository navigation
Add llmmend/cost: estimateCost and toUsage - #91
Merged
Merged
Conversation
Prices a call's token usage with prices the caller supplies per million tokens, by part, with long-context tiers and a list of missing counts. toUsage reads raw Provider usage; the stream adapters now share its conversions (src/shared/usage.ts). Closes #90 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F61ff7dDPwJn5kuD25LtiS
This was referenced Oct 27, 2026
Merged
nightlightmare
added a commit
that referenced
this pull request
Oct 27, 2026
Releases 0.6.0. ## What ships From `CHANGELOG.md`, three new modules, all **minor**: - `llmmend/prompt`: `safePrompt`, `trusted`, `escapeTags`: untrusted text can't close or forge a prompt's own tags (#89). - `llmmend/cost`: `estimateCost`, `toUsage`: the cost of a call by part from its usage and prices per million tokens (#91). - `llmmend/retry`: `classifyError`: eleven provider-neutral error kinds, `retryable` and the Provider's `retryAfter` (#94). One **patch**: stream `usage` events leave out a count the Provider sent as `null`; the four adapters now share one usage conversion with `toUsage` (#91). Added to the changelog by hand, since #91's changeset described only the new module. The playground's Prompt, Cost and Errors tabs (#92, #94) are already live on Pages. ## Before release - **Provider adapters** changed in #91 (the shared usage conversion), so the providers were re-recorded and compared with the fixtures, then restored: - **Anthropic** (via Bedrock), all seven cases: same payload fields, LLM event types, usage fields, stop reasons; `responseId` set in all. - **Gemini**: `text`, `thinking`, `max-tokens` the same on every count. `tool-calls-parallel` got HTTP 503 `UNAVAILABLE` (high demand), which `classifyError` reads as `overloaded`, retryable. - **Drift:** the Ollama drift check passes on main at f3af34c, after the adapter change ([run](https://github.com/nightlightmare/llmmend/actions/runs/40589829344)). - **Size:** within limits. `prompt` 582 B, `cost` 689 B, `retry` 1.39 of 1.5 kB; the existing bundles did not grow (`collectText` 6.78 kB, `streamToolCalls` 6.92 kB). - **Local checks:** tests (911), docs, size, smoke on all seven subpaths, and `check:publish` pass. CI on main is green at f3af34c. ## After merge `release.yml` stages 0.6.0 with `npm stage publish` and creates the tag and GitHub release; approve the staged version on npmjs.com with 2FA. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01F61ff7dDPwJn5kuD25LtiS Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
toUsagenow share one conversion per Provider, so a raw reply and a stream of the same call give the sameLlmUsage. One behaviour change in the adapters: anullcount (Anthropic sendscache_creation_input_tokens: nullin some cases) is treated as absent instead of passing through.docs/api.mdsection: all in three languages.llms.txtregenerated.Evidence
Before: the raw Anthropic
input_tokens(1,200) priced as all input misses the 40,000 cached tokens; adding them at the input price overstates the cost tenfold.After: 12 new tests (tiers, missing counts, every raw shape, unknown shapes); all 342 SSE tests pass unchanged on the shared conversion; 875 tests, docs, smoke, publint and attw pass.
llmmend/costis 689 B of its 1 kB limit; the SSE bundles did not grow (collectText6.78 kB,streamToolCalls6.92 kB).Merge Danger
Door: two-way
Blast Radius: stream usage events
Additive module, but the stream adapters' usage code moved to
src/shared/usage.ts. The recorded fixtures for all four Providers pass unchanged; the only difference isnullcounts, now dropped.Closes #90
🤖 Generated with Claude Code
https://claude.ai/code/session_01F61ff7dDPwJn5kuD25LtiS