@costwise/sdk
v0.0.17
Published
AI API cost tracking for developers
Readme
@costwise/sdk
Track OpenAI, Anthropic, Google Gemini, and Mistral API usage costs with a drop-in SDK wrapper.
Install
npm install @costwise/sdkQuick start
Replace:
import OpenAI from "openai";with:
import { CostwiseOpenAI } from "@costwise/sdk";Then initialize:
import { CostwiseOpenAI, CostwiseAnthropic } from "@costwise/sdk";
import { CostwiseGoogle, CostwiseMistral } from "@costwise/sdk/providers";
const openai = new CostwiseOpenAI({
apiKey: process.env.OPENAI_API_KEY,
costwiseKey: process.env.COSTWISE_API_KEY,
tag: "my-app"
});
const anthropic = new CostwiseAnthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
costwiseKey: process.env.COSTWISE_API_KEY,
tag: "my-app"
});
const google = new CostwiseGoogle({
apiKey: process.env.GOOGLE_API_KEY,
costwiseKey: process.env.COSTWISE_API_KEY
});
const mistral = new CostwiseMistral({
apiKey: process.env.MISTRAL_API_KEY,
costwiseKey: process.env.COSTWISE_API_KEY
});Note: @costwise/sdk/providers is ESM import only.
Tracking is fire-and-forget and never awaited, so request latency is not impacted.
Usage storage
Usage is always recorded to ~/.costwise/usage.jsonl (auto-created).
Ingest payload
The SDK emits:
modelinput_tokensoutput_tokenscostandestimated_costtagandendpoint_tag(optional)tsandtimestamp
Real usage reporting
This SDK is optimized for real usage tracking and CLI reporting:
- Track real calls in your app (usage is written locally and ingested in background).
- View last-7-days usage plus a similar cross-provider model comparison with:
npx costwise usage
CLI for actual usage
The CLI reads real usage events from the SDK usage file:
~/.costwise/usage.jsonl.
Run your app so events are recorded.
Query usage:
npx costwise usagenpx costwise usage prints an easy 7-day summary:
- total tokens used
- average cost per token (USD)
- total cost (USD)
- similar models from other providers (OpenAI, Anthropic, Google, Mistral) and what the same token usage would cost there
Note: Gemini and Mistral model catalogs are kept in sync with their latest naming (including latest aliases and preview IDs). When a brand-new model has no public token-price yet, Costwise uses the closest known tier until official pricing is available.
JSON output:
costwise usage --jsonContact
For questions or feedback: [email protected]
