Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

feat(pricing): support cacheReadPrice and cacheCreationPrice in pricing JSON (#1286)

citizen2041e7e68d47582
diff --git a/assets/pricing.json b/assets/pricing.json
index 76610818..6aeabdcb 100644
--- a/assets/pricing.json
+++ b/assets/pricing.json
@@ -247,91 +247,135 @@
},
"claude-3-opus-20240229": {
"promptPrice": 0.015,
- "completionPrice": 0.075
+ "completionPrice": 0.075,
+ "cacheReadPrice": 0.0015,
+ "cacheCreationPrice": 0.01875
},
"claude-3-sonnet-20240229": {
"promptPrice": 0.003,
- "completionPrice": 0.015
+ "completionPrice": 0.015,
+ "cacheReadPrice": 0.0003,
+ "cacheCreationPrice": 0.00375
},
"claude-3-haiku-20240307": {
"promptPrice": 0.00025,
- "completionPrice": 0.00125
+ "completionPrice": 0.00125,
+ "cacheReadPrice": 0.00003,
+ "cacheCreationPrice": 0.0003
},
"claude-3-5-sonnet-20240620": {
"promptPrice": 0.003,
- "completionPrice": 0.015
+ "completionPrice": 0.015,
+ "cacheReadPrice": 0.0003,
+ "cacheCreationPrice": 0.00375
},
"claude-3-5-sonnet-20241022": {
"promptPrice": 0.003,
- "completionPrice": 0.015
+ "completionPrice": 0.015,
+ "cacheReadPrice": 0.0003,
+ "cacheCreationPrice": 0.00375
},
"claude-3-5-haiku-20241022": {
"promptPrice": 0.0008,
- "completionPrice": 0.004
+ "completionPrice": 0.004,
+ "cacheReadPrice": 0.00008,
+ "cacheCreationPrice": 0.001
},
"claude-3-7-sonnet-20250219": {
"promptPrice": 0.003,
- "completionPrice": 0.015
+ "completionPrice": 0.015,
+ "cacheReadPrice": 0.0003,
+ "cacheCreationPrice": 0.00375
},
"claude-haiku-4-5-20251001": {
"promptPrice": 0.001,
- "completionPrice": 0.005
+ "completionPrice": 0.005,
+ "cacheReadPrice": 0.0001,
+ "cacheCreationPrice": 0.00125
},
"claude-haiku-4-5": {
"promptPrice": 0.001,
- "completionPrice": 0.005
+ "completionPrice": 0.005,
+ "cacheReadPrice": 0.0001,
+ "cacheCreationPrice": 0.00125
},
"claude-sonnet-4-0": {
"promptPrice": 0.003,
- "completionPrice": 0.015
+ "completionPrice": 0.015,
+ "cacheReadPrice": 0.0003,
+ "cacheCreationPrice": 0.00375
},
"claude-sonnet-4-5": {
"promptPrice": 0.003,
- "completionPrice": 0.015
+ "completionPrice": 0.015,
+ "cacheReadPrice": 0.0003,
+ "cacheCreationPrice": 0.00375
},
"claude-sonnet-4-6": {
"promptPrice": 0.003,
- "completionPrice": 0.015
+ "completionPrice": 0.015,
+ "cacheReadPrice": 0.0003,
+ "cacheCreationPrice": 0.00375
},
"claude-sonnet-4-7": {
"promptPrice": 0.003,
- "completionPrice": 0.015
+ "completionPrice": 0.015,
+ "cacheReadPrice": 0.0003,
+ "cacheCreationPrice": 0.00375
},
"claude-opus-4-0": {
"promptPrice": 0.015,
- "completionPrice": 0.075
+ "completionPrice": 0.075,
+ "cacheReadPrice": 0.0015,
+ "cacheCreationPrice": 0.01875
},
"claude-opus-4-1": {
"promptPrice": 0.015,
- "completionPrice": 0.075
+ "completionPrice": 0.075,
+ "cacheReadPrice": 0.0015,
+ "cacheCreationPrice": 0.01875
},
"claude-opus-4-5": {
"promptPrice": 0.005,
- "completionPrice": 0.025
+ "completionPrice": 0.025,
+ "cacheReadPrice": 0.0005,
+ "cacheCreationPrice": 0.00625
},
"claude-opus-4-6": {
"promptPrice": 0.005,
- "completionPrice": 0.025
+ "completionPrice": 0.025,
+ "cacheReadPrice": 0.0005,
+ "cacheCreationPrice": 0.00625
},
"claude-opus-4-7": {
"promptPrice": 0.005,
- "completionPrice": 0.025
+ "completionPrice": 0.025,
+ "cacheReadPrice": 0.0005,
+ "cacheCreationPrice": 0.00625
},
"claude-opus-4-7-thinking-xhigh": {
"promptPrice": 0.005,
- "completionPrice": 0.025
+ "completionPrice": 0.025,
+ "cacheReadPrice": 0.0005,
+ "cacheCreationPrice": 0.00625
},
"claude-opus-4-7-thinking-high": {
"promptPrice": 0.005,
- "completionPrice": 0.025
+ "completionPrice": 0.025,
+ "cacheReadPrice": 0.0005,
+ "cacheCreationPrice": 0.00625
},
"claude-opus-4-7-thinking-medium": {
"promptPrice": 0.005,
- "completionPrice": 0.025
+ "completionPrice": 0.025,
+ "cacheReadPrice": 0.0005,
+ "cacheCreationPrice": 0.00625
},
"claude-opus-4-7-thinking-low": {
"promptPrice": 0.005,
- "completionPrice": 0.025
+ "completionPrice": 0.025,
+ "cacheReadPrice": 0.0005,
+ "cacheCreationPrice": 0.00625
},
"gpt-5": {
"promptPrice": 0.00125,
diff --git a/sdk/python/src/openlit/__helpers.py b/sdk/python/src/openlit/__helpers.py
index 4bbf287f..f6558693 100644
--- a/sdk/python/src/openlit/__helpers.py
+++ b/sdk/python/src/openlit/__helpers.py
@@ -281,9 +281,38 @@ def general_tokens(text):
return math.ceil(len(text) / 2)
-def get_chat_model_cost(model, pricing_info, prompt_tokens, completion_tokens):
+def get_chat_model_cost(
+ model,
+ pricing_info,
+ prompt_tokens,
+ completion_tokens,
+ cache_read_tokens=0,
+ cache_creation_tokens=0,
+ prompt_tokens_include_cache=False,
+):
"""
Retrieve the cost of processing for a given model based on prompt and tokens.
+
+ Cache-aware pricing:
+ When the model's pricing entry defines ``cacheReadPrice`` /
+ ``cacheCreationPrice``, the matching cache tokens are billed at those
+ rates instead of the regular prompt price.
+
+ Token accounting differs across providers:
+ * Some providers (e.g. the Anthropic native API) report
+ ``prompt_tokens`` *exclusive* of cached tokens, so the cache tokens
+ must simply be added on top -> keep ``prompt_tokens_include_cache``
+ ``False`` (the default).
+ * Others (e.g. LangChain's normalized ``usage_metadata``) report
+ ``prompt_tokens`` as the *sum of all* input token types, already
+ including cache read / creation tokens. Pass
+ ``prompt_tokens_include_cache=True`` so the re-priced cache tokens are
+ subtracted from the prompt base and never billed twice.
+
+ Cache tokens are only ever re-priced (and, when inclusive, only ever
+ subtracted) when a dedicated cache price exists. This keeps the result
+ identical to the legacy behaviour for every model that has no cache pricing
+ configured, regardless of the token-accounting convention.
"""
try:
@@ -293,8 +322,33 @@ def get_chat_model_cost(model, pricing_info, prompt_tokens, completion_tokens):
model_pricing = chat_pricing.get(model.split("/", 1)[1])
if model_pricing is None:
return 0
- cost = ((prompt_tokens / 1000) * model_pricing["promptPrice"]) + (
- (completion_tokens / 1000) * model_pricing["completionPrice"]
+
+ cache_read_tokens = cache_read_tokens or 0
+ cache_creation_tokens = cache_creation_tokens or 0
+
+ billable_prompt_tokens = prompt_tokens
+ cache_cost = 0.0
+
+ if "cacheReadPrice" in model_pricing:
+ cache_cost += (cache_read_tokens / 1000) * (
+ model_pricing["cacheReadPrice"] or 0
+ )
+ if prompt_tokens_include_cache:
+ billable_prompt_tokens -= cache_read_tokens
+
+ if "cacheCreationPrice" in model_pricing:
+ cache_cost += (cache_creation_tokens / 1000) * (
+ model_pricing["cacheCreationPrice"] or 0
+ )
+ if prompt_tokens_include_cache:
+ billable_prompt_tokens -= cache_creation_tokens
+
+ billable_prompt_tokens = max(billable_prompt_tokens, 0)
+
+ cost = (
+ (billable_prompt_tokens / 1000) * model_pricing["promptPrice"]
+ + (completion_tokens / 1000) * model_pricing["completionPrice"]
+ + cache_cost
)
except Exception:
cost = 0
diff --git a/sdk/python/src/openlit/instrumentation/anthropic/utils.py b/sdk/python/src/openlit/instrumentation/anthropic/utils.py
index 7efdc6fc..ebb0a66d 100644
--- a/sdk/python/src/openlit/instrumentation/anthropic/utils.py
+++ b/sdk/python/src/openlit/instrumentation/anthropic/utils.py
@@ -519,7 +519,17 @@ def common_chat_logic(
input_tokens = general_tokens(prompt)
output_tokens = general_tokens(scope._llmresponse)
- cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
+ # Anthropic's API reports input_tokens exclusive of cache read / creation
+ # tokens, so the cache tokens are added on top (prompt_tokens_include_cache
+ # stays False).
+ cost = get_chat_model_cost(
+ request_model,
+ pricing_info,
+ input_tokens,
+ output_tokens,
+ cache_read_tokens=getattr(scope, "_cache_read_input_tokens", 0),
+ cache_creation_tokens=getattr(scope, "_cache_creation_input_tokens", 0),
+ )
# Common Span Attributes
common_span_attributes(
diff --git a/sdk/python/src/openlit/instrumentation/langchain/utils.py b/sdk/python/src/openlit/instrumentation/langchain/utils.py
index bbc49912..302850ae 100644
--- a/sdk/python/src/openlit/instrumentation/langchain/utils.py
+++ b/sdk/python/src/openlit/instrumentation/langchain/utils.py
@@ -374,7 +374,18 @@ def common_chat_logic(
input_tokens = general_tokens(prompt)
output_tokens = general_tokens(scope._llmresponse)
- cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
+ # LangChain's normalized usage_metadata reports input_tokens as the sum of
+ # all input token types (uncached + cache read + cache creation), so flag
+ # the prompt tokens as cache-inclusive to avoid billing cached tokens twice.
+ cost = get_chat_model_cost(
+ request_model,
+ pricing_info,
+ input_tokens,
+ output_tokens,
+ cache_read_tokens=getattr(scope, "_cache_read_input_tokens", 0),
+ cache_creation_tokens=getattr(scope, "_cache_creation_input_tokens", 0),
+ prompt_tokens_include_cache=True,
+ )
provider = (
getattr(scope, "_provider", None) or SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN
diff --git a/sdk/python/src/openlit/instrumentation/openai/utils.py b/sdk/python/src/openlit/instrumentation/openai/utils.py
index b2eba3e4..3c214684 100644
--- a/sdk/python/src/openlit/instrumentation/openai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/openai/utils.py
@@ -873,7 +873,19 @@ def common_response_logic(
input_tokens = general_tokens(prompt)
output_tokens = general_tokens(scope._llmresponse)
- cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
+ # OpenAI reports prompt_tokens inclusive of cached (cache read) tokens, so
+ # flag the prompt tokens as cache-inclusive to avoid billing cached tokens
+ # twice once a model defines cacheReadPrice. OpenAI has no cache-creation
+ # charge, so that count stays 0 unless a provider sets it.
+ cost = get_chat_model_cost(
+ request_model,
+ pricing_info,
+ input_tokens,
+ output_tokens,
+ cache_read_tokens=getattr(scope, "_cache_read_input_tokens", 0),
+ cache_creation_tokens=getattr(scope, "_cache_creation_input_tokens", 0),
+ prompt_tokens_include_cache=True,
+ )
# Common Span Attributes
common_span_attributes(
@@ -1366,7 +1378,19 @@ def common_chat_logic(
input_tokens = general_tokens(prompt)
output_tokens = general_tokens(scope._llmresponse)
- cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
+ # OpenAI reports prompt_tokens inclusive of cached (cache read) tokens, so
+ # flag the prompt tokens as cache-inclusive to avoid billing cached tokens
+ # twice once a model defines cacheReadPrice. OpenAI has no cache-creation
+ # charge, so that count stays 0 unless a provider sets it.
+ cost = get_chat_model_cost(
+ request_model,
+ pricing_info,
+ input_tokens,
+ output_tokens,
+ cache_read_tokens=getattr(scope, "_cache_read_input_tokens", 0),
+ cache_creation_tokens=getattr(scope, "_cache_creation_input_tokens", 0),
+ prompt_tokens_include_cache=True,
+ )
# Common Span Attributes
common_span_attributes(
diff --git a/sdk/python/tests/test_cache_pricing.py b/sdk/python/tests/test_cache_pricing.py
new file mode 100644
index 00000000..2873f31d
--- /dev/null
+++ b/sdk/python/tests/test_cache_pricing.py
@@ -0,0 +1,212 @@
+# pylint: disable=missing-function-docstring, missing-class-docstring, too-few-public-methods
+"""
+Unit tests for cache-aware cost calculation in ``get_chat_model_cost``.
+
+These cover the two token-accounting conventions used by the instrumentations
+that forward cache token counts:
+
+* Anthropic native API -> ``prompt_tokens`` is *exclusive* of cache tokens.
+* LangChain ``usage_metadata`` -> ``prompt_tokens`` is *inclusive* of cache
+ tokens (sum of all input token types).
+
+The same cached tokens must never be billed twice, and models without cache
+pricing must produce exactly the legacy cost regardless of convention.
+"""
+
+import pytest
+
+from openlit.__helpers import get_chat_model_cost
+
+
+PRICING_WITH_CACHE = {
+ "chat": {
+ "claude-3-5-sonnet-20241022": {
+ "promptPrice": 0.003,
+ "completionPrice": 0.015,
+ "cacheReadPrice": 0.0003,
+ "cacheCreationPrice": 0.00375,
+ },
+ # A model that supports caching but has no cache pricing configured.
+ "model-no-cache-price": {
+ "promptPrice": 0.003,
+ "completionPrice": 0.015,
+ },
+ }
+}
+
+MODEL = "claude-3-5-sonnet-20241022"
+
+
+def _approx(value):
+ return pytest.approx(value, rel=1e-9)
+
+
+class TestNoCacheTokens:
+ def test_matches_legacy_formula_without_cache(self):
+ cost = get_chat_model_cost(MODEL, PRICING_WITH_CACHE, 1000, 500)
+ assert cost == _approx((1000 / 1000) * 0.003 + (500 / 1000) * 0.015)
+
+ def test_unknown_model_returns_zero(self):
+ assert get_chat_model_cost("does-not-exist", PRICING_WITH_CACHE, 10, 10) == 0
+
+
+class TestAnthropicConvention:
+ """prompt_tokens is exclusive of cache tokens (default behaviour)."""
+
+ def test_cache_added_on_top(self):
+ # 800 uncached input + 100 cache read + 100 cache creation, 500 output
+ cost = get_chat_model_cost(
+ MODEL,
+ PRICING_WITH_CACHE,
+ prompt_tokens=800,
+ completion_tokens=500,
+ cache_read_tokens=100,
+ cache_creation_tokens=100,
+ )
+ expected = (
+ (800 / 1000) * 0.003
+ + (500 / 1000) * 0.015
+ + (100 / 1000) * 0.0003
+ + (100 / 1000) * 0.00375
+ )
+ assert cost == _approx(expected)
+
+ def test_no_cache_price_leaves_prompt_untouched(self):
+ # Without cache pricing the cache tokens contribute nothing and the
+ # prompt base is not reduced (legacy behaviour preserved).
+ cost = get_chat_model_cost(
+ "model-no-cache-price",
+ PRICING_WITH_CACHE,
+ prompt_tokens=800,
+ completion_tokens=500,
+ cache_read_tokens=100,
+ cache_creation_tokens=100,
+ )
+ assert cost == _approx((800 / 1000) * 0.003 + (500 / 1000) * 0.015)
+
+
+class TestLangChainConvention:
+ """prompt_tokens already includes cache tokens (sum of all input types)."""
+
+ def test_cache_tokens_not_billed_twice(self):
+ # 1000 total input = 800 uncached + 100 cache read + 100 cache creation
+ cost = get_chat_model_cost(
+ MODEL,
+ PRICING_WITH_CACHE,
+ prompt_tokens=1000,
+ completion_tokens=500,
+ cache_read_tokens=100,
+ cache_creation_tokens=100,
+ prompt_tokens_include_cache=True,
+ )
+ expected = (
+ (800 / 1000) * 0.003 # only the uncached remainder at prompt price
+ + (500 / 1000) * 0.015
+ + (100 / 1000) * 0.0003
+ + (100 / 1000) * 0.00375
+ )
+ assert cost == _approx(expected)
+
+ def test_inclusive_matches_exclusive_for_same_call(self):
+ # Inclusive(1000) and exclusive(800) describe the same request and must
+ # yield identical cost.
+ inclusive = get_chat_model_cost(
+ MODEL,
+ PRICING_WITH_CACHE,
+ prompt_tokens=1000,
+ completion_tokens=500,
+ cache_read_tokens=100,
+ cache_creation_tokens=100,
+ prompt_tokens_include_cache=True,
+ )
+ exclusive = get_chat_model_cost(
+ MODEL,
+ PRICING_WITH_CACHE,
+ prompt_tokens=800,
+ completion_tokens=500,
+ cache_read_tokens=100,
+ cache_creation_tokens=100,
+ )
+ assert inclusive == _approx(exclusive)
+
+ def test_no_cache_price_matches_legacy_total(self):
+ # When no cache pricing exists, an inclusive caller must get exactly the
+ # legacy cost (full input billed at prompt price), i.e. no regression.
+ cost = get_chat_model_cost(
+ "model-no-cache-price",
+ PRICING_WITH_CACHE,
+ prompt_tokens=1000,
+ completion_tokens=500,
+ cache_read_tokens=100,
+ cache_creation_tokens=100,
+ prompt_tokens_include_cache=True,
+ )
+ assert cost == _approx((1000 / 1000) * 0.003 + (500 / 1000) * 0.015)
+
+ def test_openai_style_cache_read_only(self):
+ # OpenAI reports prompt_tokens inclusive of cached_tokens and has no
+ # cache-creation charge.
+ pricing = {
+ "chat": {
+ "gpt-cache": {
+ "promptPrice": 0.0025,
+ "completionPrice": 0.01,
+ "cacheReadPrice": 0.00125,
+ }
+ }
+ }
+ cost = get_chat_model_cost(
+ "gpt-cache",
+ pricing,
+ prompt_tokens=1000,
+ completion_tokens=200,
+ cache_read_tokens=400,
+ cache_creation_tokens=0,
+ prompt_tokens_include_cache=True,
+ )
+ expected = (600 / 1000) * 0.0025 + (200 / 1000) * 0.01 + (400 / 1000) * 0.00125
+ assert cost == _approx(expected)
+
+ def test_cache_exceeding_prompt_does_not_go_negative(self):
+ # Defensive: malformed data where cache tokens exceed reported input.
+ cost = get_chat_model_cost(
+ MODEL,
+ PRICING_WITH_CACHE,
+ prompt_tokens=100,
+ completion_tokens=0,
+ cache_read_tokens=200,
+ cache_creation_tokens=200,
+ prompt_tokens_include_cache=True,
+ )
+ # billable prompt clamps to 0; only cache costs remain.
+ expected = (200 / 1000) * 0.0003 + (200 / 1000) * 0.00375
+ assert cost == _approx(expected)
+
+
+class TestBundledPricingFile:
+ """Sanity-check the cache fields actually shipped in assets/pricing.json."""
+
+ def test_claude_cache_rates_present_and_consistent(self):
+ import json
+ import os
+
+ pricing_path = os.path.join(
+ os.path.dirname(__file__),
+ "..",
+ "..",
+ "..",
+ "assets",
+ "pricing.json",
+ )
+ if not os.path.exists(pricing_path):
+ pytest.skip("bundled pricing.json not available in this checkout")
+
+ with open(pricing_path, "r", encoding="utf-8") as handle:
+ pricing = json.load(handle)
+
+ entry = pricing["chat"]["claude-3-5-sonnet-20241022"]
+ assert entry["cacheReadPrice"] == _approx(0.0003)
+ assert entry["cacheCreationPrice"] == _approx(0.00375)
+ # cache read should be cheaper than prompt; cache write pricier.
+ assert entry["cacheReadPrice"] < entry["promptPrice"]
+ assert entry["cacheCreationPrice"] > entry["promptPrice"]
diff --git a/sdk/typescript/src/helpers.ts b/sdk/typescript/src/helpers.ts
index 0c4d8d4d..b976d26b 100644
--- a/sdk/typescript/src/helpers.ts
+++ b/sdk/typescript/src/helpers.ts
@@ -304,11 +304,32 @@ export default class OpenLitHelper {
return encoding.encode(text).length;
}
+ /**
+ * Compute chat completion cost, optionally accounting for prompt-cache tokens.
+ *
+ * When the model's pricing entry defines `cacheReadPrice` / `cacheCreationPrice`,
+ * the matching cache tokens are billed at those rates instead of the regular
+ * prompt price.
+ *
+ * Token accounting differs across providers:
+ * - Anthropic's native API reports `promptTokens` exclusive of cache tokens,
+ * so cache tokens are added on top (keep `promptTokensIncludeCache` false).
+ * - OpenAI / LangChain report `promptTokens` inclusive of cache read tokens,
+ * so pass `promptTokensIncludeCache: true` to subtract the re-priced cache
+ * tokens from the prompt base and avoid billing them twice.
+ *
+ * Cache tokens are only re-priced (and, when inclusive, only subtracted) when a
+ * dedicated cache price exists, so the result is identical to the legacy
+ * behaviour for any model without cache pricing configured.
+ */
static getChatModelCost(
model: string,
pricingInfo: any,
promptTokens: number,
- completionTokens: number
+ completionTokens: number,
+ cacheReadTokens: number = 0,
+ cacheCreationTokens: number = 0,
+ promptTokensIncludeCache: boolean = false
): number {
try {
const chatPricing = pricingInfo?.chat;
@@ -318,9 +339,34 @@ export default class OpenLitHelper {
modelPricing = chatPricing[model.split('/', 2)[1]];
}
if (modelPricing == null) return 0;
+
+ const cacheRead = cacheReadTokens || 0;
+ const cacheCreation = cacheCreationTokens || 0;
+
+ let billablePromptTokens = promptTokens;
+ let cacheCost = 0;
+
+ if (modelPricing.cacheReadPrice != null) {
+ cacheCost += (cacheRead / OpenLitHelper.PROMPT_TOKEN_FACTOR) * modelPricing.cacheReadPrice;
+ if (promptTokensIncludeCache) {
+ billablePromptTokens -= cacheRead;
+ }
+ }
+ if (modelPricing.cacheCreationPrice != null) {
+ cacheCost +=
+ (cacheCreation / OpenLitHelper.PROMPT_TOKEN_FACTOR) * modelPricing.cacheCreationPrice;
+ if (promptTokensIncludeCache) {
+ billablePromptTokens -= cacheCreation;
+ }
+ }
+ if (billablePromptTokens < 0) {
+ billablePromptTokens = 0;
+ }
+
const cost =
- (promptTokens / OpenLitHelper.PROMPT_TOKEN_FACTOR) * modelPricing.promptPrice +
- (completionTokens / OpenLitHelper.PROMPT_TOKEN_FACTOR) * modelPricing.completionPrice;
+ (billablePromptTokens / OpenLitHelper.PROMPT_TOKEN_FACTOR) * modelPricing.promptPrice +
+ (completionTokens / OpenLitHelper.PROMPT_TOKEN_FACTOR) * modelPricing.completionPrice +
+ cacheCost;
return isNaN(cost) ? 0 : cost;
} catch {
return 0;
diff --git a/sdk/typescript/src/instrumentation/__tests__/cache-pricing.test.ts b/sdk/typescript/src/instrumentation/__tests__/cache-pricing.test.ts
new file mode 100644
index 00000000..2e431f6c
--- /dev/null
+++ b/sdk/typescript/src/instrumentation/__tests__/cache-pricing.test.ts
@@ -0,0 +1,119 @@
+/**
+ * Unit tests for cache-aware cost calculation in `OpenLitHelper.getChatModelCost`.
+ *
+ * Mirrors the Python `test_cache_pricing.py` coverage and validates the two
+ * provider token-accounting conventions:
+ * - Anthropic native API -> promptTokens is exclusive of cache tokens.
+ * - OpenAI / LangChain -> promptTokens is inclusive of cache tokens.
+ *
+ * The same cached tokens must never be billed twice, and models without cache
+ * pricing must produce exactly the legacy cost regardless of convention.
+ */
+
+import OpenLitHelper from '../../helpers';
+
+const PRICING = {
+ chat: {
+ 'claude-3-5-sonnet-20241022': {
+ promptPrice: 0.003,
+ completionPrice: 0.015,
+ cacheReadPrice: 0.0003,
+ cacheCreationPrice: 0.00375,
+ },
+ 'model-no-cache-price': {
+ promptPrice: 0.003,
+ completionPrice: 0.015,
+ },
+ },
+};
+
+const MODEL = 'claude-3-5-sonnet-20241022';
+
+describe('OpenLitHelper.getChatModelCost — cache pricing', () => {
+ describe('no cache tokens', () => {
+ it('matches the legacy formula', () => {
+ const cost = OpenLitHelper.getChatModelCost(MODEL, PRICING, 1000, 500);
+ expect(cost).toBeCloseTo((1000 / 1000) * 0.003 + (500 / 1000) * 0.015, 10);
+ });
+
+ it('returns 0 for an unknown model', () => {
+ expect(OpenLitHelper.getChatModelCost('nope', PRICING, 10, 10)).toBe(0);
+ });
+
+ it('returns 0 when pricing info is missing', () => {
+ expect(OpenLitHelper.getChatModelCost(MODEL, {}, 10, 10)).toBe(0);
+ });
+ });
+
+ describe('Anthropic convention (promptTokens exclusive of cache)', () => {
+ it('adds cache cost on top of the prompt base', () => {
+ const cost = OpenLitHelper.getChatModelCost(MODEL, PRICING, 800, 500, 100, 100);
+ const expected =
+ (800 / 1000) * 0.003 +
+ (500 / 1000) * 0.015 +
+ (100 / 1000) * 0.0003 +
+ (100 / 1000) * 0.00375;
+ expect(cost).toBeCloseTo(expected, 10);
+ });
+
+ it('leaves the prompt base untouched when no cache price exists', () => {
+ const cost = OpenLitHelper.getChatModelCost(
+ 'model-no-cache-price',
+ PRICING,
+ 800,
+ 500,
+ 100,
+ 100
+ );
+ expect(cost).toBeCloseTo((800 / 1000) * 0.003 + (500 / 1000) * 0.015, 10);
+ });
+ });
+
+ describe('OpenAI / LangChain convention (promptTokens inclusive of cache)', () => {
+ it('does not bill cache tokens twice', () => {
+ // 1000 total input = 800 uncached + 100 cache read + 100 cache creation
+ const cost = OpenLitHelper.getChatModelCost(MODEL, PRICING, 1000, 500, 100, 100, true);
+ const expected =
+ (800 / 1000) * 0.003 +
+ (500 / 1000) * 0.015 +
+ (100 / 1000) * 0.0003 +
+ (100 / 1000) * 0.00375;
+ expect(cost).toBeCloseTo(expected, 10);
+ });
+
+ it('matches the exclusive call describing the same request', () => {
+ const inclusive = OpenLitHelper.getChatModelCost(MODEL, PRICING, 1000, 500, 100, 100, true);
+ const exclusive = OpenLitHelper.getChatModelCost(MODEL, PRICING, 800, 500, 100, 100);
+ expect(inclusive).toBeCloseTo(exclusive, 10);
+ });
+
+ it('matches the legacy total when no cache price exists (no regression)', () => {
+ const cost = OpenLitHelper.getChatModelCost(
+ 'model-no-cache-price',
+ PRICING,
+ 1000,
+ 500,
+ 100,
+ 100,
+ true
+ );
+ expect(cost).toBeCloseTo((1000 / 1000) * 0.003 + (500 / 1000) * 0.015, 10);
+ });
+
+ it('handles OpenAI-style cache read only (no cache creation)', () => {
+ // OpenAI: prompt_tokens includes cached_tokens; no cache-creation charge.
+ const pricing = {
+ chat: { 'gpt-cache': { promptPrice: 0.0025, completionPrice: 0.01, cacheReadPrice: 0.00125 } },
+ };
+ const cost = OpenLitHelper.getChatModelCost('gpt-cache', pricing, 1000, 200, 400, 0, true);
+ const expected = (600 / 1000) * 0.0025 + (200 / 1000) * 0.01 + (400 / 1000) * 0.00125;
+ expect(cost).toBeCloseTo(expected, 10);
+ });
+
+ it('clamps the prompt base at 0 for malformed token data', () => {
+ const cost = OpenLitHelper.getChatModelCost(MODEL, PRICING, 100, 0, 200, 200, true);
+ const expected = (200 / 1000) * 0.0003 + (200 / 1000) * 0.00375;
+ expect(cost).toBeCloseTo(expected, 10);
+ });
+ });
+});
diff --git a/sdk/typescript/src/instrumentation/anthropic/wrapper.ts b/sdk/typescript/src/instrumentation/anthropic/wrapper.ts
index 7e8a60e6..11909484 100644
--- a/sdk/typescript/src/instrumentation/anthropic/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/anthropic/wrapper.ts
@@ -316,11 +316,15 @@ export default class AnthropicWrapper extends BaseWrapper {
const responseModel = result.model || requestModel;
const pricingInfo = OpenlitConfig.pricingInfo || {};
+ // Anthropic reports input_tokens exclusive of cache read / creation tokens,
+ // so cache tokens are added on top (promptTokensIncludeCache stays false).
const cost = OpenLitHelper.getChatModelCost(
requestModel,
pricingInfo,
result.usage.input_tokens,
- result.usage.output_tokens
+ result.usage.output_tokens,
+ Number(result.usage.cache_read_input_tokens) || 0,
+ Number(result.usage.cache_creation_input_tokens) || 0
);
AnthropicWrapper.setBaseSpanAttributes(span, {
diff --git a/sdk/typescript/src/instrumentation/langchain/wrapper.ts b/sdk/typescript/src/instrumentation/langchain/wrapper.ts
index a384056c..112f6fc1 100644
--- a/sdk/typescript/src/instrumentation/langchain/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/langchain/wrapper.ts
@@ -945,7 +945,18 @@ class OpenLITCallbackHandler {
// Cost
const pricingInfo = OpenlitConfig.pricingInfo || {};
- const cost = OpenLitHelper.getChatModelCost(modelName, pricingInfo, inputTokens, outputTokens);
+ // LangChain's normalized usage reports input_tokens as the sum of all
+ // input token types (uncached + cache read + cache creation), so flag the
+ // prompt tokens as cache-inclusive to avoid billing cached tokens twice.
+ const cost = OpenLitHelper.getChatModelCost(
+ modelName,
+ pricingInfo,
+ inputTokens,
+ outputTokens,
+ holder.cacheReadInputTokens || 0,
+ holder.cacheCreationInputTokens || 0,
+ true
+ );
// Provider for span attributes
const provider = holder.provider || SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN;
diff --git a/sdk/typescript/src/instrumentation/openai/wrapper.ts b/sdk/typescript/src/instrumentation/openai/wrapper.ts
index 1fe4f5ac..429bd9cc 100644
--- a/sdk/typescript/src/instrumentation/openai/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/openai/wrapper.ts
@@ -385,11 +385,17 @@ class OpenAIWrapper extends BaseWrapper {
const pricingInfo = OpenlitConfig.pricingInfo || {};
+ // OpenAI reports prompt_tokens inclusive of cached (cache read) tokens, so
+ // flag the prompt tokens as cache-inclusive to avoid billing cached tokens
+ // twice once a model defines cacheReadPrice.
const cost = OpenLitHelper.getChatModelCost(
requestModel,
pricingInfo,
result.usage.prompt_tokens,
- result.usage.completion_tokens
+ result.usage.completion_tokens,
+ Number(result.usage.prompt_tokens_details?.cached_tokens) || 0,
+ Number(result.usage.prompt_tokens_details?.cache_creation_tokens) || 0,
+ true
);
OpenAIWrapper.setBaseSpanAttributes(span, {
@@ -1181,11 +1187,15 @@ class OpenAIWrapper extends BaseWrapper {
const inputTokens = result.usage?.input_tokens || 0;
const outputTokens = result.usage?.output_tokens || 0;
+ // Responses API reports input_tokens inclusive of cached tokens.
const cost = OpenLitHelper.getChatModelCost(
requestModel,
pricingInfo,
inputTokens,
- outputTokens
+ outputTokens,
+ Number(result.usage?.input_tokens_details?.cached_tokens) || 0,
+ 0,
+ true
);
OpenAIWrapper.setBaseSpanAttributes(span, {
diff --git a/src/client/src/__tests__/helpers/server/openlit.test.ts b/src/client/src/__tests__/helpers/server/openlit.test.ts
index 586efccd..de5badb4 100644
--- a/src/client/src/__tests__/helpers/server/openlit.test.ts
+++ b/src/client/src/__tests__/helpers/server/openlit.test.ts
@@ -80,6 +80,48 @@ describe('OpenLitHelper', () => {
const cost = OpenLitHelper.getChatModelCost('gpt-4', 100, 50);
expect(cost).toBe('0');
});
+
+ describe('cache-aware pricing', () => {
+ beforeEach(() => {
+ OpenLitHelper.pricingInfo = {
+ chat: {
+ 'claude-cache': {
+ promptPrice: 0.003,
+ completionPrice: 0.015,
+ cacheReadPrice: 0.0003,
+ cacheCreationPrice: 0.00375,
+ },
+ 'gpt-4': { promptPrice: 0.03, completionPrice: 0.06 },
+ },
+ };
+ });
+
+ it('stays backward compatible for legacy 3-arg calls', () => {
+ const cost = OpenLitHelper.getChatModelCost('gpt-4', 100, 50);
+ expect(parseFloat(cost)).toBeCloseTo(0.006, 8);
+ });
+
+ it('adds cache cost on top for the exclusive (Anthropic) convention', () => {
+ const cost = OpenLitHelper.getChatModelCost('claude-cache', 800, 500, 100, 100);
+ const expected =
+ (800 / 1000) * 0.003 +
+ (500 / 1000) * 0.015 +
+ (100 / 1000) * 0.0003 +
+ (100 / 1000) * 0.00375;
+ expect(parseFloat(cost)).toBeCloseTo(expected, 8);
+ });
+
+ it('does not bill cache tokens twice for the inclusive (OpenAI/LangChain) convention', () => {
+ const inclusive = OpenLitHelper.getChatModelCost('claude-cache', 1000, 500, 100, 100, true);
+ const exclusive = OpenLitHelper.getChatModelCost('claude-cache', 800, 500, 100, 100);
+ expect(parseFloat(inclusive)).toBeCloseTo(parseFloat(exclusive), 8);
+ });
+
+ it('matches the legacy total when no cache price exists', () => {
+ const cost = OpenLitHelper.getChatModelCost('gpt-4', 1000, 500, 100, 100, true);
+ expect(parseFloat(cost)).toBeCloseTo((1000 / 1000) * 0.03 + (500 / 1000) * 0.06, 8);
+ });
+ });
});
describe('getEmbedModelCost', () => {
diff --git a/src/client/src/helpers/server/openlit.ts b/src/client/src/helpers/server/openlit.ts
index 0b1c9d35..3450fb83 100644
--- a/src/client/src/helpers/server/openlit.ts
+++ b/src/client/src/helpers/server/openlit.ts
@@ -19,17 +19,54 @@ export default class OpenLitHelper {
return encoding.encode(text).length;
}
+ // Cache-aware cost calculation, kept in parity with the SDK helpers.
+ // When the model pricing defines cacheReadPrice / cacheCreationPrice the
+ // matching cache tokens are billed at those rates. Providers that report
+ // promptTokens inclusive of cache tokens (OpenAI, LangChain) should pass
+ // promptTokensIncludeCache=true so cached tokens are not billed twice;
+ // providers that report them exclusively (Anthropic native) leave it false.
static getChatModelCost(
model: string,
promptTokens: number,
- completionTokens: number
+ completionTokens: number,
+ cacheReadTokens: number = 0,
+ cacheCreationTokens: number = 0,
+ promptTokensIncludeCache: boolean = false
): string {
try {
+ const modelPricing = this.pricingInfo.chat[model];
+ const cacheRead = cacheReadTokens || 0;
+ const cacheCreation = cacheCreationTokens || 0;
+
+ let billablePromptTokens = promptTokens;
+ let cacheCost = 0;
+
+ if (modelPricing.cacheReadPrice != null) {
+ cacheCost +=
+ (cacheRead / OpenLitHelper.PROMPT_TOKEN_FACTOR) *
+ modelPricing.cacheReadPrice;
+ if (promptTokensIncludeCache) {
+ billablePromptTokens -= cacheRead;
+ }
+ }
+ if (modelPricing.cacheCreationPrice != null) {
+ cacheCost +=
+ (cacheCreation / OpenLitHelper.PROMPT_TOKEN_FACTOR) *
+ modelPricing.cacheCreationPrice;
+ if (promptTokensIncludeCache) {
+ billablePromptTokens -= cacheCreation;
+ }
+ }
+ if (billablePromptTokens < 0) {
+ billablePromptTokens = 0;
+ }
+
return (
- (promptTokens / OpenLitHelper.PROMPT_TOKEN_FACTOR) *
- this.pricingInfo.chat[model].promptPrice +
+ (billablePromptTokens / OpenLitHelper.PROMPT_TOKEN_FACTOR) *
+ modelPricing.promptPrice +
(completionTokens / OpenLitHelper.PROMPT_TOKEN_FACTOR) *
- this.pricingInfo.chat[model].completionPrice
+ modelPricing.completionPrice +
+ cacheCost
).toFixed(8);
} catch (error) {
console.error(`Error in getChatModelCost: ${error}`);