A dashboard that charts only input_tokens can make a long prompt look remarkably short. In Claude Messages usage, cache reads and cache creation have separate input counters. The same prompt can therefore have the same total input on two requests even though the first writes its reusable prefix and the second reads it.
Parse a saved usage record
Use Python 3 with its standard library and save this as cache.py. The JSON strings are synthetic completed-response fixtures. For this adapter, omitted cache counters mean zero, while an explicitly malformed counter is rejected. The function requires both the ordinary input and output counters since it is designed for completed responses, not partial streaming events.
import json
def account(raw):
usage = json.loads(raw)["usage"]
if not isinstance(usage, dict):
raise ValueError("expected a usage object")
counts = []
for field in ("input_tokens", "cache_creation_input_tokens",
"cache_read_input_tokens", "output_tokens"):
value = usage.get(field, 0) if field.startswith("cache_") else usage[field]
if type(value) is not int or value < 0:
raise ValueError(f"invalid counter: {field}")
counts.append(value)
fresh, written, read, output = counts
return fresh + written + read, read, output
cold = '{"usage":{"input_tokens":20,"cache_creation_input_tokens":2000,"output_tokens":5}}'
warm = '{"usage":{"input_tokens":20,"cache_read_input_tokens":2000,"output_tokens":7}}'
both = '{"usage":{"input_tokens":20,"cache_creation_input_tokens":300,"cache_read_input_tokens":1700,"output_tokens":4}}'
assert account(cold) == (2020, 0, 5)
assert account(warm) == (2020, 2000, 7)
assert account(both) == (2020, 1700, 4)
try:
account('{"usage":{"input_tokens":true,"output_tokens":1}}')
except ValueError:
pass
else:
raise AssertionError("boolean counter accepted")
for phase, raw in (("cold", cold), ("warm", warm)):
total, read, output = account(raw)
print(f"{phase}: input={total} read={read} output={output}")Run python3 cache.py. Expected output:
cold: input=2020 read=0 output=5
warm: input=2020 read=2000 output=7Interpret the counters
Add the three input buckets for total input processed. Do not add generated output to that total, and do not treat cache writes as reads. A request can read one prefix and write another, which is why the third fixture sets both counters. The strict integer check rejects JSON booleans even though Python's boolean type is a subclass of integer. Malformed JSON and missing required fields raise errors rather than creating an apparently empty request in the dashboard.
Troubleshoot a report that shows no reads
Start by confirming that the report receives completed Messages usage rather than a partial event or a normalized third-party envelope. Compare cache creation and read counters on successive requests. A write without a subsequent read is different from never seeing a write. In a live integration, compare the cacheable prefix, breakpoint placement, and applicable model requirements against the provider guide. Keep the separate buckets in stored telemetry if you later need billing analysis. This script counts tokens, not charges, and does not configure caching or establish that any real request hit a cache.
Primary reference and related tutorials: Claude prompt caching and usage counters · Java Samples Python tutorials · AI tutorials.