Full site navigation

AI model sub-channel
AI product discovery
Ranking breakdown
Language · English
Chapter 3 Intermediate ~8 min Verified 2026-12-06

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.

What You’ll Learn

Run through the DeepSeek API in 5 minutes, master the prefix cache arrangement rules, reduce input costs by 75%+, and stably output structured JSON.

Prerequisites
Latest Update DeepSeek automatic context hard disk caching does not require additional code switches. As long as the System and fixed context prefix bytes are kept consistent, the input price of the hit part is directly as low as about 1/10 of the original price.
01

Core Commands & Key Techniques

4 items
deepseek-chat
High concurrency general/JSON extraction

Corresponding to the main V3 model, it supports Function Calling and JSON Output, with low latency and extremely cost-effectiveness.

deepseek-reasoner
Complex Reasoning/Strategic Planning

Corresponding to the R1 reasoning model, the return field contains the reasoning_content thinking chain and final content.

response_format: json_object
Force structured output

Provide clear JSON Schema examples in the prompt words to prevent redundant explanation text from causing parsing crashes.

prompt_cache_hit_tokens
Verify cache hit rate

Check the response usage field to confirm whether the fixed rule prefix successfully hits the low-price cache.

02

High-frequency shortcut keys and practical skills

Must memorize shortcut keys
OpenAI SDK One Line Everything DeepSeek base_url="https://api.deepseek.com"
Handy and practical tips
01
Put the dynamic timestamp at the end of User and don’t put it at the beginning of System If you write "current time 14:32:05" in the first line of System, the prefix will change every time the request is made, resulting in 0% cache hits! Fixed rules are placed first and dynamic variables are placed last.
02
Set reasonable max_tokens and timeout retry The inference model thinking chain is long. It is recommended to set the HTTP timeout to 90~120 seconds and enable streaming `stream: true` to prevent gateway 504 disconnection.
03
Temperature is divided by scene Use `temperature=0.0` for data extraction and code generation, `0.7` for general Q&A, and `1.2` for creative copywriting.
03

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
04

Comparison between inefficient questioning method and golden questioning method

✕ Broken prefix cache + no JSON example given

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.

✓ Static Schema is placed first + dynamic input is placed User

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.

05

High Hit Cache JSON Extraction System Prompt Workshop

Modify parameters and automatically assemble, copy and use with one click
Live Preview
You are a rigorous [{{biz}}] structured extraction engine. Rules: 1) Strictly only output a single legal JSON object, and wrapping Markdown code blocks or any additional explanations is prohibited; 2) The output structure must match exactly: {{schema}}; 3) Fields not mentioned in the original text retain their default values, and subjective guessing is prohibited.
06

Step-by-Step Walkthrough

0 / 2 steps done
01

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.

Expected Output Complete business code docking in 3 minutes.
Practical Tips Enabling `stream: true` can not only improve the user's first-word experience, but also avoid long-thinking triggering agent timeout.
02

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.

Expected Output The cost of batch processing long documents/classification tasks is directly cut by half.
Practical Tips Long background information (such as a 5,000-word product manual) can also be put into a fixed prefix for repeated questions.
07

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.
08

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.

Use response_format=json_object to continuously extract the sentiment polarity and question labels of 2 user comments.
验收标准:The 2nd request `prompt_cache_hit_tokens > 0` and returns 100% valid JSON.

Security verification

Please complete the verification before proceeding.