DeepSeek API access, context prefix caching saves 75% of costs and stable JSON extraction in practice
How can developers stably integrate DeepSeek into business systems? Master OpenAI compatible SDK calls, Prompt prefix cache hit techniques, and JSON Output zero-error parsing.
Run through the DeepSeek API in 5 minutes, master the prefix cache arrangement rules, reduce input costs by 75%+, and stably output structured JSON.
Core Commands & Key Techniques
4 itemsdeepseek-chat
Corresponding to the main V3 model, it supports Function Calling and JSON Output, with low latency and extremely cost-effectiveness.
deepseek-reasoner
Corresponding to the R1 reasoning model, the return field contains the reasoning_content thinking chain and final content.
response_format: json_object
Provide clear JSON Schema examples in the prompt words to prevent redundant explanation text from causing parsing crashes.
prompt_cache_hit_tokens
Check the response usage field to confirm whether the fixed rule prefix successfully hits the low-price cache.
High-frequency shortcut keys and practical skills
Tool and model selection comparison
| Tools/Patterns | Best applicable scenarios | Fees and methods | Practical suggestions |
|---|---|---|---|
| deepseek-chat + cache hit | Customer service classification, product marking, structured document storage | Extremely low (hit cache and save 75%+) | Fixed System Prompt + few sample examples to be placed first |
| deepseek-reasoner (R1) | Complex SQL generation, financial rule audit, algorithm derivation | Billed by thinking + output | Only `content` is taken from the business database, and `reasoning_content` is only kept for logs |
Comparison between inefficient questioning method and golden questioning method
System: Today is 2026-12-06 18:30, user ID 9921. Please convert the resume below into JSON.
Cause of the problem:The start time and user ID of each request are different, and the cache hit rate is 0%; and the field name is not defined, and the key name returned is erratic every time.
System (all requests are fixed): You are a resume structured extractor and must only output legal JSON: {"name":"","years":0,"skills":[],"city":""}, and fill in blank strings or 0 for missing fields. \nUser (dynamic content): [Paste candidate resume text]
Core advantages:System 100% hits the prefix cache, the field names and types are completely stable, and `json_decode` is directly stored in the database with zero error.
High Hit Cache JSON Extraction System Prompt Workshop
Step-by-Step Walkthrough
Reuse existing OpenAI SDK, only replace base_url and api_key
No need to learn a new library, just change `base_url` to `https://api.deepseek.com` for seamless migration.
Check usage.prompt_cache_hit_tokens to verify the saving effect
Send two consecutive requests with different User content but the same System, and observe the number of Tokens that hit the cache for the second one.
Frequently Asked Questions and One-Sentence Correction
| common phenomenon | Cause analysis | one sentence correction instruction | Actions |
|---|---|---|---|
| JSON parsing occasionally reports an error due to returning ```json package | `response_format: {"type":"json_object"}` is not passed and the code does not strip the fence |
The request body turns on `response_format={"type":"json_object"}`, and the first and last ```json fences are regularly stripped off in the parsing layer and then decoded.
|
Downloads & 3-Min Practice
5-minute actual test: Compare prompt_cache_hit_tokens of two requestsabout 5 minutes
Use a fixed 800-word System Prompt to continuously send two different business texts to verify the number of cache hits in the second response.