npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ai-sdk-cost-calculator

v7.1.0

Published

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.

Readme

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 onFinish callbacks via withCost() 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 with calculateEmbeddingCost()
  • Speech & transcription: Calculate generateSpeech() (TTS) and transcribe() (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.

const cost = calculateCost({
  model: "gemini-2.0-flash",
  usage: result.usage,
  webSearchRequests: 10,   // Number of web searches
  googleMapsRequests: 5,   // Number of Google Maps API calls
});

console.log(cost.webSearchCost);   // $0.14 for 10 searches
console.log(cost.googleMapsCost);  // $0.035 for 5 maps requests

Auto-Detection from Tool Calls

Automatically detect web search and Google Maps usage from AI SDK results:

import { createTracker } from "ai-sdk-cost-calculator";

const tracker = createTracker();

await generateText({
  model: google("gemini-2.0-flash"),
  prompt: "Find coffee shops near Tokyo Station",
  tools: { web_search: mySearchTool, places: myPlacesTool },
  onFinish: tracker.onFinish("gemini-2.0-flash", undefined, {
    webSearchTools: [],  // Use default tool names for detection
  }),
});

// Or add custom tool names
await generateText({
  model: openai("gpt-4o"),
  prompt: "Search the web",
  tools: { my_custom_search: customSearchTool },
  onFinish: tracker.onFinish("gpt-4o", undefined, {
    webSearchTools: ["my_custom_search"],  // Add to defaults
  }),
});

Using detectRequestsFromResult Directly

import { detectRequestsFromResult, calculateCost } from "ai-sdk-cost-calculator";

const result = await generateText({
  model: google("gemini-2.0-flash"),
  prompt: "What's the weather?",
  tools: { web_search: searchTool },
});

// Detect tool usage
const { webSearchRequests, googleMapsRequests } = detectRequestsFromResult(result, {
  model: "gemini-2.0-flash",       // Pass the model for correct grounding billing
  webSearchTools: ["my_search"],   // Optional: add custom tool names
});

const cost = calculateCost({
  model: "gemini-2.0-flash",
  usage: result.usage,
  webSearchRequests,
  googleMapsRequests,
});

Pass model when detecting Google Search grounding. Google bills grounding differently by model family: the Gemini 3 family is charged per search query ($14/1k), while Gemini 2.5 / 2.0 / 1.5 (and the same models on Vertex AI) are charged once per grounded prompt ($35/1k for 2.5), regardless of how many queries the model issued internally. Detection reads grounding metadata from providerMetadata.google (@ai-sdk/google) and providerMetadata.googleVertex / providerMetadata.vertex (@ai-sdk/google-vertex), so Vertex AI is covered. Without model it falls back to the conservative per-prompt count. The withCost / getCost / tracker integrations pass the model automatically.

Default Tool Names

The following tool names are detected by default:

Web Search: web_search, webSearch, google_search, googleSearch, grounding, web_search_preview, tavily_search, brave_search, bing_search, duckduckgo_search

Google Maps: google_maps, googleMaps, place_search, placeSearch, geocode, places

Pricing by Provider

| Provider | Model | Web Search (/1k) | Google Maps (/1k) | Notes | | -------------- | ---------------- | ---------------- | ----------------- | ---------------------- | | OpenAI | GPT-4o, GPT-4.1 | $10 | - | + tokens at model rate | | OpenAI | GPT-4o-mini | $10 | - | + 8k tokens per search | | Google | Gemini Flash | $14 | $7 | Grounding with Google | | Google | Gemini Pro | $35 | $7 | Grounding with Google | | xAI | Grok models | $5 | - | Web Search / X Search | | Perplexity | Sonar | $5 | - | Search-native | | Perplexity | Sonar Pro | $6 | - | Search-native |

Image Generation Costs

Calculate costs for image generation models (GPT Image, DALL-E, Imagen).

const cost = calculateCost({
  model: "gpt-image-2",
  usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 },
  imageGenerations: 2,          // Number of images
  imageSize: "high-1024x1024",  // {quality}-{size}; omit → medium 1024x1024
});

console.log(cost.imageGenerationCost); // $0.422 for 2 high-quality images

The GPT Image models price per image by quality (low/medium/high) and size (1024x1024, 1024x1536, 1536x1024). Pass imageSize as "{quality}-{size}" (e.g. "high-1536x1024"). A bare size ("1024x1024") or omitting imageSize bills at medium quality. For image edits, reference images are billed as image input tokens — pass imageInputTokens to charge them at the model's image input rate.

Image Generation Pricing

GPT Image — per image, USD (low / medium / high):

| Model | 1024x1024 | 1024x1536 · 1536x1024 | Image out /1M | | ----------------- | ---------------------- | ----------------------- | ------------- | | gpt-image-2 | 0.006 / 0.053 / 0.211 | 0.005 / 0.041 / 0.165 | $30 | | gpt-image-1.5 | 0.009 / 0.034 / 0.133 | 0.013 / 0.050 / 0.200 | $32 | | gpt-image-1 ⚠️ | 0.011 / 0.042 / 0.167 | 0.016 / 0.063 / 0.250 | $40 | | gpt-image-1-mini | 0.005 / 0.011 / 0.036 | 0.006 / 0.015 / 0.052 | $8 |

⚠️ gpt-image-1 deprecates 2026-10-23. Source: OpenAI image generation guide.

Legacy / other providers — per image (flat by size):

| Provider | Model | Default (/image) | Size Options | | ---------- | ----------------- | ---------------- | ------------------------------------------------- | | OpenAI | DALL-E 3 | $0.04 | 1024x1024: $0.04, 1024x1792: $0.08, hd-*: $0.08-$0.12 | | OpenAI | DALL-E 2 | $0.02 | 1024x1024: $0.02, 512x512: $0.018, 256x256: $0.016 | | Google | Imagen 3 | $0.04 | 1024x1024: $0.04, 1536x1536: $0.08 | | Google | Imagen 3 Fast | $0.02 | 1024x1024: $0.02, 1536x1536: $0.04 |

Auto-Detection for Image Generation

await generateText({
  model: openai("gpt-4o"),
  prompt: "Generate an image of a cat",
  tools: { generate_image: myImageGenTool },
  onFinish: tracker.onFinish("gpt-4o", undefined, {
    imageGenerationTools: [],  // Use default tool names
  }),
});

// Custom tool names
onFinish: tracker.onFinish("gpt-4o", undefined, {
  imageGenerationTools: ["my_dalle_tool"],
})

Default Image Generation Tool Names: generate_image, generateImage, image_generation, imageGeneration, create_image, createImage, dall_e, dalle, imagen, stable_diffusion, text_to_image, textToImage

Realtime / Live Sessions

Realtime and Live models (AI SDK experimental_useRealtime, OpenAI Realtime, Gemini Live, xAI Grok Voice) report usage split by modality — text, audio, image, and video tokens are each billed at different rates, and some providers offer per-minute billing as an alternative to per-token.

Use calculateRealtimeCost for a one-shot total, or createRealtimeCostTracker to accumulate cost across a session as usage events stream in.

import { calculateRealtimeCost } from "ai-sdk-cost-calculator";

const cost = calculateRealtimeCost({
  model: "gemini-3.1-flash-live-preview",
  usage: {
    inputTextTokens: 1_000,
    inputAudioTokens: 12_000,   // billed at the audio input rate ($3/1M)
    inputImageTokens: 500,      // billed at the image input rate ($1/1M)
    outputTextTokens: 800,
    outputAudioTokens: 6_000,   // billed at the audio output rate ($12/1M)
  },
  webSearchRequests: 3,         // Google Search grounding ($14/1k)
});

console.log(cost.audioInputCost, cost.audioOutputCost, cost.totalCost);

Per-minute billing

Live APIs may bill audio and image/video by wall-clock minutes instead of tokens. Supply the *Minutes fields and the matching modality is billed at minutes × rate. Still pass the modality token counts so those tokens are excluded from the text charge.

const cost = calculateRealtimeCost({
  model: "gemini-3.1-flash-live-preview",
  usage: {
    inputAudioTokens: 100_000,
    inputAudioMinutes: 10,      // 10 min × $0.005 = $0.05 (instead of per-token)
    outputAudioTokens: 50_000,
    outputAudioMinutes: 5,      // 5 min × $0.018 = $0.09
  },
});

Session tracking

import { createRealtimeCostTracker } from "ai-sdk-cost-calculator";

const tracker = createRealtimeCostTracker({
  model: "gpt-realtime",
  costMultiplier: 1.2, // optional markup
});

// Feed each realtime usage event (a delta) as it arrives:
tracker.addUsage({ inputAudioTokens: 2_000, outputAudioTokens: 1_000 });
tracker.addUsage({ inputAudioTokens: 1_500, outputAudioTokens: 800 });

console.log(tracker.getTotalCost().totalCost);
console.log(tracker.getTotalUsage()); // accumulated per-modality tokens/minutes

Wire it into the AI SDK realtime hook by forwarding usage events from the session to tracker.addUsage.

Realtime models with modality pricing: gemini-3.1-flash-live-preview, gemini-2.5-flash-native-audio-preview-12-2025, gemini-3.5-live-translate-preview, gpt-realtime, gpt-realtime-2, gpt-realtime-mini, gpt-4o-realtime-preview, grok-voice (pricing unconfirmed).

Embeddings

embed() and embedMany() report usage as { tokens } (AI SDK EmbeddingModelUsage), a shape calculateCost cannot price. Use calculateEmbeddingCost — embeddings are billed input-only.

import { embedMany } from "ai";
import { openai } from "@ai-sdk/openai";
import { calculateEmbeddingCost } from "ai-sdk-cost-calculator";

const { embeddings, usage } = await embedMany({
  model: openai.textEmbedding("text-embedding-3-small"),
  values: chunks,
});

const cost = calculateEmbeddingCost({
  model: "text-embedding-3-small",
  usage, // or a raw token count
});
console.log(cost.totalCost);

Accumulate across many calls with createEmbeddingCostTracker:

import { createEmbeddingCostTracker } from "ai-sdk-cost-calculator";

const tracker = createEmbeddingCostTracker({ model: "text-embedding-3-small" });
tracker.addUsage(result1.usage);
tracker.addUsage(result2.usage);
console.log(tracker.getTotalCost().totalCost, tracker.getTotalTokens());

Embedding models with pricing: OpenAI text-embedding-3-small / text-embedding-3-large / text-embedding-ada-002, Google gemini-embedding-2, Cohere embed-v4.0 / embed-english-v3.0 / embed-multilingual-v3.0, Mistral mistral-embed / codestral-embed, Perplexity pplx-embed-*.

Speech (TTS) & Transcription (STT)

generateSpeech() and transcribe() results carry no usage object, so cost is derived from the input. TTS is billed per input character (tts-1) or per token (gpt-4o-mini-tts); transcription is billed per minute of audio (whisper-1, using the result's durationInSeconds) or per token (gpt-4o-transcribe).

import { generateSpeech, transcribe } from "ai";
import { openai } from "@ai-sdk/openai";
import {
  calculateSpeechCost,
  calculateTranscriptionCost,
} from "ai-sdk-cost-calculator";

// Text-to-speech — billed per input character
const text = "Hello, world!";
await generateSpeech({ model: openai.speech("tts-1"), text });
const ttsCost = calculateSpeechCost({ model: "tts-1", text });

// Speech-to-text — billed per minute of audio
const transcript = await transcribe({
  model: openai.transcription("whisper-1"),
  audio,
});
const sttCost = calculateTranscriptionCost({
  model: "whisper-1",
  durationInSeconds: transcript.durationInSeconds,
});

Use createSpeechCostTracker / createTranscriptionCostTracker to accumulate across many calls.

Models with pricing: OpenAI tts-1 / tts-1-hd / gpt-4o-mini-tts, whisper-1 / gpt-4o-transcribe / gpt-4o-mini-transcribe, Groq whisper-large-v3 / whisper-large-v3-turbo, Google Gemini TTS models.

Video Generation

experimental_generateVideo() returns no usage — video length lives in providerMetadata. Cost is billed per second (Google Veo) or per video, from the duration and n you requested.

import { experimental_generateVideo as generateVideo } from "ai";
import { vertex } from "@ai-sdk/google-vertex";
import { calculateVideoCost } from "ai-sdk-cost-calculator";

const { videos } = await generateVideo({
  model: vertex.video("veo-3.0-generate-001"),
  prompt: "A cat walking on a treadmill",
  duration: 8,
});

const cost = calculateVideoCost({
  model: "veo-3.0-generate-001",
  seconds: 8,
  videos: videos.length,
});

Use createVideoCostTracker to accumulate across calls, and customPricing (with videoGenerationPerSecond or videoGenerationPerVideo) for models not in the registry.

Models with pricing: Google veo-2.0-generate-001 ($0.35/s), veo-3.0 / veo-3.1 (-generate-001 $0.40/s, -fast-generate-001 $0.15/s).

API Reference

calculateCost(options)

Calculate the cost for a single API request.

import { calculateCost } from "ai-sdk-cost-calculator";

const cost = calculateCost({
  model: "claude-sonnet-4-5",
  usage: {
    inputTokens: 1000,
    outputTokens: 500,
    inputTokenDetails: {
      noCacheTokens: 700,
      cacheReadTokens: 200,
      cacheWriteTokens: 100,
    },
    outputTokenDetails: {
      textTokens: 500,
      reasoningTokens: 0,
    },
  },
  webSearchRequests: 5,
});

Options

| Property | Type | Required | Description | | -------------------- | -------------------- | -------- | ---------------------------------------------------------- | | model | string | Yes | Model identifier (e.g., "gpt-4o", "claude-sonnet-4-5") | | usage | LanguageModelUsage | Yes | Token usage from AI SDK | | customPricing | ModelPricing | No | Override default pricing | | webSearchRequests | number | No | Number of web search requests | | googleMapsRequests | number | No | Number of Google Maps API requests |

Return Value: CostBreakdown

| Property | Type | Description | | ---------------- | --------- | -------------------------------------------- | | inputCost | number | Cost for input tokens (excluding cache) | | outputCost | number | Cost for output tokens (excluding reasoning) | | cacheReadCost | number | Cost for cached input tokens | | cacheWriteCost | number | Cost for writing to cache | | reasoningCost | number | Cost for reasoning tokens | | webSearchCost | number | Cost for web search requests | | googleMapsCost | number | Cost for Google Maps API requests | | totalCost | number | Sum of all costs | | currency | "USD" | Always USD | | isLongContext | boolean | Whether long context pricing was applied |

createTracker(options?)

Create a multi-model cost tracker.

import { createTracker } from "ai-sdk-cost-calculator";

const tracker = createTracker({
  customPricing: {
    "my-model": { inputPer1MTokens: 1, outputPer1MTokens: 2 },
  },
  onCost: (model, cost) => console.log(model, cost.totalCost),
});

// Methods
tracker.onFinish(model, callback?, options?);  // Create onFinish callback
tracker.add(model, usage, options?);           // Add usage, returns CostBreakdown
tracker.getModel(model);                       // Get ModelCostSummary for a model
tracker.getModels();                           // Get all tracked model names
tracker.getAllCosts();                         // Get all ModelCostSummary[]
tracker.getTotal();                            // Get total CostBreakdown
tracker.getTotalRequestCount();                // Get total request count
tracker.resetModel(model);                     // Reset specific model
tracker.reset();                               // Reset all

createCostTracker(options)

Track cumulative costs for a single model.

import { createCostTracker } from "ai-sdk-cost-calculator";

const tracker = createCostTracker({ model: "gpt-4o" });

tracker.addUsage(result1.usage);
tracker.addUsage(result2.usage);

console.log(tracker.getTotalCost().totalCost);
tracker.reset();

withCost(model, callback?, options?)

Create an onFinish callback wrapper that adds cost calculation.

import { withCost } from "ai-sdk-cost-calculator";

await generateText({
  model: openai("gpt-4o"),
  prompt: "Hello!",
  onFinish: withCost("gpt-4o", ({ cost, usage, text }) => {
    console.log(`Cost: $${cost.totalCost}`);
  }),
});

| Parameter | Type | Required | Description | | ---------- | ------------------------- | -------- | ------------------------------------ | | model | LanguageModel \| string | Yes | Model ID or AI SDK model instance | | callback | function | No | Called with result + cost breakdown | | options | CostAwareOptions | No | Custom pricing, web search, onCost |

getCost(model, usage, options?)

One-shot cost calculation from AI SDK result.

import { getCost } from "ai-sdk-cost-calculator";

const result = await generateText({ model: openai("gpt-4o"), prompt: "Hello!" });
const cost = getCost("gpt-4o", result.usage);
// or: getCost(openai("gpt-4o"), result.usage)

| Parameter | Type | Required | Description | | --------- | ------------------------- | -------- | ----------------------------------- | | model | LanguageModel \| string | Yes | Model ID or AI SDK model instance | | usage | LanguageModelUsage | Yes | Token usage from AI SDK | | options | CostAwareOptions | No | Custom pricing, web search, onCost |

createCostAwareAI(options?)

Create a cost-tracking helper for use across multiple AI SDK calls.

import { createCostAwareAI } from "ai-sdk-cost-calculator";

const ai = createCostAwareAI({
  customPricing: { "my-model": { inputPer1MTokens: 1, outputPer1MTokens: 2 } },
  onCost: (model, cost, usage) => console.log(`[${model}] $${cost.totalCost}`),
});

// Use with onFinish
await generateText({
  model: openai("gpt-4o"),
  onFinish: ai.onFinish("gpt-4o"),
});

// Or calculate directly
const cost = ai.calculate("gpt-4o", result.usage);

// Methods
ai.onFinish(model, callback?, options?);  // Create onFinish callback
ai.calculate(model, usage, options?);     // Calculate and track cost
ai.getModel(model);                       // Get CostBreakdown for a model
ai.getModels();                           // Get all tracked model names
ai.getTotal();                            // Get total CostBreakdown
ai.getRequestCount();                     // Get total request count
ai.reset();                               // Reset all tracking

getModelId(model)

Extract model ID from AI SDK model or string.

import { getModelId } from "ai-sdk-cost-calculator";
import { openai } from "@ai-sdk/openai";

getModelId("gpt-4o");           // "gpt-4o"
getModelId(openai("gpt-4o"));   // "gpt-4o"

formatCost(cost, decimals?)

Format a cost value as a currency string.

formatCost(0.0015);      // "$0.001500"
formatCost(0.0015, 4);   // "$0.0015"

formatCostBreakdown(breakdown, decimals?)

Format a full cost breakdown as a multi-line string.

const output = formatCostBreakdown(cost);
// Input:        $0.002400
// Cache Read:   $0.000060
// Output:       $0.007500
// Web Search:   $0.050000
// Google Maps:  $0.007000
// Total:        $0.066960

calculateStreamCost(streamResult, options)

Calculate cost from a streaming response.

import { streamText } from "ai";
import { calculateStreamCost, formatCost } from "ai-sdk-cost-calculator";

const stream = await streamText({
  model: openai("gpt-4o"),
  prompt: "Write a haiku",
});

const cost = await calculateStreamCost(stream, {
  model: "gpt-4o",
  onCost: (cost, usage) => {
    console.log(`Stream completed: ${formatCost(cost.totalCost)}`);
  },
});

detectRequestsFromResult(result, options?)

Detect web search and Google Maps tool calls from an AI SDK result.

import { detectRequestsFromResult } from "ai-sdk-cost-calculator";

const result = await generateText({
  model: google("gemini-2.0-flash"),
  prompt: "Find restaurants",
  tools: { web_search: searchTool, places: placesTool },
});

// Pass `model` so Google Search grounding is billed per the model's family
// (Gemini 3 = per query, Gemini 2.5/2.0/1.5 + Vertex = per grounded prompt).
const { webSearchRequests, googleMapsRequests } = detectRequestsFromResult(result, {
  model: "gemini-2.0-flash",
});

// With custom tool names (added to defaults)
const detected = detectRequestsFromResult(result, {
  model: "gemini-2.0-flash",
  webSearchTools: ["my_search", "custom_tavily"],
  googleMapsTools: ["location_finder"],
});

| Parameter | Type | Required | Description | | ---------------------- | --------------------- | -------- | ------------------------------------------ | | result | ResultWithToolCalls | Yes | AI SDK result with toolCalls/steps | | options.model | LanguageModel \| string | No | Model id — required for correct grounding billing | | options.webSearchTools | string[] | No | Additional tool names to count as search | | options.googleMapsTools | string[] | No | Additional tool names to count as maps |

withDetectedRequests(result, options?)

Convenience wrapper that returns detected requests for spreading into calculateCost.

import { withDetectedRequests, calculateCost } from "ai-sdk-cost-calculator";

const cost = calculateCost({
  model: "gemini-2.0-flash",
  usage: result.usage,
  ...withDetectedRequests(result, { model: "gemini-2.0-flash" }),
});

// With custom tool names
const cost2 = calculateCost({
  model: "gemini-2.0-flash",
  usage: result.usage,
  ...withDetectedRequests(result, {
    model: "gemini-2.0-flash",
    webSearchTools: ["my_search"],
  }),
});

Supported Models

OpenAI (23 models)

  • GPT-4o, GPT-4o-mini
  • GPT-4.1, GPT-4.1-mini, GPT-4.1-nano
  • GPT-4 Turbo, GPT-4, GPT-4-32k
  • GPT-3.5 Turbo
  • o1, o1-mini, o1-pro, o1-preview
  • o3, o3-mini, o3-pro
  • o4-mini

Anthropic (23 models)

  • Claude Opus 4.5, Sonnet 4.5, Haiku 4.5
  • Claude Opus 4.1, Sonnet 4, Opus 4
  • Claude 3.5 Sonnet, 3.5 Haiku
  • Claude 3 Opus, 3 Sonnet, 3 Haiku
  • All dated variants (e.g., claude-3-5-sonnet-20241022)

Google Gemini (17 models)

  • Gemini 3 Pro, 3 Flash
  • Gemini 2.5 Pro, 2.5 Flash, 2.5 Flash-Lite
  • Gemini 2.0 Flash, 2.0 Flash-Lite
  • Gemini 1.5 Pro, 1.5 Flash, 1.5 Flash-8B

xAI Grok (12 models)

  • Grok 4, Grok 4-0709
  • Grok 4 Fast (reasoning/non-reasoning)
  • Grok 4.1 Fast (reasoning/non-reasoning)
  • Grok Code Fast
  • Grok 3, Grok 3-mini
  • Grok 2, Grok 2 Vision

DeepSeek (10 models)

  • DeepSeek Chat (V3, V3.2)
  • DeepSeek Reasoner (R1)
  • DeepSeek R1 Distill variants
  • DeepSeek Coder

Perplexity (6 models)

  • Sonar, Sonar Small
  • Sonar Pro
  • Sonar Reasoning, Sonar Reasoning Pro
  • Sonar Deep Research

Long Context Pricing

Some models have tiered pricing based on input token count:

| Model | Threshold | Standard | Long Context | | ----------------- | --------- | ----------- | ------------ | | Claude Sonnet 4.5 | 200K | $3/$15 | $6/$22.50 | | Gemini 2.5 Pro | 200K | $1.25/$10 | $2.50/$15 | | Gemini 1.5 Pro | 128K | $1.25/$5 | $2.50/$10 | | Grok 4 Fast | 128K | $0.20/$0.50 | $0.40/$1.00 |

The calculator automatically applies the correct pricing tier based on input token count.

Custom Pricing

Override default pricing for any model:

const cost = calculateCost({
  model: "gpt-4o",
  usage: result.usage,
  customPricing: {
    inputPer1MTokens: 2.0,
    outputPer1MTokens: 8.0,
    cacheReadPer1MTokens: 1.0,
    webSearchPer1kRequests: 15,
    googleMapsPer1kRequests: 10,
  },
});

TypeScript Types

import type {
  // Core
  CostBreakdown,
  CalculateCostOptions,
  ModelPricing,
  ProviderPricing,
  Provider,
  // Single model tracker
  CostTracker,
  CreateCostTrackerOptions,
  StreamCostOptions,
  // Multi-model tracker
  MultiModelCostTracker,
  CreateMultiModelTrackerOptions,
  ModelCostSummary,
  AddUsageOptions,
  // AI SDK integration
  CostAwareAI,
  CreateCostAwareAIOptions,
  CostAwareOptions,
  ResultWithCost,
  FinishResultWithCost,
  // Auto-detection
  DetectOptions,
  DetectedRequests,
  ResultWithToolCalls,
} from "ai-sdk-cost-calculator";

Pricing Sources

Pricing data is sourced from official provider documentation:

Note: Prices may change. Please verify current pricing with official sources for production use.

License

MIT