Skip to main content
AIDevOps
  • Learn
  • Learning Paths
  • Practice
  • Open Source
  • Books
  • Engineering

    AI DevOpsThe full map of building and operating AI servicesLLMOpsLLM deployment ยท evaluation ยท observabilityHands-on ProjectsBuild AI Agent projects

    Knowledge

    DocsTechnical documentationBlogEngineering articlesPloggerDevelopment log feed

    Validate

    Certification3-level skills certification ยท coming soon
AI Models
LlamaTutorial|MistralTutorial|GemmaTutorial|DeepSeekTutorial|QwenTutorial
๐Ÿง  AI Core
AI Intro & RoadmapML FundamentalsLLM Fundamentals|Python AIC++|PyTorchTensorFlowJAX
๐Ÿค– AI Applied Development
Applied AI Intro & RoadmapHugging FaceLangChainLlamaIndexLLMOps|LangGraphMCPMulti-AgentAgent Evaluation
๐Ÿง  AI Agent Development
Finance AI AgentLLM API ServerStock Investing AgentAIOps AI AgentEducation AI AgentCoding AI Agent
๐ŸŒฑ Spring Cloud
Spring Intro & RoadmapSpring Cloud GatewaySpring BootJava|Spring AISpring SecuritySpring BatchSpring JPA
๐Ÿณ DevOps
DevOps Intro & RoadmapLinuxDockerCI/CD|Kubernetes BasicsK8s AdvancedPrometheusGrafana
๐Ÿงฑ Infrastructure
Infrastructure Intro & RoadmapNginxRedis
โ˜๏ธ Cloud
Cloud Intro & RoadmapAWSGCPAzureNCPCloudflare
๐ŸŽจ Frontend
Frontend Intro & RoadmapJavaScriptTypeScript|ReactNext.js|VueNuxt|Electron
๐Ÿ“ฑ Mobile
Mobile Intro & RoadmapKotlinAndroidFlutter
โš™๏ธ Backend
Backend Intro & RoadmapPython BasicsFastAPIDjangoFlask|CGoGinNode.js
๐Ÿ’พ Database
DB Intro & RoadmapCore SQLOracleMySQLPostgreSQL|MongoDBVector DB
๐Ÿงช Testing
k6JMeternGrinder
AIDevOps

Engineering AI. From Code to Production.
An engineering learning platform for building and operating AI and AI Agents

Learn

  • All Guides
  • Learning Paths
  • AI Tutorial
  • Practice
  • Books

Resources

  • AI DevOps
  • LLMOps
  • Hands-on Projects
  • Docs
  • Blog
  • Plogger
  • Open Source
  • Certification (coming soon)

Start Here

  • AI Core Roadmap
  • AI Applied Development Roadmap
  • Spring Cloud Roadmap
  • DevOps Roadmap
  • Infrastructure Roadmap

ย 

  • Cloud Roadmap
  • Frontend Roadmap
  • Mobile Roadmap
  • Backend Roadmap
  • Database Roadmap
ยฉ 2026 AIDevOps. All rights reserved.
Terms of ServicePrivacy PolicySitemaptestforge.kr
Reasoning & Low-Cost LLM API Guide

๐Ÿง  DeepSeek Complete Guide

Visitors

DeepSeek is a Chinese AI company known for MIT-licensed open weights and very low API prices. This guide covers using the 1M-token-context V4.1-Flash and V4-Pro through the OpenAI-compatible API, the rules for thinking mode and tool calls, cutting cost with automatic context caching, and running R1 distills locally โ€” in the order you would use them.

โ† Back to AIDevOps home
๐Ÿง 

DeepSeek Practical Examples

Examples of a Think-tag parser, math problem solving, a code-debugging agent and a reasoning API.

View practical examples โ†’

Contents

0 / 12
  1. DeepSeek at a glance
  2. Model lineup & how to choose
  3. Migrating from deepseek-chat/reasoner
  4. Pricing and context caching
  5. API basics
  6. Thinking mode
  7. Tool calls and reasoning_content
  8. JSON output
  9. Running locally (R1 distills)
  10. Self-hosting V4
  11. Troubleshooting
  12. FAQ
Contents 12 sections
  1. DeepSeek at a glance
  2. Model lineup & how to choose
  3. Migrating from deepseek-chat/reasoner
  4. Pricing and context caching
  5. API basics
  6. Thinking mode
  7. Tool calls and reasoning_content
  8. JSON output
  9. Running locally (R1 distills)
  10. Self-hosting V4
  11. Troubleshooting
  12. FAQ

DeepSeek at a glance

DeepSeek showed with V3 and the R1 reasoning model that top-tier performance does not need top-tier cost. It now releases the V4 family as MIT-licensed open weights and offers the same models through a very cheap OpenAI-compatible API.

WhenWhat changed
Aug-Dec 2025V3.1, V3.2 โ€” deepseek-chat/deepseek-reasoner became the normal and thinking modes of one model; tool calls supported in thinking mode
April 2026V4 (Pro, Flash) announced with 1M-token context; retirement of the old model names announced
July-August 2026V4-Flash and V4-Pro generally available; deepseek-chat and deepseek-reasoner retired (July 24)
September 2026V4.1-Flash โ€” native image understanding; API name deepseek-flash
๐Ÿ’ก

To build an agent that puts long logs and runbooks into a 1M-token context, cuts cost with caching and analyzes incidents, see the DeepSeek incident log-analysis agent tutorial.

Model lineup & how to choose

ModelAPI nameContext / max outputInputLicenseBest for
DeepSeek-V4.1-Flashdeepseek-flash1M / 384KText + imageMITThe default โ€” fast and cheapest; JSON, tools and FIM
DeepSeek-V4-Prodeepseek-v4-pro1M / 384KTextMITHard reasoning, coding and agent work
DeepSeek-R1 distillsโ€” (local)VariesTextMITRunning a reasoning model on a PC (Ollama deepseek-r1)
DeepSeek-OCRโ€” (local)โ€”ImageMITDocument images โ†’ text (Ollama deepseek-ocr, 3B)

The V4 family are MoE (mixture-of-experts) models with hundreds of billions to over a trillion parameters, computing only some experts per token. That keeps API prices low, but serving them yourself needs GPU server nodes.

Migrating from deepseek-chat and deepseek-reasoner

deepseek-chat and deepseek-reasoner were retired on July 24, 2026. Now there is one model (deepseek-flash) and you turn thinking mode on or off per request.

Old codeNew code
model="deepseek-chat"model="deepseek-flash" + extra_body={"thinking": {"type": "disabled"}}
model="deepseek-reasoner"model="deepseek-flash" + extra_body={"thinking": {"type": "enabled"}}
When you need stronger reasoningmodel="deepseek-v4-pro" + reasoning_effort="high"
โš ๏ธ

Thinking mode is on by default. If you expected the fast responses of the old deepseek-chat, always set "disabled"; otherwise responses get slower and output token costs rise.

Pricing and context caching

Model (per 1M tokens, peak)Input โ€” cache hitInput โ€” cache missOutput
deepseek-flash$0.006$0.30$1.20
deepseek-v4-pro$0.044$1.32$3.96

Off-peak hours cost half of the above. Peak hours are weekdays 01:00-04:00 and 06:00-10:00 UTC. Prices are from the official pricing page as of October 2026 and may change.

Context caching applies to every request automatically. When the beginning of a request exactly matches an earlier one, that part is billed at the cache-hit price (about 1/50 of a miss), so keep system prompts and long documents at the front and append only the changing question.

cache_check.pyPYTHON
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")
runbook = open("runbook.md", encoding="utf-8").read()     # long document โ€” keep it unchanged at the front of every request

for question in ["What is the disk alert procedure?", "What is the DB failover order?"]:
    resp = client.chat.completions.create(
        model="deepseek-flash",
        messages=[
            {"role": "system", "content": "Answer only from the following runbook.\n\n" + runbook},
            {"role": "user", "content": question},
        ],
        extra_body={"thinking": {"type": "disabled"}},
    )
    u = resp.usage
    # From the second request on, the runbook part hits the cache
    print(question, "| cache hit", getattr(u, "prompt_cache_hit_tokens", 0), "/ miss", getattr(u, "prompt_cache_miss_tokens", 0))

API basics

The DeepSeek API is OpenAI-compatible, so use the OpenAI Python SDK with a different base_url. Get a key from the DeepSeek Platform.

BASH
uv add openai
export DEEPSEEK_API_KEY="your_key"
deepseek_api.pyPYTHON
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")

resp = client.chat.completions.create(
    model="deepseek-flash",
    messages=[
        {"role": "system", "content": "You are an SRE who answers concisely."},
        {"role": "user", "content": "Explain the difference between 502 and 504 errors"},
    ],
    extra_body={"thinking": {"type": "disabled"}},   # fast normal response
    stream=True,
)
for chunk in resp:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
print()

Thinking mode

With thinking on, the model generates its reasoning before answering, returned separately in a reasoning_content field at the same level as content. Choose the depth with reasoning_effort: low, high or max (default high).

thinking.pyPYTHON
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")

resp = client.chat.completions.create(
    model="deepseek-flash",
    messages=[{"role": "user", "content": "How should I tune autovacuum for PostgreSQL taking 3,000 writes per second?"}],
    reasoning_effort="high",
    extra_body={"thinking": {"type": "enabled"}},
)
msg = resp.choices[0].message
reasoning = getattr(msg, "reasoning_content", None)   # a DeepSeek extension field, not part of the OpenAI SDK
print("[reasoning]", (reasoning or "")[:300], "...")
print("[answer]", msg.content)
print("output tokens:", resp.usage.completion_tokens)
โ„น๏ธ

Thinking mode ignores temperature, presence_penalty and frequency_penalty. In conversations without tools, you do not need to send earlier turnsโ€™ reasoning_content back (it is ignored if you do).

Tool calls and reasoning_content

Tools work in thinking mode too. But in conversations that use tools, you must send back the reasoning_content of every previous turn, including turns without a tool call. Storing the whole response message with model_dump() keeps that field.

tools.pyPYTHON
import json
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")

def get_error_rate(service: str, minutes: int = 15) -> dict:
    """Recent error rate for a service (query Prometheus in a real implementation)"""
    return {"service": service, "minutes": minutes, "error_rate": 0.083, "p95_ms": 1840}

TOOLS = {"get_error_rate": get_error_rate}
TOOL_SCHEMAS = [{"type": "function", "function": {
    "name": "get_error_rate",
    "description": "Get a service's recent error rate and p95 latency",
    "parameters": {"type": "object", "properties": {
        "service": {"type": "string"}, "minutes": {"type": "integer"}}, "required": ["service"]},
}}]

messages = [{"role": "user", "content": "Is the checkout service healthy right now?"}]
for _ in range(5):
    resp = client.chat.completions.create(
        model="deepseek-flash", messages=messages, tools=TOOL_SCHEMAS,
        extra_body={"thinking": {"type": "enabled"}},
    )
    msg = resp.choices[0].message
    messages.append(msg.model_dump(exclude_none=True))   # keeps reasoning_content as well
    if not msg.tool_calls:
        print(msg.content)
        break
    for tc in msg.tool_calls:
        result = TOOLS[tc.function.name](**json.loads(tc.function.arguments))
        messages.append({"role": "tool", "tool_call_id": tc.id, "content": json.dumps(result)})
โš ๏ธ

If you store only some fields, as in messages.append({"role": "assistant", "content": msg.content}), reasoning_content and tool_calls are lost and the next request fails or the reasoning breaks.

JSON output

Use response_format={"type": "json_object"}, and always include the word "json" and an example of the format in the prompt.

json_output.pyPYTHON
import json
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")

SYSTEM = """Extract details from an incident report and answer only in json.
Example format: {"service": "checkout", "symptom": "rising 5xx", "started_at": "2026-10-04 09:12", "suspected_cause": "deploy"}"""

resp = client.chat.completions.create(
    model="deepseek-flash",
    messages=[{"role": "system", "content": SYSTEM},
              {"role": "user", "content": "500 errors on checkout started rising right after the 09:12 deploy"}],
    response_format={"type": "json_object"},
    max_tokens=500,                                   # enough that the JSON is not cut off
    extra_body={"thinking": {"type": "disabled"}},
)
content = resp.choices[0].message.content
data = json.loads(content) if content else {}         # empty responses happen occasionally โ€” retry
print(data)

Running locally (R1 distills)

V4 is too large for a personal PC, so locally use deepseek-r1, which distills R1โ€™s reasoning into smaller Qwen- and Llama-family models. It helps when sensitive logs cannot leave your machine.

BASH
ollama pull deepseek-r1:8b      # 8 GB GPU class
ollama pull deepseek-r1:14b     # 16 GB GPU class
ollama pull deepseek-r1:32b     # 24 GB GPU class
local_r1.pyPYTHON
from ollama import chat

resp = chat(
    model="deepseek-r1:8b",
    messages=[{"role": "user", "content": "Guess the cause of this stack trace: NullPointerException at OrderService.java:118"}],
    think=True,
)
print("[thinking]", (resp.message.thinking or "")[:300], "...")
print("[answer]", resp.message.content)
โ„น๏ธ

Distills have less knowledge and weaker reasoning than V4. A practical setup is first-pass analysis with the local model, sending only summaries that are allowed to leave to the large API model.

Self-hosting V4

Companies that cannot send data to an external API can serve the MIT-licensed weights themselves. V4.1-Flash lists vLLM and SGLang as recommended engines, and the official model card recommends a single node of 4ร— GB300 for V4-Pro.

BASH
vllm serve deepseek-ai/DeepSeek-V4.1-Flash   # add tensor/expert parallel options for your GPU setup
๐Ÿ’ก

Self-hosting costs a lot in GPUs and operations staff. With API prices this low for the same model, start with the API and check your usage first, unless data policy forbids it.

Troubleshooting

SymptomCauseFix
Model Not Exist / 400 errorUsing the retired deepseek-chat or deepseek-reasonerdeepseek-flash + set thinking
Slower and more expensive than beforeThinking mode is on by defaultUse {"type": "disabled"} for simple requests
Tool conversations fail midwayEarlier turnsโ€™ reasoning_content is missingStore the whole response message with model_dump()
Empty JSON responseA known intermittent issue, or too few max_tokensRetry on empty content; give max_tokens room
Few cache hitsThe start of each request changes (time, request ID, etc.)Move changing values toward the end of the prompt
Changing temperature changes nothingThinking mode does not support temperatureAdjust with reasoning_effort

Frequently asked questions

The deepseek-chat model no longer works.

deepseek-chat and deepseek-reasoner were retired on July 24, 2026. Replace deepseek-chat with deepseek-flash with thinking disabled, and deepseek-reasoner with deepseek-flash with thinking enabled. Set thinking with extra_body={"thinking": {"type": "enabled"}} or "disabled".

How much does the DeepSeek API cost?

As of October 2026, deepseek-flash at peak hours costs $0.30 per million input tokens ($0.006 on a cache hit) and $1.20 per million output tokens, and off-peak hours are half price. Prices change often, so check the official pricing page before calculating real costs.

Do I need to configure DeepSeek context caching?

No. Disk-based context caching is on by default for every user, with no code changes. Keep the same system prompt and documents at the front and change only the question at the end, and that prefix hits the cache and cuts input cost sharply; check the hit volume in usage.prompt_cache_hit_tokens.

Can I run DeepSeek V4 on my own computer?

V4.1-Flash and V4-Pro weights are public under the MIT license, but they are MoE models with hundreds of billions to over a trillion parameters and need GPU server nodes. On a personal PC, the realistic option is deepseek-r1 (1.5B-70B) via Ollama, which distills R1โ€™s reasoning into smaller models.

Changing temperature in thinking mode does nothing.

Thinking mode does not support temperature, presence_penalty or frequency_penalty. Sending them causes no error, but they have no effect. To adjust depth, choose reasoning_effort low, high or max.