ai-sdk-cost-calculator weekly downloads statistics
Cost calculator for Vercel AI SDK token usage. Supports OpenAI, Anthropic, Google, xAI (Grok), and DeepSeek with long context pricing, prompt caching, and reasoning tokens.
ai-sdk-cost-calculator has 3,361 recorded weekly downloads on npm, a decrease of 56.0% over the past 30 days, ranking #1221 of 2,322 tracked listings.
About ai-sdk-cost-calculator
ai-sdk-cost-calculator
Cost calculator for Vercel AI SDK token usage. Calculate API costs for OpenAI, Anthropic, Google, xAI (Grok), DeepSeek, and Perplexity with support for long context pricing, prompt caching, reasoning tokens, and web search.
Features
- Multi-provider support: OpenAI, Anthropic, Google Gemini, xAI Grok, DeepSeek, Perplexity
- AI SDK native: Use with
onFinishcallbacks viawithCost()wrapper - Session tracking: Track costs across multiple calls with
createCostAwareAI() - Multi-model tracking: Track costs across different models in a single tracker
- Web search pricing: Calculate costs for grounding/search features
- Google Maps pricing: Calculate costs for Google Maps API requests
- Image generation pricing: Calculate costs for DALL-E, Imagen, and other image models
- Embedding pricing: Calculate
embed()/embedMany()costs withcalculateEmbeddingCost() - Speech & transcription: Calculate
generateSpeech()(TTS) andtranscribe()(STT) costs - Video generation: Calculate
experimental_generateVideo()costs (per-second / per-video) - Auto-detection: Automatically detect web search/maps/image generation tool calls from results
- Long context pricing: Automatic tier-based pricing (e.g., Claude Sonnet 4.5 >200K tokens)
- Prompt caching: Separate pricing for cache reads and writes
- Reasoning tokens: Support for o1/o3/R1 reasoning model costs
- TypeScript: Full type definitions included
Installation
npm install ai-sdk-cost-calculator
Peer dependency: Requires ai (Vercel AI SDK) version 6.0.0 or higher.
npm install ai
Quick Start
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
import { calculateCost, formatCost } from "ai-sdk-cost-calculator";
const result = await generateText({
model: openai("gpt-4o"),
prompt: "Hello, world!",
});
const cost = calculateCost({
model: "gpt-4o",
usage: result.usage,
});
console.log(`Total cost: ${formatCost(cost.totalCost)}`);
// Output: Total cost: $0.000150
AI SDK Integration
Using withCost with onFinish
The easiest way to track costs is with the withCost callback wrapper:
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
import { withCost, formatCost } from "ai-sdk-cost-calculator";
const result = await generateText({
model: openai("gpt-4o"),
prompt: "Hello!",
onFinish: withCost("gpt-4o", ({ cost, usage }) => {
console.log(`Cost: ${formatCost(cost.totalCost)}`);
console.log(`Tokens: ${usage.inputTokens} in, ${usage.outputTokens} out`);
}),
});
You can also pass the model object directly:
const model = openai("gpt-4o");
await generateText({
model,
prompt: "Hello!",
onFinish: withCost(model, ({ cost }) => {
console.log(`Cost: ${formatCost(cost.totalCost)}`);
}),
});
Using getCost for One-Shot Calculation
For simple post-hoc cost calculation:
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
import { getCost, formatCost } from "ai-sdk-cost-calculator";
const result = await generateText({
model: openai("gpt-4o"),
prompt: "Hello!",
});
const cost = getCost("gpt-4o", result.usage);
console.log(`Cost: ${formatCost(cost.totalCost)}`);
Using createCostAwareAI for Session Tracking
For tracking costs across multiple calls in a session:
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
import { anthropic } from "@ai-sdk/anthropic";
import { createCostAwareAI, formatCost } from "ai-sdk-cost-calculator";
const ai = createCostAwareAI({
onCost: (model, cost) => {
console.log(`[${model}] ${formatCost(cost.totalCost)}`);
},
});
// First call
await generateText({
model: openai("gpt-4o"),
prompt: "Hello!",
onFinish: ai.onFinish("gpt-4o"),
});
// Second call with different model
await generateText({
model: anthropic("claude-sonnet-4-5"),
prompt: "Hello!",
onFinish: ai.onFinish("claude-sonnet-4-5"),
});
// Get totals
console.log(`Total: ${formatCost(ai.getTotal().totalCost)}`);
console.log(`Requests: ${ai.getRequestCount()}`);
console.log(`Models: ${ai.getModels().join(", ")}`);
// Reset when needed
ai.reset();
Multi-Model Tracker
Track costs across multiple models in a single tracker instance.
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
import { anthropic } from "@ai-sdk/anthropic";
import { createTracker, formatCost } from "ai-sdk-cost-calculator";
const tracker = createTracker({
onCost: (model, cost) => {
console.log(`[${model}] Cost: ${formatCost(cost.totalCost)}`);
},
});
// Use with onFinish callback
await generateText({
model: openai("gpt-4o"),
prompt: "Hello!",
onFinish: tracker.onFinish("gpt-4o"),
});
await generateText({
model: anthropic("claude-sonnet-4-5"),
prompt: "Hello!",
onFinish: tracker.onFinish("claude-sonnet-4-5", ({ cost, text }) => {
console.log(`Response: ${text}, Cost: ${formatCost(cost.totalCost)}`);
}),
});
// Or add usage manually
tracker.add("gpt-4o", result.usage, { webSearchRequests: 5 });
// Get per-model costs
for (const summary of tracker.getAllCosts()) {
console.log(`${summary.model}: ${formatCost(summary.cost.totalCost)}`);
}
// Get total cost across all models
console.log(`Total: ${formatCost(tracker.getTotal().totalCost)}`);
console.log(`Requests: ${tracker.getTotalRequestCount()}`);
console.log(`Models: ${tracker.getModels().join(", ")}`);
// Reset when needed
tracker.reset();
Web Search & Google Maps Costs
Calculate costs including web search/grounding and Google Maps features.…
Description from the npm listing.
- ai
- ai-sdk
- vercel-ai
- cost
- calculator
- pricing
- token
- usage
- openai
- anthropic
- claude
- gemini
- xai
- grok
- deepseek
- llm
- gpt
- chatgpt
README badge
Show your rank in a README or on your website. The badge updates automatically.
Charts
Images: 30d · 1y · all time · 1600×900 dark
Adoption over time
7/30/90-day growth
Ranking history
Listing placement
Listing placement
Filled points are observed values; hollow points come from Internet Archive snapshots of the listing page. Dashed segments span periods with no observations — values between points are not interpolated.
Each band is one page of 20. Weekly downloads on a linear scale. Placement is projected from weekly downloads, so it can differ from the marketplace's own sort order. Hover a dot to see the listing.
Recent observations
Full history (122)Similar npm AI SDKs plugins
All 20 ai-sdk-cost-calculator alternativesData source
Weekly downloads (trailing 7 complete days) come from the official npm downloads API. Downloads include CI and mirrors and are not equivalent to installs or users. npm publishes no ratings.
Listing: https://www.npmjs.com/package/ai-sdk-cost-calculator. First seen Oct 16, 2026; last successful collection Oct 20, 2026. See the methodology.