All files / src/cost index.ts

100% Statements 79/79
90.9% Branches 20/22
100% Functions 3/3
100% Lines 79/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138                                                                            1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x       1x 25x 525x 91x 25x 25x   1x 4x 4x 4x 4x 4x 48x 4x   4x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x   1x             3x 3x 1x 2x   3x 3x 3x   33x 7x 26x   33x 33x 33x 33x 33x 33x 33x 33x 3x 3x  
/**
 * AgentKits — Cost Calculator
 *
 * Token pricing for all providers. Updated 2026-04.
 *
 * Usage:
 *   import { estimateCost, getPricing, comparePricing } from 'agentkits/cost';
 *   const cost = estimateCost('deepseek', { promptTokens: 1000, completionTokens: 500 });
 *   console.table(comparePricing());
 */
 
// ── Types ──────────────────────────────────────────────────────────
 
export interface TokenPricing {
  provider: string;
  model: string;
  currency: string;
  /** Cost per 1M input tokens */
  inputPer1M: number;
  /** Cost per 1M output tokens */
  outputPer1M: number;
  /** Cost per 1M embedding tokens (if applicable) */
  embeddingPer1M?: number;
  /** Last updated */
  updated: string;
}
 
export interface CostEstimate {
  provider: string;
  model: string;
  inputCost: number;
  outputCost: number;
  totalCost: number;
  currency: string;
}
 
// ── Pricing Data (2026-04) ─────────────────────────────────────────
 
const PRICING: TokenPricing[] = [
  // LLM
  { provider: 'openai',    model: 'gpt-4o',              currency: 'USD', inputPer1M: 2.50,  outputPer1M: 10.00, updated: '2026-04' },
  { provider: 'openai',    model: 'gpt-4o-mini',         currency: 'USD', inputPer1M: 0.15,  outputPer1M: 0.60,  updated: '2026-04' },
  { provider: 'gemini',    model: 'gemini-2.5-flash',    currency: 'USD', inputPer1M: 0.15,  outputPer1M: 0.60,  updated: '2026-04' },
  { provider: 'gemini',    model: 'gemini-2.5-pro',      currency: 'USD', inputPer1M: 1.25,  outputPer1M: 10.00, updated: '2026-04' },
  { provider: 'deepseek',  model: 'deepseek-chat',       currency: 'USD', inputPer1M: 0.14,  outputPer1M: 0.28,  updated: '2026-04' },
  { provider: 'deepseek',  model: 'deepseek-reasoner',   currency: 'USD', inputPer1M: 0.55,  outputPer1M: 2.19,  updated: '2026-04' },
  { provider: 'dashscope', model: 'qwen-max',            currency: 'CNY', inputPer1M: 2.40,  outputPer1M: 9.60,  updated: '2026-04' },
  { provider: 'dashscope', model: 'qwen-plus',           currency: 'CNY', inputPer1M: 0.80,  outputPer1M: 2.00,  updated: '2026-04' },
  { provider: 'dashscope', model: 'qwen-turbo',          currency: 'CNY', inputPer1M: 0.30,  outputPer1M: 0.60,  updated: '2026-04' },
  { provider: 'zhipu',     model: 'glm-4-plus',          currency: 'CNY', inputPer1M: 50.00, outputPer1M: 50.00, updated: '2026-04' },
  { provider: 'zhipu',     model: 'glm-4-flash',         currency: 'CNY', inputPer1M: 0.10,  outputPer1M: 0.10,  updated: '2026-04' },
  { provider: 'moonshot',  model: 'moonshot-v1-auto',    currency: 'CNY', inputPer1M: 12.00, outputPer1M: 12.00, updated: '2026-04' },
  { provider: 'minimax',   model: 'MiniMax-Text-01',     currency: 'CNY', inputPer1M: 1.00,  outputPer1M: 8.00,  updated: '2026-04' },
  { provider: 'ollama',    model: 'any',                 currency: 'USD', inputPer1M: 0,     outputPer1M: 0,     updated: '2026-04' },
 
  // Embedding
  { provider: 'openai',    model: 'text-embedding-3-large', currency: 'USD', inputPer1M: 0.13,  outputPer1M: 0, embeddingPer1M: 0.13, updated: '2026-04' },
  { provider: 'openai',    model: 'text-embedding-3-small', currency: 'USD', inputPer1M: 0.02,  outputPer1M: 0, embeddingPer1M: 0.02, updated: '2026-04' },
  { provider: 'gemini',    model: 'gemini-embedding-001',   currency: 'USD', inputPer1M: 0,     outputPer1M: 0, embeddingPer1M: 0,    updated: '2026-04' },
  { provider: 'deepseek',  model: 'deepseek-embedding-v2',  currency: 'USD', inputPer1M: 0.07,  outputPer1M: 0, embeddingPer1M: 0.07, updated: '2026-04' },
  { provider: 'dashscope', model: 'text-embedding-v3',      currency: 'CNY', inputPer1M: 0.70,  outputPer1M: 0, embeddingPer1M: 0.70, updated: '2026-04' },
  { provider: 'zhipu',     model: 'embedding-3',            currency: 'CNY', inputPer1M: 0.50,  outputPer1M: 0, embeddingPer1M: 0.50, updated: '2026-04' },
  { provider: 'ollama',    model: 'nomic-embed-text',       currency: 'USD', inputPer1M: 0,     outputPer1M: 0, embeddingPer1M: 0,    updated: '2026-04' },
];
 
// ── Public API ─────────────────────────────────────────────────────
 
export function getPricing(provider?: string, model?: string): TokenPricing[] {
  return PRICING.filter(p =>
    (!provider || p.provider === provider) &&
    (!model || p.model === model)
  );
}
 
export function estimateCost(
  provider: string,
  usage: { promptTokens: number; completionTokens: number },
  model?: string,
): CostEstimate {
  const pricing = PRICING.find(p =>
    p.provider === provider && (!model || p.model === model) && p.outputPer1M > 0
  );
 
  if (!pricing) {
    return {
      provider,
      model: model ?? 'unknown',
      inputCost: 0,
      outputCost: 0,
      totalCost: 0,
      currency: 'USD',
    };
  }
 
  const inputCost = (usage.promptTokens / 1_000_000) * pricing.inputPer1M;
  const outputCost = (usage.completionTokens / 1_000_000) * pricing.outputPer1M;
 
  return {
    provider: pricing.provider,
    model: pricing.model,
    inputCost: Math.round(inputCost * 1_000_000) / 1_000_000,
    outputCost: Math.round(outputCost * 1_000_000) / 1_000_000,
    totalCost: Math.round((inputCost + outputCost) * 1_000_000) / 1_000_000,
    currency: pricing.currency,
  };
}
 
export function comparePricing(type: 'llm' | 'embedding' = 'llm'): Array<{
  provider: string;
  model: string;
  inputPer1M: number;
  outputPer1M: number;
  currency: string;
  costFor1KCalls: string;
}> {
  const filtered = type === 'embedding'
    ? PRICING.filter(p => p.embeddingPer1M !== undefined)
    : PRICING.filter(p => p.outputPer1M > 0 && p.embeddingPer1M === undefined);
 
  return filtered
    .sort((a, b) => a.inputPer1M - b.inputPer1M)
    .map(p => {
      // Assume average 500 input + 200 output tokens per call
      const costPer1K = type === 'embedding'
        ? ((500 / 1_000_000) * (p.embeddingPer1M ?? 0) * 1000)
        : ((500 / 1_000_000) * p.inputPer1M + (200 / 1_000_000) * p.outputPer1M) * 1000;
 
      return {
        provider: p.provider,
        model: p.model,
        inputPer1M: p.inputPer1M,
        outputPer1M: p.outputPer1M,
        currency: p.currency,
        costFor1KCalls: `${p.currency} ${costPer1K.toFixed(4)}`,
      };
    });
}