@sisu-ai/mw-usage-tracker
v9.0.4
Published
Measure token usage and estimated cost across your pipeline with minimal setup.
Downloads
219
Maintainers
Readme
@sisu-ai/mw-usage-tracker
Measure token usage and estimated cost across your pipeline with minimal setup.
Setup
npm i @sisu-ai/mw-usage-trackerUsage
import { usageTracker } from '@sisu-ai/mw-usage-tracker';
const app = new Agent()
.use(usageTracker({
'openai:gpt-4o-mini': {
// Preferred: prices per 1M tokens (matches provider docs)
inputPer1M: 0.15,
outputPer1M: 0.60,
// Optional vision pricing (choose one):
// a) Per 1K images (e.g., $0.217/K images)
imagePer1K: 0.217,
// b) Approximate per-1K "image tokens"
// imageInputPer1K: 0.217,
// imageTokenPerImage: 1000,
},
// Fallback default for other models
'*': { inputPer1M: 0.15, outputPer1M: 0.60 },
}, { logPerCall: true }))How it works
- Wraps
ctx.model.generatefor the duration of the pipeline. - Accumulates
promptTokens,completionTokens,totalTokensfromModelResponse.usage. - If a price table is provided, computes
costUSDper call and totals. - Optional: for vision models
- Providers vary: some include image cost in native token usage; others bill separately per image.
- If billed per image batch, set
imagePer1K(e.g., 0.217 USD per 1K images). The tracker converts this to per-image. - Or configure
imageInputPer1K+imageTokenPerImageto approximate per-1K image-token pricing. In that mode we split prompt tokens into estimatedimageTokens = imageCount * imageTokenPerImage(default 1000) andtextPromptTokens = promptTokens - imageTokens, then price them separately.
- Writes totals to
ctx.state.usageand logs them at the end. - Cost is rounded to 6 decimals to avoid showing small calls as 0.00.
Notes
- Each adapter should map its native usage fields to
ModelResponse.usage. - If a provider doesn’t return usage, you’ll get counts of calls only (no cost).
- Image cost estimation is an approximation unless your adapter returns precise image token usage.
Community & Support
Discover what you can do through examples or documentation. Check it out at https://github.com/finger-gun/sisu. Example projects live under examples/ in the repo.
Documentation
Core — Package docs · Error types
Adapters — OpenAI · Anthropic · Ollama
- @sisu-ai/mw-agent-run-api
- @sisu-ai/mw-context-compressor
- @sisu-ai/mw-control-flow
- @sisu-ai/mw-conversation-buffer
- @sisu-ai/mw-cors
- @sisu-ai/mw-error-boundary
- @sisu-ai/mw-guardrails
- @sisu-ai/mw-invariants
- @sisu-ai/mw-orchestration
- @sisu-ai/mw-rag
- @sisu-ai/mw-react-parser
- @sisu-ai/mw-register-tools
- @sisu-ai/mw-tool-calling
- @sisu-ai/mw-trace-viewer
- @sisu-ai/mw-usage-tracker
- @sisu-ai/tool-aws-s3
- @sisu-ai/tool-azure-blob
- @sisu-ai/tool-extract-urls
- @sisu-ai/tool-github-projects
- @sisu-ai/tool-summarize-text
- @sisu-ai/tool-terminal
- @sisu-ai/tool-vec-chroma
- @sisu-ai/tool-web-fetch
- @sisu-ai/tool-web-search-duckduckgo
- @sisu-ai/tool-web-search-google
- @sisu-ai/tool-web-search-openai
- @sisu-ai/tool-wikipedia
Anthropic — hello · control-flow · stream · weather
Ollama — hello · stream · vision · weather · web-search
OpenAI — hello · weather · stream · vision · reasoning · react · control-flow · branch · parallel · graph · orchestration · orchestration-adaptive · guardrails · error-handling · rag-chroma · web-search · web-fetch · wikipedia · terminal · github-projects · server · aws-s3 · azure-blob
Contributing
We build Sisu in the open. Contributions welcome.
Contributing Guide · Report a Bug · Request a Feature · Code of Conduct
Star on GitHub if Sisu helps you build better agents.
Quiet, determined, relentlessly useful.
