DeepSeekDSH
Independent community guideNot affiliated with DeepSeek.Download versions and guide references

DeepSeek Prompt Caching & Off-Peak Cost Guide

DeepSeek caches identical prompt prefixes automatically on the server. Review current V4.1 Flash cache-hit pricing, prefix engineering rules, workday peak hours, and Python audit code.

On this page

1. Cache mechanics & V4.1 Flash billing rules

DeepSeek enables automatic server-side Context KV Cache (Prompt Caching) with no extra request headers or manual cache TTL management:

Cache Miss (prompt_cache_miss_tokens)

Uncached input prefixes populate the server KV cache and bill at the standard uncached input rate (on DeepSeek V4.1 Flash: $0.30 / 1M peak, $0.15 / 1M off-peak).

Cache Hit (prompt_cache_hit_tokens)

Subsequent requests sharing an exact byte-for-byte prefix bill matched 64-token blocks at the cache-hit rate (on DeepSeek V4.1 Flash: $0.006 / 1M peak, $0.003 / 1M off-peak) while lowering first-token latency.

2. Three prefix engineering rules

Rule 1: Strict sequential prefix match

Caching matches sequentially from token 0. Inserting per-request metadata (such as Current Time: 2026-10-10T14:02:11Z or random request UUIDs) at the top invalidates every downstream token.

Rule 2: Static content first, dynamic last

Place unchanging system instructions, coding conventions, and reference docs at the start of messages; append retrieved snippets and the latest user turn at the end.

Rule 3: Freeze tool schemas and append-only history

Keep the tools array in a deterministic order across turns and append to conversation history rather than rewriting or re-sorting earlier turns on every call.

3. Peak vs off-peak pricing windows

On current DeepSeek V4.1 Flash pricing, peak rates apply during workdays 09:00–12:00 and 14:00–18:00 (UTC+8). All other windows—including workday lunch breaks (12:00–14:00 UTC+8), nights, and weekends—automatically receive 50% off across uncached input, cache hits, and output ($1.20 / 1M peak output vs $0.60 / 1M off-peak).

Batch scheduling tip: Queue non-urgent repository audits, test generation, and document extraction jobs outside workday peak windows, and always verify current rate tables on DeepSeek's official pricing page before running large batch jobs.

4. Python cache hit ratio & token audit script

Inspect cache statistics directly from the API response usage object:

from openai import OpenAI client = OpenAI( api_key="sk-your_deepseek_api_key", base_url="https://api.deepseek.com" ) # Keep static rules identical across turns system_prompt = "You are a code reviewer. " + ("Standard rules... " * 100) response = client.chat.completions.create( model="deepseek-chat", messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": "Audit code style."} ] ) usage = response.usage prompt_tokens = usage.prompt_tokens cache_hit_tokens = getattr(usage, "prompt_cache_hit_tokens", 0) cache_miss_tokens = getattr(usage, "prompt_cache_miss_tokens", prompt_tokens - cache_hit_tokens) print(f"Total prompt tokens: {prompt_tokens}") print(f"Cache hit tokens: {cache_hit_tokens}") print(f"Cache miss tokens: {cache_miss_tokens}") if prompt_tokens > 0: hit_ratio = (cache_hit_tokens / prompt_tokens) * 100 print(f"Cache hit ratio: {hit_ratio:.2f}%")

Troubleshooting

Why is prompt_cache_hit_tokens always 0?

Server-side caching is optimized for prompts exceeding a threshold (typically 64 to 1024 tokens). Short messages or dynamically modified system prompts will not trigger cache hits.

Cache miss during multi-turn conversation

Check if the client application inserts unique session IDs, timestamps, or alters previous turn message ordering.

References