altimate-code icon indicating copy to clipboard operation
altimate-code copied to clipboard

AI spent going through the roof because requests are not cached

Open FrankTub opened this issue 4 months ago • 2 comments

Question

Do you think the following AI analysis makes sense? An analysis of my spent is showing that it is going to the roof. Below my tokens which is just going wild. I changed from Opus 4.6 to Opus 4.7 for most of my work on Jun 6.

Apr 13 ██░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 22.7M Apr 20 █████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 89.7M Apr 27 ███████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 74.6M May 04 ██████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░ 147.5M May 11 █░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 7.8M May 18 ███████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 111.6M May 25 ████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 43.5M Jun 01 █████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 87.3M Jun 08 ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ — gap Jun 15 ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ — gap Jun 22 ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ — gap Jun 29 █████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 91.6M Jul 06 ████████████████████████████████████████░░ 408.2M ⚠️ Jul 13 ██████████████████████████████░░░░░░░░░░░░ 303.8M

I asked Cortex Code to analyze why this happens:

No, caching is NOT being applied to your REST API calls.

Here's the evidence:

Service | cache_read_input | cache_write_input | Caching Active? -- | -- | -- | -- REST API (your calls) | NULL | NULL | No Cortex Code Snowsight | 271,527+ per request | 8,123+ per request | Yes>

What This Means

  • Your REST API calls show only input and output in TOKENS_GRANULAR — no cache_read_input or cache_write_input fields at all.
  • By contrast, your Cortex Code Snowsight usage (this session) shows 96%+ cache hit rates (cache_read_input dominates input), meaning prompt caching is heavily utilized there.

Cost Impact

Prompt caching on Anthropic models typically gives 90% discount on cached input tokens. Your REST API calls average ~300K input tokens per request with zero caching. If even 70% of those tokens were cacheable (common for repeated system prompts, tool definitions, or conversation history):

  • Current: ~300K input tokens billed at full rate per request
  • With caching: ~90K at full rate + ~210K at 10% rate = effective ~111K tokens
  • Potential savings: ~63% on input token costs → roughly ~$4,000/month in savings at current volume

Environment

Altimate code 0.9.1 Connected to Snowflake Cortex Opus 4.7

FrankTub avatar Jul 17 '26 12:07 FrankTub

I double checked and the analysis seems legit, this is an absolute dealbreaker for us to keep on using it as we do right now

FrankTub avatar Jul 20 '26 06:07 FrankTub

This is fixed in v0.9.2 (npm i -g @altimateai/altimate-code, or let auto-upgrade pick it up).

Your Cortex analysis was correct — caching was never being applied to your REST API calls. Root cause: Snowflake Cortex only honors cache markers placed inside content blocks (messages[].content[].cache_control), but the SDK layer we use serialized them as message-level fields, which Cortex silently ignores. Every request billed its full input (~300K tokens in your case) at the undiscounted rate — hence cache_read_input/cache_write_input staying NULL in TOKENS_GRANULAR while Cortex Code (which talks to Cortex natively) showed 96% hits.

What v0.9.2 does: relocates the cache markers into content blocks on the system prompt and trailing messages (verified live against a real Cortex account on every Claude model in the catalog — cache write and read activity confirmed via usage.prompt_tokens_details.cached_tokens). Repeated prefixes now bill at Snowflake's reduced cached-input rate; long agent sessions with large stable context benefit most.

How to verify on your side: after upgrading, cache_read_input/cache_write_input in TOKENS_GRANULAR should start populating for your altimate-code workload (they'll go from NULL to non-null — if any dashboard queries assume those columns are NULL, update them). Give it a session or two of normal use and compare input-token consumption on repeated turns.

anandgupta42 avatar Jul 21 '26 04:07 anandgupta42