Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Helicone Helicone CostOf

From Leeroopedia
Knowledge Sources
Domains Cost Calculation, LLM Observability, Pricing Registry
Last Updated 2026-02-14 00:00 GMT

Overview

Concrete function for looking up per-token cost rates for a model/provider pair, provided by the @helicone/cost package.

Description

The costOf function accepts a model name and provider identifier and returns the cost rate object (ModelRow["cost"]) for that combination, or null if no matching rate is found. The function performs a case-insensitive lookup through two stages:

  1. Provider resolution: Finds the provider entry by comparing the provider argument against both the provider name string (exact match) and the pattern regex of each registered provider.
  2. Model resolution: Within the matched provider's costs array, finds the cost entry whose model identifier matches using the entry's operator: equals (case-insensitive exact match), startsWith (lowercase prefix match), or includes (lowercase substring match).

If the model string is falsy (null, undefined, or empty), the function returns null immediately.

Usage

Use this function when you need raw per-token cost rates without computing a total cost. It is the building block used by costOfPrompt and the ClickHouse SQL generator.

Code Reference

Source Location

  • Repository: Helicone
  • File: packages/cost/index.ts (lines 23-59)

Signature

export function costOf({
  model,
  provider,
}: {
  model: string;
  provider: string;
}): ModelRow["cost"] | null

Import

import { costOf } from "@helicone/cost";

I/O Contract

Inputs

Name Type Required Description
model string Yes The model identifier (e.g., "gpt-4o", "claude-sonnet-4-20250514"). Matched case-insensitively.
provider string Yes The provider name (e.g., "OPENAI", "ANTHROPIC") or a provider URL that will be tested against registered patterns.

Outputs

Name Type Description
return ModelRow["cost"] ¦ null The cost rate object containing per-token prices, or null if no match is found.

Cost Rate Object Fields

Field Type Description
prompt_token number Cost per prompt/input token in USD
completion_token number Cost per completion/output token in USD
prompt_cache_write_token number ¦ undefined Cost per cache write token (Anthropic: sum of 5m + 1h)
prompt_cache_read_token number ¦ undefined Cost per cache read token
prompt_audio_token number ¦ undefined Cost per input audio token
completion_audio_token number ¦ undefined Cost per output audio token
per_image number ¦ undefined Cost per image
per_call number ¦ undefined Fixed cost per API call
prompt_cache_creation_5m number ¦ undefined Anthropic 5-minute cache creation cost per token
prompt_cache_creation_1h number ¦ undefined Anthropic 1-hour cache creation cost per token

Usage Examples

Basic Usage

import { costOf } from "@helicone/cost";

const rates = costOf({
  model: "gpt-4o",
  provider: "OPENAI",
});

if (rates) {
  console.log("Prompt token cost:", rates.prompt_token);
  console.log("Completion token cost:", rates.completion_token);
}

Provider URL Matching

// The provider argument can also be a URL
const rates = costOf({
  model: "claude-sonnet-4-20250514",
  provider: "https://api.anthropic.com",
});

if (rates) {
  console.log("Has cache pricing:", !!rates.prompt_cache_write_token);
}

Null Handling

const rates = costOf({
  model: "unknown-model-xyz",
  provider: "OPENAI",
});

// Returns null when model is not found in the provider's cost table
if (!rates) {
  console.log("No pricing data available for this model");
}

Related Pages

Implements Principle

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment