×
Community Blog I Tested 19 LLM API Workloads on Real Calls and Cut Costs 79% — Here's the Data

I Tested 19 LLM API Workloads on Real Calls and Cut Costs 79% — Here's the Data

518 real API calls. $33.99 → $7.06 in a single run. The same parameter change projects $15,667/year saved on a healthcare workload — here's the exact code, the math, and every scenario I measured.

09_savings_dashboard

A Hypothetical Example

A healthcare AI application — one that queries the same 100K-token medical knowledge base hundreds of times per day — was costing us over $50 per day. Eighteen thousand dollars a year. For what was essentially the same prompt, asked slightly differently each time.

The knowledge base never changed. The system prompt never changed. The tool definitions never changed. But every single request? Full price. Every token reprocessed from scratch.

I knew caching existed in Alibaba Cloud Model Studio. What I didn't know was how dramatic the savings could be — or how trivially simple the implementation was.

So I did what any obsessive engineer would do: I built a tool that analyzes 19 different workload patterns across 4 production Qwen models and 3 caching strategies, runs Monte Carlo simulations, generates confidence intervals, and produces charts that make the answer undeniable.

The result? That $18,527/year application now costs $2,860.

Here's everything I learned.


Key Takeaways

  • Measured this run: 79.2% cost reduction ($33.99 → $7.06) across 19 scenarios and 518 real API calls on qwen3.7-max. Caching paid off in 18/19 scenarios (the only loser was the truly one-off request).
  • Break-even is 0.278 requests. The very first cache reuse already puts you ahead. If you'll ever send the same prefix twice, explicit caching saves money — period.
  • Top measured wins: RAG Fixed Document (50K) 80.0%, Large Context (202K tokens) 78.7%, Code Generation (80K) 76.0%, Agentic Workflow (15K tools) 72.5%, Batch Processing 72.0%.
  • The implementation is one parameter. Add cache_control: {"type": "ephemeral"} to your message. That's it.
  • It's not just Alibaba Cloud. Anthropic offers the same 90% discount with identical syntax. OpenAI gives 50% automatically. Google Gemini gives 75%. Every major provider now supports this.
  • Content ordering is the #1 mistake. A single dynamic value (timestamp, request ID) at the start of your prompt destroys all caching. Most developers do this without realizing it.
  • Projected annual savings across 5 industries: $34,042/year. From a single parameter change.

The Problem Nobody Talks About

Here's the anatomy of a typical LLM API request:

[System Prompt: 3,000 tokens]
[Tool Definitions: 15,000 tokens]
[Conversation History: 5,000 tokens]
[User's Actual Question: 200 tokens]

Total: 23,200 tokens. You pay for all of them.

Now here's the same application, request #2:

[System Prompt: 3,000 tokens]       ← IDENTICAL to request #1
[Tool Definitions: 15,000 tokens]   ← IDENTICAL to request #1
[Conversation History: 5,600 tokens]← 5,000 tokens IDENTICAL
[User's New Question: 200 tokens]   ← Only this changed

You're paying full price for 23,000 tokens that are exactly the same as last time. Only 800 tokens are actually new.

In a typical production workload, 70–95% of input tokens are duplicates from the previous request.

01_token_repetition_problem
Three consecutive API requests. Red = duplicated tokens you're paying for again. Green = actually new content.

Multiply that by hundreds or thousands of requests per day, and you're burning money reprocessing content the model has already seen.


Not All Caching Is the Same

Before diving into the solution, a quick disambiguation. The term "caching" gets thrown around loosely in the LLM space, and it refers to at least four different things — only one of which this article is about.

Type What It Caches Where It Lives Example
Prompt / Context Caching Repeated prefixes in API requests Provider-side (per-request billing) Alibaba Cloud, Anthropic, OpenAI, Gemini
Semantic Caching Meaning-similar queries and responses Application layer (vector DB) GPTCache, Redis LangCache
KV Cache Key-value tensors during model inference Server-side (GPU memory) vLLM, TensorRT-LLM
Response Caching Exact repeated request→response pairs Application layer (hash lookup) Custom middleware, API gateways

Prompt/context caching (this article) operates at the API billing layer. The provider recognizes that the beginning of your prompt hasn't changed since the last request, and charges you less for those tokens. It's deterministic, provider-managed, and requires no infrastructure beyond a single API parameter.

Semantic caching is fundamentally different. It uses embeddings to detect when a new query is similar enough to a previous one that the cached response can be reused. It's powerful for chatbots and search, but introduces accuracy trade-offs — "similar" isn't "identical."

KV cache is internal to the model serving infrastructure. It avoids recomputing attention for tokens already processed in the current session. You don't control it directly as an API user — it's a server-side optimization.

Response caching is the simplest: if the exact same request comes in twice, return the exact same response. Works for deterministic use cases but useless for anything with conversation history or dynamic context.

This article focuses exclusively on prompt/context caching — because it's the only type that's simultaneously easy to implement, provider-guaranteed, and applicable to virtually every production LLM workload.


The Two Caching Strategies (And When Each One Wins)

Alibaba Cloud Model Studio offers two approaches to solve this:

03_implicit_vs_explicit
The two caching strategies compared. Choose based on your workload pattern.

Implicit (Auto) Cache — The Zero-Effort Option

  • The system automatically detects repeated prefixes
  • Cached tokens cost 20% of standard price (80% discount)
  • No code changes whatsoever
  • Best-effort (no guarantee it'll hit)
  • Minimum: 256 tokens

Think of it as: The system is smart enough to notice you're sending the same stuff repeatedly, and charges you less for it. Sometimes.

Explicit Cache — The Serious Option

  • You add one parameter: cache_control: {"type": "ephemeral"}
  • First request: 125% cost (25% surcharge to create the cache)
  • Every subsequent hit: 10% cost (90% discount)
  • Deterministic — guaranteed to hit if content matches
  • TTL: 5 minutes (resets on each hit)
  • Minimum: 1,024 tokens

Think of it as: You tell the system exactly what to cache, pay a small premium once, and get a 90% discount on every request after that. Guaranteed.

The Break-Even Math

This is the number that changed how I think about LLM costs:

Break-even = creation_surcharge / savings_per_hit
           = 0.25 / 0.90
           = 0.278 requests

After just ONE cache hit — the very first reuse — you've already recovered the creation cost and started profiting.

02_prefix_caching_model
The economics of explicit caching: pay 125% once, then 10% forever. Break-even in under 1 request.

The break-even is less than a single request. If you'll use the same prefix twice, explicit caching saves money. Period.


The Analysis: 19 Scenarios, Real API Calls, Real Numbers

I didn't want to guess about savings. I wanted proof. So I built an analysis tool that:

  • Runs 19 realistic workload scenarios as real API calls against Alibaba Cloud Model Studio
  • Tests across 4 production Qwen models (Qwen3.7-Max, Qwen3.6-Plus, Qwen3.6-Flash, Qwen3-Coder-Plus)
  • Compares all 3 strategies — no cache, implicit, explicit — using the actual prompt_tokens_details returned by the API
  • Runs Monte Carlo simulations for confidence intervals on hit-rate variability
  • Scores each scenario 0–100 on caching effectiveness
  • Projects annual ROI for 5 industry verticals

Here's what fell out of the most recent comprehensive run on qwen3.7-max518 API requests, 100% success rate:

Metric Value
Input tokens sent 8,991,566
Output tokens received 685,577
Implicit cached tokens (72.9% hit rate) 3,504,384
Explicit cached tokens (88.8% hit rate) 3,904,876
Cost without caching $33.99
Cost with implicit cache $8.78 (saved $25.21, 74.2%)
Cost with explicit cache $7.06** (saved **$26.93, 79.2%)

Caching paid off in 18 of the 19 scenarios measured — the lone exception was a deliberately one-off request where there is no second call to recover the cache-creation cost. The full per-scenario data is below.

11_savings_heatmap
The full savings matrix across all 4 models. Darker green = higher savings. Most production scenarios land in the 60–80% range.


The Winners: Scenarios That Save 72–80% (Measured)

All numbers below are from real API responses on qwen3.7-max, computed against official tier pricing.

1: RAG with Fixed Document (50K tokens) — 80.0% savings

15 questions against the same 50K-token document:

Strategy Total Cost Savings
No Cache $1.8522
Implicit $0.5082 72.6%
Explicit $0.3702 80.0%

Textbook caching scenario: same large prefix, many queries, minimal dynamic content. Highest savings percentage in the run.

2: Large Context Window (202K tokens) — 78.7% savings

Queries against a 202K-token corpus (near the 256K model limit):

Strategy Total Cost Savings
No Cache $7.4340
Implicit $2.1012 71.7%
Explicit $1.5861 78.7%

Highest absolute dollar savings of any scenario in the run — $5.85 saved on a single workload. A single cache hit on 202K tokens recovers the creation surcharge almost instantly.

3: Code Generation with Large Context (80K) — 76.0% savings

15 iterations against an 80K-token codebase:

Strategy Total Cost Savings
No Cache $3.2364
Implicit $1.0054 68.9%
Explicit $0.7763 76.0%

This is why AI coding assistants (Cursor, Continue, Windsurf) need caching. The codebase context dominates every request.

14_benchmark_scores
Benchmark scores (0–100) for each scenario. Higher = better caching fit. RAG, code generation, and large-context workloads lead.

4: Agentic Workflow (15K tool definitions) — 72.5% savings

12 sequential agent calls with 15K of tool definitions as the stable prefix:

Strategy Total Cost Savings
No Cache $0.3384
Implicit $0.1156 65.8%
Explicit $0.0930 72.5%

For agent frameworks, this should be the default — every observation builds on the same tool prefix.

5: Batch Processing (same template) — 72.0% savings

15 items processed with an identical 7K template prefix:

Strategy Total Cost Savings
No Cache $0.1440
Implicit $0.0499 65.3%
Explicit $0.0403 72.0%

Process items in rapid succession to keep the 5-minute TTL alive across the batch.

Honorable Mentions

Scenario Best Strategy Measured Savings
Long Multi-Turn Chat (20 turns) Explicit 68.9%
High-Frequency API (rapid requests) Explicit 66.6%
Intermittent Usage (TTL Expiration) Implicit 65.4%
Concurrent Users Shared Prefix Explicit 64.3%
Session Resumption (gap between bursts) Implicit/Explicit (tie) 63.8%
Short Multi-Turn Chat Explicit 59.6%
Dynamic Tool Definitions Implicit 59.5%
Multi-Document Comparison Explicit 49.0%
Prompt Prefix Optimization Explicit 46.2%
Streaming with Large Context Explicit 40.2%
Thinking Model with Large Output Explicit 36.7%
Below Minimum Cache Threshold Implicit 16.7%
RAG with Varying Retrieved Context Explicit 9.5%

13_cost_comparison
Cost comparison for Qwen3.7-Max across scenarios. The green bars (explicit cache) are barely visible next to the red bars (no cache).


The Losers: When Caching Hurts

12_break_even_analysis
Break-even analysis across different token sizes. The crossover happens almost immediately.

Not everything benefits from explicit caching. Three scenarios where it fails:

Single One-Off Requests: -10.0% (you LOSE money)

With explicit caching on a single request, you pay the 25% creation surcharge with no subsequent hit to recover it. In this run, the lone one-off scenario went from $0.0120 (no cache) to $0.0132 (explicit) — never use explicit caching for truly one-off queries.

Below 1,024 Tokens: 0% savings (can't activate)

If your system prompt is only 500 tokens, explicit caching literally cannot activate. You're below the minimum threshold. Options: pad your prompt with more detailed instructions, or rely on implicit (which has a 256-token minimum).

Intermittent Usage with >5 Minute Gaps: Implicit wins

The 5-minute TTL means the cache expires between requests. Each burst requires re-creation. In this case, implicit caching (no creation surcharge) actually outperforms explicit.


From Percentages to Dollars: The Case Studies

Percentages are nice. But CFOs want dollars. Here's what the analysis projects for five real deployments:

Industry Model Daily Vol. Annual (No Cache) Annual (Cached) Saved
Healthcare Qwen3.7-Max 200/day $18,527 $2,860 $15,667
SaaS Code Review Qwen3-Coder-Plus 300/day $16,983 $2,853 $14,131
E-commerce Qwen3.6-Plus 1,000/day $2,555 $597 $1,958
Education Qwen3.6-Plus 500/day $1,781 $474 $1,307
Fintech Qwen3.6-Flash 5,000/day $1,332 $353 $979

Combined annual savings: $34,042.

05_case_studies_savings
Annual cost comparison across 5 industries. The "before" bars are barely visible at this scale.

The healthcare case is striking. A 100K-token medical knowledge base at 200 requests/day — with explicit caching, each request after the first costs 90% less on those 100K tokens. Even at Qwen3.7-Max's premium pricing ($1.20–$3.00 per 1M input tokens depending on tier), the math is overwhelming.


The Implementation: It's Embarrassingly Simple

Here's the thing that frustrated me most when I discovered this: the code change is trivial.

08_code_before_after
The entire implementation. One parameter. That's it.

Before (no caching):

response = client.chat.completions.create(
    model="qwen3.6-plus",
    messages=[
        {"role": "system", "content": "You are an expert..."},
        {"role": "user", "content": "Analyze this..."}
    ]
)

After (explicit caching):

response = client.chat.completions.create(
    model="qwen3.6-plus",
    messages=[
        {
            "role": "system",
            "content": [
                {
                    "type": "text",
                    "text": "You are an expert...",
                    "cache_control": {"type": "ephemeral"}
                }
            ]
        },
        {"role": "user", "content": "Analyze this..."}
    ]
)

That's it. You convert the content field from a string to an array of content blocks, and add cache_control to the block you want cached.

One parameter. 90% discount on every subsequent request. Break-even in less than 1 hit.


Five Rules I Learned the Hard Way

After running hundreds of test scenarios, here are the patterns that separate 40% savings from 85% savings:

Rule 1: Content Order Is Everything

Caching works on prefixes — the beginning of your prompt. If you put anything dynamic at the start, everything after it becomes uncacheable.

06_content_ordering
Left: stable content first = 85% cached. Right: timestamp first = 0% cached. Order matters enormously.

Good ordering:

1. System prompt (never changes)       ← CACHED
2. Tool definitions (rarely changes)   ← CACHED
3. Few-shot examples (stable)          ← CACHED
4. Reference document (session-stable) ← CACHED
5. User query (changes every time)     ← Full price (but tiny)

Terrible ordering:

1. Timestamp / request ID (dynamic!)   ← BREAKS ENTIRE CACHE
2. System prompt                       ← Not cached (prefix broken)
3. Everything else                     ← Not cached

I've seen production code with f"Current time: {datetime.now()}\n" prepended to the system prompt. That single line destroys all caching potential.

Rule 2: Exceed 1,024 Tokens (It's Not Hard)

Your cacheable block must be at least 1,024 tokens. If your system prompt is too short, combine it with:

  • Tool/function definitions
  • Few-shot examples
  • Reference documentation
  • Detailed instructions

Most production prompts already exceed this naturally.

Rule 3: Batch Tightly

The cache TTL is 5 minutes (resets on each hit). If you're processing 50 items:

  • Good: Send them in rapid succession (sub-second gaps)
  • Bad: Add time.sleep(30) between items (6 TTL expirations)

For batch workloads, process everything in one tight burst.

Rule 4: Use Multiple Markers Strategically

You get up to 4 cache markers per request. Place them at stability boundaries:

messages=[
    {"role": "system", "content": [
        {"type": "text", "text": system_prompt,
         "cache_control": {"type": "ephemeral"}}     # Marker 1
    ]},
    {"role": "user", "content": [
        {"type": "text", "text": reference_doc,
         "cache_control": {"type": "ephemeral"}},    # Marker 2
        {"type": "text", "text": user_question}      # Not cached
    ]}
]

This way, even if the reference doc changes between sessions, the system prompt cache stays valid.

Rule 5: Verify It's Working

Always check the response:

usage = response.usage

# Cache was created (first request)
if hasattr(usage, 'cache_creation_input_tokens'):
    print(f"Cache created: {usage.cache_creation_input_tokens} tokens")

# Cache was hit (subsequent requests)  
if hasattr(usage, 'cache_read_input_tokens'):
    print(f"Cache hit: {usage.cache_read_input_tokens} tokens")
    # You just saved 90% on these tokens!

If you're not seeing cache_read_input_tokens on request #2+, something is wrong with your prefix stability.


3 Mistakes That Silently Destroy Your Savings

After testing 19 scenarios, I found the same three errors appearing over and over. These aren't theoretical — I made every one of them before the data forced me to pay attention.

Mistake #1: The Timestamp Trap

# DON'T DO THIS — it destroys ALL caching
system_prompt = f"Current time: {datetime.now()}\nYou are a helpful assistant..."

That single datetime.now() call makes every request unique from the very first token. The cache sees a different prefix every time and never activates. I found this pattern in 3 of the 5 production codebases I reviewed.

Fix: Move timestamps to the end of the prompt, or pass them in the user message (after the cached prefix).

Mistake #2: The TTL Death Spiral

The cache expires after 5 minutes of inactivity. If your application makes one request every 6 minutes, you're paying the 25% creation surcharge every single time — and actually spending more than with no explicit caching at all.

Fix: For intermittent traffic, either stick with implicit caching (no surcharge) or batch requests together to keep the cache warm.

Mistake #3: The Phantom Cache

Developers add cache_control, assume it's working, and never check the response. Weeks later, they discover a formatting inconsistency (trailing whitespace, different Unicode normalization, reordered JSON keys) was silently preventing cache hits the entire time.

Fix: Always log cache_read_input_tokens in your monitoring. If it's zero on request #2+, your prefix isn't as stable as you think.


The Decision Flowchart

After analyzing all 19 scenarios, the decision is surprisingly simple:

04_decision_flowchart
Follow the arrows. Three questions, clear answers.

For the vast majority of production LLM applications — chatbots, agents, RAG systems, batch processors, code assistants — the answer is explicit caching.


10_cost_projection_chart
Cumulative costs diverge dramatically. By 50,000 requests, explicit caching has saved thousands.

What This Means for the Industry

The implications extend beyond individual cost savings:

For startups: The difference between $18K/year and $2.8K/year in API costs might be the difference between runway lasting 12 months vs 18 months. At early stage, this is existential.

For enterprises: Multiplied across dozens of LLM-powered features, caching can represent hundreds of thousands in annual savings. It's the rare optimization that's both easy to implement and massive in impact.

For the LLM ecosystem: As more providers adopt similar caching mechanisms (Anthropic, OpenAI, and Google all have their versions), understanding prefix-based caching becomes a core competency for any ML engineer. The principles are universal even if the APIs differ.


How Every Major Provider Compares

This isn't an Alibaba Cloud-only trick. Every major LLM provider now offers prompt caching — but the details vary significantly. Here's the side-by-side comparison I wish I'd had before choosing a provider:

Alibaba Cloud Anthropic (Claude) OpenAI Google Gemini
Max discount 90% 90% 50% 75%
Cache creation cost +25% surcharge +25% surcharge None Storage-based fee
Min tokens 1,024 1,024 1,024 32,768
TTL 5 minutes 5 minutes Automatic (no control) 1 hour (configurable)
Guarantee Deterministic Deterministic Best-effort Deterministic
Implementation cache_control param cache_control param Automatic CachedContent object
Multi-marker Up to 4 per request Up to 4 per request N/A Single cache point

The headline: Alibaba Cloud and Anthropic offer identical economics (90% discount, same cache_control syntax, same 1,024-token minimum). If you're using Claude today, you can switch to Qwen models — or vice versa — with minimal code changes.

OpenAI's approach is the simplest (zero code changes, automatic caching) but also the weakest: 50% discount with no guarantee it'll hit. Google's minimum of 32,768 tokens makes caching impractical for most short-prompt applications.

The universal principle is the same across all providers: stable content first, dynamic content last. Master this pattern once, and you'll save money regardless of which API you call.


Try It Yourself

The analysis tool I built will able to do:

  • Runs entirely locally (no API key needed for simulated analysis)
  • Generates a 50KB+ Markdown report with 17+ charts
  • Supports live API calls to measure real caching behavior
  • Uses YAML configuration for easy customization
  • Covers your exact scenario if you configure it

07_terminal_analysis_output
What you'll see when you run the analysis. Full report in seconds.

pip install -r requirements.txt
python main.py analyze
# → reports/YYYY-MM-DD_HHMMSS_<mode>/caching_report.md
# → reports/YYYY-MM-DD_HHMMSS_<mode>/business_value.md
# → reports/YYYY-MM-DD_HHMMSS_<mode>/charts/ (17+ PNG/SVG visualizations)

You can also run specific models:

python main.py analyze --models qwen3.6-plus,qwen3.6-flash

Or skip chart generation for faster results:

python main.py analyze --no-charts

The Bottom Line

Context caching isn't a micro-optimization you'll get to someday. It's a fundamental architecture decision that determines whether your LLM application costs $500/year or $18,000/year.

The data is clear:

  • Measured this run: 79.2% cost reduction ($33.99 → $7.06 across 19 scenarios, 518 real API calls)
  • Break-even: 0.278 requests (instant ROI)
  • Top scenarios save 72–80% measured (RAG, large-context, code generation, agentic, batch)
  • Implementation: one parameter change
  • Projected savings across 5 industries: $34,042/year

16_savings_percentage
Per-scenario savings percentages. The vast majority of production workloads see 60%+ savings.

If you're building anything that makes repeated LLM API calls with a stable prefix — and that's almost everything in production — you're leaving money on the table every day you don't enable explicit caching.

The 15 minutes it takes to add cache_control markers will be the highest-ROI engineering work you do this quarter.

Here's your first step: Open your production code, find the LLM API call, and check what the first 1,024 tokens of your prompt look like. If they're the same across requests (they almost certainly are), add one line:

"cache_control": {"type": "ephemeral"}

Then check your next response for cache_read_input_tokens. When you see that number, you'll know exactly how much you were overpaying.

Question for you: What does your LLM API bill look like right now? I'm curious which workload patterns people are seeing in production — drop a comment with your use case and I'll tell you what savings to expect.


If this saved you money (or prevented you from wasting it), hit the clap button — it helps other engineers find this analysis. I write data-driven deep dives on cloud cost optimization and AI engineering. Follow to get the next one.

Implementing caching for an unusual use case? Drop a comment with your scenario — I'll help you figure out which strategy fits and what savings to expect.


References & Documentation

Official Documentation

Caching Across Providers (Comparison Reading)

Technical Deep Dives & Further Reading

Tools & Source Code

Related Concepts

  • KV Cache Optimization in LLMs — Academic paper on KV-cache compression techniques that underpin prefix caching at the model level.
  • Prefix Caching in vLLM — How the open-source vLLM serving framework implements automatic prefix caching (the same principle, server-side).
0 1 0
Share on

Farruh

37 posts | 33 followers

You may also like

Comments