Skip to content

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

fix(anthropic): keep prompt-cache counts across a streamed message_delta (#1611)

BASIL K AJIa30ed430c6b1
diff --git a/sdk/python/src/openlit/instrumentation/anthropic/utils.py b/sdk/python/src/openlit/instrumentation/anthropic/utils.py
index 0cad2c79..d6817af6 100644
--- a/sdk/python/src/openlit/instrumentation/anthropic/utils.py
+++ b/sdk/python/src/openlit/instrumentation/anthropic/utils.py
@@ -543,11 +543,19 @@ def process_chunk(scope, chunk):
delta = chunked.get("delta") or {}
scope._output_tokens = usage.get("output_tokens", 0) or 0
scope._finish_reason = delta.get("stop_reason") or scope._finish_reason
- # message_delta carries final usage; update cache token counts when present
- scope._cache_creation_input_tokens = (
- usage.get("cache_creation_input_tokens", 0) or 0
- )
- scope._cache_read_input_tokens = usage.get("cache_read_input_tokens", 0) or 0
+ # message_delta carries final usage; update cache token counts when
+ # present. The comment always said "when present" and the code did not:
+ # a message_delta carrying only output_tokens reset both counters to
+ # zero, losing what message_start reported. `is not None` keeps a delta
+ # that does report a cache field authoritative while leaving the
+ # message_start values alone when it does not, which is what the Go SDK
+ # in this repository already does (sdk/go/.../anthropic/streaming.go).
+ cache_creation = usage.get("cache_creation_input_tokens")
+ if cache_creation is not None:
+ scope._cache_creation_input_tokens = cache_creation or 0
+ cache_read = usage.get("cache_read_input_tokens")
+ if cache_read is not None:
+ scope._cache_read_input_tokens = cache_read or 0
def common_chat_logic(
diff --git a/sdk/python/tests/test_anthropic_stream_cache_tokens.py b/sdk/python/tests/test_anthropic_stream_cache_tokens.py
new file mode 100644
index 00000000..bdbec0d6
--- /dev/null
+++ b/sdk/python/tests/test_anthropic_stream_cache_tokens.py
@@ -0,0 +1,147 @@
+# pylint: disable=protected-access, missing-function-docstring
+"""Regression tests: a streamed Anthropic response must keep the prompt-cache
+counts reported at `message_start`.
+
+`process_chunk` read `cache_creation_input_tokens` and `cache_read_input_tokens`
+from `message_start`, then reassigned both at `message_delta` with a `0`
+default. The comment above that line said "when present"; the code assigned
+unconditionally, so a `message_delta` carrying only `output_tokens` — which is
+what the Anthropic API sends — reset both counters to zero. Every streamed call
+therefore reported no cache reads and no cache writes, and the cost calculation
+lost the cache discount with them.
+
+The Go SDK in this repository already does it the other way:
+`sdk/go/instrumentation/anthropic/streaming.go` takes the cache counts at
+`message_start` and its `message_delta` case reads only `StopReason` and
+`OutputTokens` (#1462, merged 2026-08-16).
+
+These tests drive the real `process_chunk` with anthropic-shaped events.
+"""
+
+from openlit.instrumentation.anthropic.utils import process_chunk
+
+
+# pylint: disable=too-many-instance-attributes, too-few-public-methods
+class _Scope:
+ """Minimal stand-in for the streaming wrapper's scope object.
+
+ It mirrors the attributes `process_chunk` reads and writes, so the count
+ is the wrapper's, not this test's.
+ """
+
+ def __init__(self):
+ self._timestamps = []
+ self._start_time = 0.0
+ self._ttft = 0
+ self._response_id = ""
+ self._response_model = ""
+ self._response_role = ""
+ self._input_tokens = 0
+ self._output_tokens = 0
+ self._cache_creation_input_tokens = 0
+ self._cache_read_input_tokens = 0
+ self._finish_reason = ""
+ self._llmresponse = ""
+ self._tool_calls_by_index = None
+
+
+MESSAGE_START = {
+ "type": "message_start",
+ "message": {
+ "id": "msg_01",
+ "model": "claude-3-5-sonnet-latest",
+ "role": "assistant",
+ "usage": {
+ "input_tokens": 10,
+ "cache_creation_input_tokens": 1024,
+ "cache_read_input_tokens": 2048,
+ },
+ },
+}
+
+# What the API actually sends: final usage with output_tokens and no cache keys.
+MESSAGE_DELTA_WITHOUT_CACHE = {
+ "type": "message_delta",
+ "delta": {"stop_reason": "end_turn"},
+ "usage": {"output_tokens": 25},
+}
+
+
+def test_a_delta_without_cache_keys_keeps_the_message_start_counts():
+ scope = _Scope()
+ process_chunk(scope, MESSAGE_START)
+ assert scope._cache_creation_input_tokens == 1024
+ assert scope._cache_read_input_tokens == 2048
+
+ process_chunk(scope, MESSAGE_DELTA_WITHOUT_CACHE)
+
+ # Before the fix both fell to zero here and the cache discount was lost.
+ assert scope._cache_creation_input_tokens == 1024
+ assert scope._cache_read_input_tokens == 2048
+ # The fields message_delta does own must still be taken from it.
+ assert scope._output_tokens == 25
+ assert scope._finish_reason == "end_turn"
+
+
+def test_a_delta_that_does_report_cache_counts_still_wins():
+ """`is not None` must not become "ignore the delta"."""
+ scope = _Scope()
+ process_chunk(scope, MESSAGE_START)
+
+ process_chunk(
+ scope,
+ {
+ "type": "message_delta",
+ "delta": {"stop_reason": "end_turn"},
+ "usage": {
+ "output_tokens": 25,
+ "cache_creation_input_tokens": 4096,
+ "cache_read_input_tokens": 8192,
+ },
+ },
+ )
+
+ assert scope._cache_creation_input_tokens == 4096
+ assert scope._cache_read_input_tokens == 8192
+
+
+def test_an_explicit_zero_in_the_delta_is_honoured():
+ """A reported zero is a value, not an absence."""
+ scope = _Scope()
+ process_chunk(scope, MESSAGE_START)
+
+ process_chunk(
+ scope,
+ {
+ "type": "message_delta",
+ "delta": {"stop_reason": "end_turn"},
+ "usage": {
+ "output_tokens": 25,
+ "cache_creation_input_tokens": 0,
+ "cache_read_input_tokens": 0,
+ },
+ },
+ )
+
+ assert scope._cache_creation_input_tokens == 0
+ assert scope._cache_read_input_tokens == 0
+
+
+def test_no_cache_anywhere_stays_zero():
+ scope = _Scope()
+ process_chunk(
+ scope,
+ {
+ "type": "message_start",
+ "message": {
+ "id": "msg_02",
+ "model": "claude-3-5-sonnet-latest",
+ "role": "assistant",
+ "usage": {"input_tokens": 10},
+ },
+ },
+ )
+ process_chunk(scope, MESSAGE_DELTA_WITHOUT_CACHE)
+
+ assert scope._cache_creation_input_tokens == 0
+ assert scope._cache_read_input_tokens == 0