|
| 1 | +using System; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | + |
| 6 | +namespace Anthropic.SDK.Messaging |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Represents per-model pricing data for estimating API request costs. |
| 10 | + /// All costs are in USD per million tokens unless otherwise noted. |
| 11 | + /// </summary> |
| 12 | + public class ModelPricing |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Cost per million input tokens. |
| 16 | + /// </summary> |
| 17 | + public decimal InputTokenCostPerMillion { get; } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Cost per million output tokens. |
| 21 | + /// </summary> |
| 22 | + public decimal OutputTokenCostPerMillion { get; } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Cost per million cache read tokens (typically 0.1x input cost). |
| 26 | + /// </summary> |
| 27 | + public decimal CacheReadCostPerMillion { get; } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Cost per million 5-minute cache write tokens (typically 1.25x input cost). |
| 31 | + /// </summary> |
| 32 | + public decimal Cache5mWriteCostPerMillion { get; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Cost per million 1-hour cache write tokens (typically 2x input cost). |
| 36 | + /// </summary> |
| 37 | + public decimal Cache1hWriteCostPerMillion { get; } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Cost per 1,000 web search requests. Default is $10 per 1,000 searches. |
| 41 | + /// </summary> |
| 42 | + public decimal WebSearchCostPer1000 { get; } |
| 43 | + |
| 44 | + public ModelPricing( |
| 45 | + decimal inputTokenCostPerMillion, |
| 46 | + decimal outputTokenCostPerMillion, |
| 47 | + decimal? cacheReadCostPerMillion = null, |
| 48 | + decimal? cache5mWriteCostPerMillion = null, |
| 49 | + decimal? cache1hWriteCostPerMillion = null, |
| 50 | + decimal? webSearchCostPer1000 = null) |
| 51 | + { |
| 52 | + InputTokenCostPerMillion = inputTokenCostPerMillion; |
| 53 | + OutputTokenCostPerMillion = outputTokenCostPerMillion; |
| 54 | + CacheReadCostPerMillion = cacheReadCostPerMillion ?? inputTokenCostPerMillion * 0.1m; |
| 55 | + Cache5mWriteCostPerMillion = cache5mWriteCostPerMillion ?? inputTokenCostPerMillion * 1.25m; |
| 56 | + Cache1hWriteCostPerMillion = cache1hWriteCostPerMillion ?? inputTokenCostPerMillion * 2m; |
| 57 | + WebSearchCostPer1000 = webSearchCostPer1000 ?? 10m; |
| 58 | + } |
| 59 | + |
| 60 | + private static readonly ConcurrentDictionary<string, ModelPricing> CustomPricing = new(); |
| 61 | + |
| 62 | + // Ordered longest-prefix-first so that more specific entries match before shorter ones. |
| 63 | + private static readonly List<(string Prefix, ModelPricing Pricing)> BuiltInPricing = new() |
| 64 | + { |
| 65 | + // Opus 4.6 / 4.5 — $5 input, $25 output |
| 66 | + ("claude-opus-4-6", new ModelPricing(5m, 25m)), |
| 67 | + ("claude-opus-4-5", new ModelPricing(5m, 25m)), |
| 68 | + |
| 69 | + // Opus 4.1 — $15 input, $75 output |
| 70 | + ("claude-opus-4-1", new ModelPricing(15m, 75m)), |
| 71 | + |
| 72 | + // Opus 4 — $15 input, $75 output |
| 73 | + ("claude-opus-4", new ModelPricing(15m, 75m)), |
| 74 | + |
| 75 | + // Sonnet 4.6 — $3 input, $15 output |
| 76 | + ("claude-sonnet-4-6", new ModelPricing(3m, 15m)), |
| 77 | + |
| 78 | + // Sonnet 4.5 — $3 input, $15 output |
| 79 | + ("claude-sonnet-4-5", new ModelPricing(3m, 15m)), |
| 80 | + |
| 81 | + // Sonnet 4 — $3 input, $15 output |
| 82 | + ("claude-sonnet-4", new ModelPricing(3m, 15m)), |
| 83 | + |
| 84 | + // Sonnet 3.7 — $3 input, $15 output |
| 85 | + ("claude-3-7-sonnet", new ModelPricing(3m, 15m)), |
| 86 | + |
| 87 | + // Haiku 4.5 — $1 input, $5 output |
| 88 | + ("claude-haiku-4-5", new ModelPricing(1m, 5m)), |
| 89 | + |
| 90 | + // Haiku 3.5 — $0.80 input, $4 output |
| 91 | + ("claude-3-5-haiku", new ModelPricing(0.80m, 4m)), |
| 92 | + }; |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Register or override pricing for a model ID prefix. |
| 96 | + /// Custom registrations take priority over built-in pricing. |
| 97 | + /// </summary> |
| 98 | + /// <param name="modelIdPrefix">The model ID or prefix to match (e.g. "claude-sonnet-4-6").</param> |
| 99 | + /// <param name="pricing">The pricing to use for matching models.</param> |
| 100 | + public static void Register(string modelIdPrefix, ModelPricing pricing) |
| 101 | + { |
| 102 | + if (string.IsNullOrWhiteSpace(modelIdPrefix)) |
| 103 | + throw new ArgumentException("Model ID prefix cannot be null or empty.", nameof(modelIdPrefix)); |
| 104 | + if (pricing == null) |
| 105 | + throw new ArgumentNullException(nameof(pricing)); |
| 106 | + |
| 107 | + CustomPricing[modelIdPrefix] = pricing; |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Remove a previously registered custom pricing entry. |
| 112 | + /// </summary> |
| 113 | + public static bool Unregister(string modelIdPrefix) |
| 114 | + { |
| 115 | + return CustomPricing.TryRemove(modelIdPrefix, out _); |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Clear all custom pricing registrations, reverting to built-in pricing only. |
| 120 | + /// </summary> |
| 121 | + public static void ClearCustomPricing() |
| 122 | + { |
| 123 | + CustomPricing.Clear(); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Look up pricing for a given model ID. Custom registrations are checked first |
| 128 | + /// (longest prefix match), then built-in pricing. |
| 129 | + /// </summary> |
| 130 | + /// <returns>The matching <see cref="ModelPricing"/>, or null if no match is found.</returns> |
| 131 | + public static ModelPricing ForModel(string modelId) |
| 132 | + { |
| 133 | + if (string.IsNullOrWhiteSpace(modelId)) |
| 134 | + return null; |
| 135 | + |
| 136 | + // Check custom registrations first (longest prefix match) |
| 137 | + if (!CustomPricing.IsEmpty) |
| 138 | + { |
| 139 | + var customMatch = CustomPricing.Keys |
| 140 | + .Where(prefix => modelId.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) |
| 141 | + .OrderByDescending(prefix => prefix.Length) |
| 142 | + .FirstOrDefault(); |
| 143 | + |
| 144 | + if (customMatch != null) |
| 145 | + return CustomPricing[customMatch]; |
| 146 | + } |
| 147 | + |
| 148 | + // Fall back to built-in pricing (already ordered longest-first) |
| 149 | + foreach (var (prefix, pricing) in BuiltInPricing) |
| 150 | + { |
| 151 | + if (modelId.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) |
| 152 | + return pricing; |
| 153 | + } |
| 154 | + |
| 155 | + return null; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments