import * as plugins from './plugins.js'; const checkedAt = '2026-10-21'; const pricingSource: plugins.IModelSource = { url: 'https://platform.claude.com/docs/en/about-claude/pricing', checkedAt }; /** USD per million tokens as an integer rate (micro-USD per million tokens). */ const usd = (dollarsArg: number): number => Math.round(dollarsArg * 1_000_000); /** A price row as the pricing page prints it: base input, 5-minute write, 1-hour write, cache hit, output. */ type TAnthropicPriceRow = readonly [input: number, write5m: number, write1h: number, cacheHit: number, output: number]; const rates = ([input, write5m, write1h, cacheHit, output]: TAnthropicPriceRow): plugins.IModelTokenRates => ({ input: usd(input), output: usd(output), cacheRead: usd(cacheHit), cacheWrite: { '5m': usd(write5m), '1h': usd(write1h) }, }); const flat = (rowArg: TAnthropicPriceRow): plugins.IModelServicePrice => ({ bands: [{ rates: rates(rowArg) }] }); /** Web search is $10 per 1,000 searches; web fetch costs nothing beyond its tokens. */ const toolCallFees = { web_search: 10_000, web_fetch: 0 }; /** `inference_geo: "us"` multiplies every token category by 1.1 on Claude 4.6 and later models. */ const usInference = { 'inference_geo:us': 1100 }; interface IAnthropicPriceEntry { modelId: string; aliases?: readonly string[]; standard: plugins.IModelServicePrice; /** Fast mode (`speed: "fast"`), on the models the page prices it for. */ fast?: plugins.IModelServicePrice; /** Whether the model takes `inference_geo` (Claude 4.6 and later). */ inferenceGeo: boolean; } const anthropicPrice = (entryArg: IAnthropicPriceEntry): plugins.IModelPrice => ({ providerId: 'anthropic', modelId: entryArg.modelId, ...(entryArg.aliases ? { aliases: entryArg.aliases } : {}), services: { standard: entryArg.standard, ...(entryArg.fast ? { fast: entryArg.fast } : {}) }, toolCallFees, ...(entryArg.inferenceGeo ? { surcharges: usInference } : {}), source: pricingSource, }); const deepFreeze = (valueArg: T): T => { if (typeof valueArg === 'object' && valueArg !== null && !Object.isFrozen(valueArg)) { Object.freeze(valueArg); for (const entry of Object.values(valueArg)) deepFreeze(entry); } return valueArg; }; /** * The Claude API prices of the models Anthropic lists as available, from * https://platform.claude.com/docs/en/about-claude/pricing, checked 2026-10-21, keyed by the service the response * reports: `standard`, and `fast` for Fast mode. Priority Tier has no published per-token price, so it is absent and a * call served in it is refused. Fast-mode cache rates apply the page's caching multipliers to the Fast-mode base * price (1.25× and 2× for writes; 0.05× hits on Claude Opus 5.5, 0.1× otherwise). Model ids and dated snapshots are * from the model pages (https://platform.claude.com/docs/en/models//overview). */ export const ANTHROPIC_MODEL_PRICES: readonly plugins.IModelPrice[] = deepFreeze([ anthropicPrice({ modelId: 'claude-fable-5-1', standard: flat([10, 12.5, 20, 0.25, 50]), inferenceGeo: true }), anthropicPrice({ modelId: 'claude-opus-5-5', standard: flat([4, 5, 8, 0.2, 20]), fast: flat([8, 10, 16, 0.4, 40]), inferenceGeo: true, }), anthropicPrice({ modelId: 'claude-sonnet-5-5', standard: flat([2, 2.5, 4, 0.1, 10]), inferenceGeo: true }), anthropicPrice({ modelId: 'claude-haiku-5-5', // Priced by prompt length: a prompt over 100,000 tokens, cache reads and writes included, pays the higher prices. standard: { bands: [ { maxPromptTokens: 100_000, rates: rates([0.1, 0.125, 0.2, 0.01, 0.5]) }, { rates: rates([0.5, 0.625, 1, 0.05, 2.5]) }, ] }, inferenceGeo: true, }), anthropicPrice({ modelId: 'claude-fable-5', standard: flat([10, 12.5, 20, 1, 50]), inferenceGeo: true }), anthropicPrice({ modelId: 'claude-opus-5', standard: flat([5, 6.25, 10, 0.5, 25]), fast: flat([10, 12.5, 20, 1, 50]), inferenceGeo: true, }), anthropicPrice({ modelId: 'claude-opus-4-8', standard: flat([5, 6.25, 10, 0.5, 25]), fast: flat([10, 12.5, 20, 1, 50]), inferenceGeo: true, }), anthropicPrice({ modelId: 'claude-opus-4-7', standard: flat([5, 6.25, 10, 0.5, 25]), inferenceGeo: true }), anthropicPrice({ modelId: 'claude-opus-4-6', standard: flat([5, 6.25, 10, 0.5, 25]), inferenceGeo: true }), anthropicPrice({ modelId: 'claude-opus-4-5', aliases: ['claude-opus-4-5-20251101'], standard: flat([5, 6.25, 10, 0.5, 25]), inferenceGeo: false, }), anthropicPrice({ modelId: 'claude-sonnet-5', standard: flat([2, 2.5, 4, 0.2, 10]), inferenceGeo: true }), anthropicPrice({ modelId: 'claude-sonnet-4-6', standard: flat([3, 3.75, 6, 0.3, 15]), inferenceGeo: true }), anthropicPrice({ modelId: 'claude-sonnet-4-5', aliases: ['claude-sonnet-4-5-20250929'], standard: flat([3, 3.75, 6, 0.3, 15]), inferenceGeo: false, }), anthropicPrice({ modelId: 'claude-haiku-4-5', aliases: ['claude-haiku-4-5-20251001'], standard: flat([1, 1.25, 2, 0.1, 5]), inferenceGeo: false, }), ] satisfies plugins.IModelPrice[]); /** The price of exactly this model id or one of its listed snapshots; `undefined` for any other id. */ export function getAnthropicModelPrice(modelIdArg: string): plugins.IModelPrice | undefined { return ANTHROPIC_MODEL_PRICES.find((price) => price.modelId === modelIdArg || (price.aliases ?? []).includes(modelIdArg)); } // --------------------------------------------------------------------------------------------------------------- // Server tools /** What one server-tool `type` of a Messages request costs beyond the model's tokens. */ export interface IAnthropicServerToolPrice { /** The exact `type` of the tool definition in the request's `tools`. */ type: string; /** * Integer micro-USD per call, by the name `usage.server_tool_use` counts the calls under (`web_search` for * `web_search_requests`), for every call a request declaring this tool may be billed for. `{}` is a tool that * costs only its tokens and has no counter. */ toolCallFees: Readonly>; source: plugins.IModelSource; } const serverToolsCheckedAt = '2026-10-22'; const serverToolPricingSource: plugins.IModelSource = { url: pricingSource.url, checkedAt: serverToolsCheckedAt }; const toolSearchSource: plugins.IModelSource = { url: 'https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool', checkedAt: serverToolsCheckedAt, }; /** * With `web_search_20260209`, `web_fetch_20260209` or a later version in the request, code execution in that request * (dynamic filtering and code Claude runs directly) costs nothing beyond its tokens. */ const freeCodeExecution = { code_execution: 0 }; /** * The server tools whose cost is published, by exact `type`: every version of web search and web fetch the tool * reference lists (https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-reference), at the fees of * https://platform.claude.com/docs/en/about-claude/pricing ($10 per 1,000 searches, no fee for fetches, free code * execution with the 2026 versions), and the dated tool search variants, which are not metered and cost only the * tool definitions they load, as input tokens (the tool search page's "Usage"), all checked 2026-10-22. Undated * aliases (`tool_search_tool_regex`, which resolves to the latest version), code execution (billed by container * time), the advisor (billed at the advisor model's token rates) and the MCP connector are absent, so a request * declaring them cannot be priced. */ export const ANTHROPIC_SERVER_TOOL_PRICES: readonly IAnthropicServerToolPrice[] = deepFreeze([ { type: 'web_search_20250305', toolCallFees: { web_search: toolCallFees.web_search }, source: serverToolPricingSource }, { type: 'web_search_20260209', toolCallFees: { web_search: toolCallFees.web_search, ...freeCodeExecution }, source: serverToolPricingSource }, { type: 'web_search_20260318', toolCallFees: { web_search: toolCallFees.web_search, ...freeCodeExecution }, source: serverToolPricingSource }, { type: 'web_fetch_20250910', toolCallFees: { web_fetch: toolCallFees.web_fetch }, source: serverToolPricingSource }, { type: 'web_fetch_20260209', toolCallFees: { web_fetch: toolCallFees.web_fetch, ...freeCodeExecution }, source: serverToolPricingSource }, { type: 'web_fetch_20260309', toolCallFees: { web_fetch: toolCallFees.web_fetch, ...freeCodeExecution }, source: serverToolPricingSource }, { type: 'web_fetch_20260318', toolCallFees: { web_fetch: toolCallFees.web_fetch, ...freeCodeExecution }, source: serverToolPricingSource }, { type: 'tool_search_tool_regex_20251119', toolCallFees: {}, source: toolSearchSource }, { type: 'tool_search_tool_bm25_20251119', toolCallFees: {}, source: toolSearchSource }, ]); /** The price of exactly this server-tool `type`; `undefined` for any other type, which a request cannot be priced with. */ export function getAnthropicServerToolPrice(typeArg: string): IAnthropicServerToolPrice | undefined { return ANTHROPIC_SERVER_TOOL_PRICES.find((tool) => tool.type === typeArg); } /** * The price of one request's answers: the model's price with the call fees of the server tools the request declares * (`serverToolTypesArg`, the `type` of each server tool in its `tools`; client tools are not passed). `undefined` * when one of the types has no price, so the request cannot be priced. */ export function getAnthropicRequestPrice( priceArg: plugins.IModelPrice, serverToolTypesArg: readonly string[], ): plugins.IModelPrice | undefined { const fees: Record = { ...priceArg.toolCallFees }; for (const type of serverToolTypesArg) { const tool = getAnthropicServerToolPrice(type); if (!tool) return undefined; for (const [name, fee] of Object.entries(tool.toolCallFees)) { if (Object.prototype.hasOwnProperty.call(fees, name) && fees[name] !== fee) { throw new Error(`Anthropic prices disagree on the ${name} call fee of ${priceArg.modelId} and ${type}.`); } fees[name] = fee; } } return { ...priceArg, toolCallFees: fees }; } const isRecord = (valueArg: unknown): valueArg is Record => typeof valueArg === 'object' && valueArg !== null && !Array.isArray(valueArg); const tokenCount = (valueArg: unknown, nameArg: string): number => { if (!Number.isSafeInteger(valueArg) || (valueArg as number) < 0) { throw new plugins.ModelUsageReadError(`Anthropic usage ${nameArg} is not a count.`); } return valueArg as number; }; const optionalTokenCount = (valueArg: unknown, nameArg: string): number => valueArg === undefined || valueArg === null ? 0 : tokenCount(valueArg, nameArg); const optionalRecord = (valueArg: unknown, nameArg: string): Record | undefined => { if (valueArg === undefined || valueArg === null) return undefined; if (!isRecord(valueArg)) throw new plugins.ModelUsageReadError(`Anthropic usage ${nameArg} is not an object.`); return valueArg; }; /** * The priced service of a tier and speed: `standard` at standard speed, `fast` for Fast mode on the standard tier. * Any other speed names a service no price has (`:speed:`), so the call is refused as unpriced. */ const serviceOf = (tierArg: string, speedArg: string | undefined): string => { if (speedArg === undefined || speedArg === 'standard') return tierArg; if (speedArg === 'fast' && tierArg === 'standard') return 'fast'; return `${tierArg}:speed:${speedArg}`; }; /** * Accepts `usage.iterations` only when every entry is a `message` iteration, whose tokens the top-level counts already * sum. Any other iteration, such as an advisor's `advisor_message`, is billed at rates of its own and left out of the * top-level counts (https://platform.claude.com/docs/en/agents-and-tools/tool-use/advisor-tool), so the usage * cannot be read completely. */ const readIterations = (valueArg: unknown): void => { if (valueArg === undefined || valueArg === null) return; if (!Array.isArray(valueArg)) throw new plugins.ModelUsageReadError('Anthropic usage iterations is not a list.'); for (const iteration of valueArg) { if (!isRecord(iteration) || typeof iteration.type !== 'string') { throw new plugins.ModelUsageReadError('Anthropic usage names an unreadable iteration.'); } if (iteration.type !== 'message') { throw new plugins.ModelUsageReadError(`Anthropic usage has a ${iteration.type} iteration, which its top-level counts leave out.`); } } }; /** * Reads a Messages API `usage` object, raw as the API sends it at the end of a call (a stream's usage merged from * `message_start` and the last `message_delta`). * * `input_tokens` excludes the cache; cache writes are split by lifetime from `cache_creation` * (`ephemeral_5m_input_tokens`, `ephemeral_1h_input_tokens`), and writes it does not split go to * `unknownLifetimeCacheWriteTokens`. The service is `service_tier`, or `fast` for a standard-tier call whose `speed` * is `fast`; any other speed names a service no price has. An `inference_geo` other than `global` is the surcharge * `inference_geo:`, which a price without that surcharge refuses. Server-tool calls come * from `server_tool_use` (`web_search_requests` as `web_search`, and so on). A usage that is not complete throws a * `ModelUsageReadError`, and so does one with a `usage.iterations` entry other than `message` (an advisor's * `advisor_message`, for example), whose tokens the top-level counts leave out. */ export function readAnthropicMessagesUsage(rawUsageArg: unknown): plugins.IModelPricedUsage { if (!isRecord(rawUsageArg)) throw new plugins.ModelUsageReadError('Anthropic usage is not an object.'); const serviceTier = rawUsageArg.service_tier; if (typeof serviceTier !== 'string' || serviceTier.length === 0) { throw new plugins.ModelUsageReadError('Anthropic usage names no service tier.'); } const speed = rawUsageArg.speed; if (speed !== undefined && speed !== null && (typeof speed !== 'string' || speed.length === 0)) { throw new plugins.ModelUsageReadError('Anthropic usage names an unreadable speed.'); } const writeTotal = optionalTokenCount(rawUsageArg.cache_creation_input_tokens, 'cache_creation_input_tokens'); const cacheCreation = optionalRecord(rawUsageArg.cache_creation, 'cache_creation'); const cacheWriteTokens: Record = {}; let unsplitWrites = writeTotal; if (cacheCreation) { const write5m = optionalTokenCount(cacheCreation.ephemeral_5m_input_tokens, 'ephemeral_5m_input_tokens'); const write1h = optionalTokenCount(cacheCreation.ephemeral_1h_input_tokens, 'ephemeral_1h_input_tokens'); unsplitWrites = writeTotal - write5m - write1h; if (unsplitWrites < 0) throw new plugins.ModelUsageReadError('Anthropic usage splits more cache writes than it counts.'); if (write5m > 0) cacheWriteTokens['5m'] = write5m; if (write1h > 0) cacheWriteTokens['1h'] = write1h; } const toolCalls: Record = {}; const serverToolUse = optionalRecord(rawUsageArg.server_tool_use, 'server_tool_use'); for (const [name, count] of Object.entries(serverToolUse ?? {})) { if (!name.endsWith('_requests')) continue; const calls = optionalTokenCount(count, name); if (calls > 0) toolCalls[name.slice(0, -'_requests'.length)] = calls; } const geo = rawUsageArg.inference_geo; if (geo !== undefined && geo !== null && (typeof geo !== 'string' || geo.length === 0)) { throw new plugins.ModelUsageReadError('Anthropic usage names an unreadable inference_geo.'); } readIterations(rawUsageArg.iterations); return { service: serviceOf(serviceTier, typeof speed === 'string' ? speed : undefined), uncachedInputTokens: tokenCount(rawUsageArg.input_tokens, 'input_tokens'), cacheReadTokens: optionalTokenCount(rawUsageArg.cache_read_input_tokens, 'cache_read_input_tokens'), cacheWriteTokens, ...(unsplitWrites > 0 ? { unknownLifetimeCacheWriteTokens: unsplitWrites } : {}), outputTokens: tokenCount(rawUsageArg.output_tokens, 'output_tokens'), ...(Object.keys(toolCalls).length > 0 ? { toolCalls } : {}), ...(typeof geo === 'string' && geo !== 'global' ? { surcharges: [`inference_geo:${geo}`] } : {}), }; }