@@ -78,6 +78,60 @@ def test_prompt_tokens_details_dict_empty():
7878 assert result ["output" ] == 100
7979
8080
81+ def test_anthropic_cache_creation_nested_dict ():
82+ """Anthropic extended prompt caching: cache_creation nested dict is flattened."""
83+ usage = {
84+ "input_tokens" : 9454 ,
85+ "output_tokens" : 380 ,
86+ "cache_read_input_tokens" : 0 ,
87+ "cache_creation" : {
88+ "ephemeral_1h_input_tokens" : 0 ,
89+ "ephemeral_5m_input_tokens" : 2048 ,
90+ },
91+ }
92+ result = _parse_usage_model (usage )
93+ assert result ["input" ] == 9454
94+ assert result ["output" ] == 380
95+ assert result ["cache_creation_ephemeral_1h_input_tokens" ] == 0
96+ assert result ["cache_creation_ephemeral_5m_input_tokens" ] == 2048
97+ assert result ["cache_creation_input_tokens" ] == 2048
98+ # No nested dict may survive into the final usage model
99+ assert all (isinstance (v , int ) for v in result .values ())
100+
101+
102+ def test_anthropic_cache_creation_keeps_existing_aggregate ():
103+ """API-provided cache_creation_input_tokens is not overwritten by the computed sum."""
104+ usage = {
105+ "input_tokens" : 100 ,
106+ "output_tokens" : 10 ,
107+ "cache_creation_input_tokens" : 3000 ,
108+ "cache_creation" : {
109+ "ephemeral_1h_input_tokens" : 1000 ,
110+ "ephemeral_5m_input_tokens" : 2000 ,
111+ },
112+ }
113+ result = _parse_usage_model (usage )
114+ assert result ["cache_creation_input_tokens" ] == 3000
115+ assert result ["cache_creation_ephemeral_1h_input_tokens" ] == 1000
116+ assert result ["cache_creation_ephemeral_5m_input_tokens" ] == 2000
117+
118+
119+ def test_anthropic_cache_creation_all_zero ():
120+ """All-zero cache_creation tiers: flattened keys kept, no aggregate invented."""
121+ usage = {
122+ "input_tokens" : 50 ,
123+ "output_tokens" : 5 ,
124+ "cache_creation" : {
125+ "ephemeral_1h_input_tokens" : 0 ,
126+ "ephemeral_5m_input_tokens" : 0 ,
127+ },
128+ }
129+ result = _parse_usage_model (usage )
130+ assert result ["cache_creation_ephemeral_1h_input_tokens" ] == 0
131+ assert result ["cache_creation_ephemeral_5m_input_tokens" ] == 0
132+ assert "cache_creation_input_tokens" not in result
133+
134+
81135def test_priority_tier_not_subtracted ():
82136 """Priority tier: 'priority' and 'priority_*' keys must NOT be subtracted."""
83137 usage = {
0 commit comments