Skip to content

Prompt Caching

Prompt caching allows you to cache large blocks of static context—such as system instructions, boilerplate codebase references, background documentation, or long conversation histories—at the LLM provider level. Subsequent requests that reuse this context bypass full-cost token parsing, resulting in significantly reduced API costs and sub-second time-to-first-token latency.

Candela integrates with prompt caching transparently for both Anthropic Claude and Google Gemini, auto-normalizing cache metrics and applying the correct pricing discounts directly to user budget calculations.


While the goal of prompt caching is the same for all providers, the implementation details, billing structures, and minimum token thresholds differ significantly.

FeatureAnthropic ClaudeGoogle Gemini (1.5/2.5/3.x)
Caching TechnologyPrompt Caching (Prefix Caching)Context Caching (Persistent Memory)
Minimum Prompt Size1,024 tokens (Sonnet/Haiku)
2,048 tokens (Opus)
2,048 tokens (2.5 Flash/Pro)
4,096 tokens (3.x)
Write Cost (Cache Create)1.25× base price (5m TTL)
2.0× base price (1h TTL)
No surcharge (Free to create)
Read Cost (Cache Hit)90% off base price (0.1× cost)75–90% off base price (see below)
Cache Lifetime (TTL)Sliding window: 5 mins to 1 hourUser-configurable (Default: 300s / 5m)
Ideal Use CaseFast developer loops, agent actionsLarge documents, codebase context, media analysis

When a request is sent, Candela automatically injects cache_control markers into eligible sections of your messages (e.g., system prompt and early turns) if caching_mode is set to auto. Anthropic charges an upfront write surcharge to build the cache, but subsequent turns inside the TTL enjoy a 90% read discount.

Candela manages this transparently:

  1. Injects cache_control markers into eligible message blocks
  2. Tracks cache_creation_input_tokens and cache_read_input_tokens from the response
  3. Applies the correct pricing multiplier based on your TTL setting
  4. Reports cache savings in the dashboard and trace details

Google Gemini caching is managed as Context Caching in Vertex AI. Because Google does not charge a write surcharge, creating a cache is extremely cost-effective for large payloads. Candela automatically reads Google’s cachedContentTokenCount response metadata and applies model-aware discounts:

  • Gemini 2.5+ and 3.x models: 90% off (0.10× base price)
  • Gemini 2.0 and older: 75% off (0.25× base price)

Anthropic offers two cache TTL (time-to-live) options with different pricing:

TTLWrite CostRead CostBest For
5 minutes (default)1.25× input price0.1× input priceShort interactive sessions, chat
1 hour2.0× input price0.1× input priceLong coding sessions, agents, batch processing

Set defaults in ~/.config/candela/config.yaml:

~/.config/candela/config.yaml
vertex_ai:
caching_mode: auto # off | auto | system-only
cache_ttl: 5m # 5m (default) | 1h
caching_modeBehavior
offNo cache headers injected
autoCache system prompt + early turns automatically
system-onlyOnly cache the system prompt

Override caching settings per-request via HTTP headers, or update the server’s global config via the local API:

Terminal window
# Override caching mode and TTL for a single request
curl -X POST http://localhost:8181/v1/chat/completions \
-H "X-Candela-Caching: auto" \
-H "X-Candela-Cache-TTL: 1h" \
-H "Content-Type: application/json" \
-d '{ ... }'

In Settings → Prompt Caching, toggle between:

  • Standard (5 min) — lower upfront cost, suitable for short sessions
  • Extended (1 hour) — higher upfront cost, ideal for long coding sessions with Claude Code

The setting takes effect immediately for all subsequent proxy requests.


For Claude Sonnet 4 (claude-sonnet-4-20250514, $3/MTok input):

ScenarioTokensTTLCost
Cache write (first request)10,0005m$0.0375 (10K × $3 × 1.25 / 1M)
Cache write (first request)10,0001h$0.06 (10K × $3 × 2.0 / 1M)
Cache read (subsequent)10,000any$0.003 (10K × $3 × 0.1 / 1M)
No cache (baseline)10,000$0.03 (10K × $3 / 1M)

The 5m TTL write cost ($0.0375) + one cache read ($0.003) = $0.0405, which is already cheaper than two baseline requests ($0.06). The 1h TTL breaks even after just 2 cache reads — and you get 12× longer cache retention.


  1. Structure your Prompts: Put static instructions, system definitions, tools/functions, and reference documents at the very beginning of your prompt. Put the fast-moving user query at the very end.
  2. Combine Small System Prompts: If your system prompt is just under the 1,024/2,048 token threshold for Claude, consider adding developer guidelines or schemas to push it past the minimum size and activate caching.
  3. Choose the Right TTL:
    • Use 5 minutes for quick chat sessions.
    • Use 1 hour for developer loops (e.g., using Cline/Zed/Cursor with Candela) where files are constantly re-read over an extended period.
  4. Use Gemini for Large Multi-modal Files: If you are feeding entire PDFs or codebase dumps (exceeding 32k tokens), routing them to Gemini models utilizing Vertex AI Context Caching will yield the highest cost savings since there is no cache write surcharge.

Candela tracks cache performance across all Anthropic requests:

MetricDescription
Cache hit ratePercentage of input tokens served from cache
Cache savingsUSD saved vs. full-price input tokens
Write tokensTokens written to cache (charged at 1.25× or 2.0×)
Read tokensTokens read from cache (charged at 0.1×)

These metrics appear in:

  • Dashboard — aggregate cache savings in the cost overview
  • Trace detail — per-request cache breakdown
  • Models view — per-model cache hit rates

Candela uses model-aware discounts by default:

  • Gemini 2.5+ and 3.x: 0.10 (90% off)
  • Gemini 2.0 and older: 0.25 (75% off)

For enterprise customers with negotiated pricing, override the discount at runtime:

Terminal window
# Set Gemini cache price multiplier (0.10 = cached tokens cost 10% of base price)
curl -X POST http://localhost:8181/_local/api/config/caching \
-H "Content-Type: application/json" \
-d '{"gemini_cache_discount": 0.10}'

The current multiplier is reflected in the GET /_local/api/config response, so clients can display the active configuration.


SymptomCauseFix
Cache hit rate is 0%caching_mode set to offSet to auto in config or desktop settings
High write costsUsing 1h TTL with short sessionsSwitch to 5m TTL if sessions are under 5 minutes
Cache not persistingTTL expired between requestsIncrease TTL or reduce time between requests
Cost shows $0 for cache tokensModel not in pricing tableCheck server logs for unrecognized model warnings