Long-Document Workflows on a 1M Context: Combining Context Caching with Off-Peak Pricing (Python)
Feeding an entire contract, codebase or book into a model and then asking follow-ups no longer requires bolting RAG onto everything — deepseek-flash offers a 1M-token context with a 384K output ceiling. But long-context bills can spiral. This tutorial covers the two cost levers: how context caching actually hits, and how to schedule around peak/off-peak pricing. For connection basics see our API quick reference, and for window strategy the 1M context guide.
Setup: Two Minutes
The API exposes an OpenAI-compatible endpoint, so the official openai SDK works with a swapped base_url:
from openai import OpenAI
client = OpenAI(api_key="YOUR_KEY", base_url="https://api.deepseek.com")
resp = client.chat.completions.create(
model="deepseek-flash",
messages=[
{"role": "system", "content": "You are a contract reviewer. Quote the source text."},
{"role": "user", "content": DOC_TEXT + "\n\nList all risky clauses with reasons."},
],
max_tokens=4000,
)
print(resp.choices[0].message.content)
DOC_TEXT can run to hundreds of thousands of tokens — anything under the 1M window. The first request bills entirely at cache-miss rates, and it is the single expensive call in the whole workflow.
Caching Rules: Only Exact Prefix Matches Hit
Disk caching is enabled by default for all users with no code changes, but the hit condition is stricter than intuition suggests: with the Sliding Window Attention mechanism, each cached prefix is an independent, complete unit, and a later request counts as a hit only when it fully matches a unit. Prefix units persist at three moments: request boundaries (one unit at the end of user input, one at the end of model output), common-prefix detection across requests, and fixed token intervals for long inputs/outputs. Example: a first request A+B followed by A+B+C hits the cache for A+B; a first request A+B followed by A+C matches no unit completely — even A misses. That dictates prompt assembly: stable instructions and the document go first, the changing question goes last. For follow-ups, reuse the same messages list and append.
Off-Peak Scheduling: Run the Heavy Jobs When It's Cheap
deepseek-flash pricing per million tokens: cache-hit input $0.003 off-peak / $0.006 peak; cache-miss input $0.15 / $0.30; output $0.60 / $1.20. Peak hours are only UTC 01:00-04:00 and 06:00-10:00, Monday through Friday (excluding Chinese public holidays) — everything else is off-peak, and the entire weekend bills at half price. Concentrating follow-up rounds off-peak halves output and miss costs; batch review jobs scheduled on weekends see substantial savings.
Putting It Together: What a Follow-Up Round Costs
Take a 300K-token document: the first full input plus 4,000 output tokens costs about $45 off-peak (300K × $0.15). Every follow-up round after that bills its input almost entirely at the $0.003 hit rate — effectively zero — leaving roughly $2.40 of output per round. The more rounds, the more the cache amortizes, which is why one ingestion with repeated follow-ups beats re-sending the document every round by orders of magnitude. A 2,500-request concurrency cap is ample for reviewing document sets in parallel. If quality demands are higher, compare tiers in our V4-Pro vs V4.1-Flash breakdown; for the first-run basics, revisit the Python quickstart.
大模型网摘