DeepSeek-R1 & DeepSeek-V3.2 · 確定性 Agent 與評測 Harness

掌握 DeepSeek 深度推理與 DeepSeek Harness 工程實踐

提供經生產環境驗證的 DeepSeek-R1 <think> 推理鏈引導技巧、沙箱化 DeepSeek Harness 工具迴圈配置、節省 90% 成本的 KV 快取前綴固定法,以及自動化程式碼驗證門禁。

DeepSeek-R1 · 深度推理

零樣本 <think> 推理引導與不變量約束

DeepSeek-R1 在不帶冗長 Few-Shot 範例時表現最佳。直接給出明確的系統不變量、邊界條件與自檢清單,讓模型在 <think> 階段先行推演並排除競態條件。

[DeepSeek-R1 Reasoning Contract]
Goal: Fix the race condition in async connection pool checkout.
Invariants:
1. Never hold the pool mutex across an await boundary.
2. Preserve FIFO fairness for waiting coroutines on timeout cancellation.
3. In <think>, trace 2 concurrent cancellation interleavings first.
Output: Minimal unified diff + 1 deterministic reproduction pytest.
DeepSeek Harness · 核心配置

確定性 Harness 宣告式配置 (harness.yaml)

DeepSeek Harness 將模型推論封裝在隔離的 Git Worktree 與容器沙箱中,自動將編譯器與 pytest 錯誤輸出回注至推理迴圈,直至所有驗證門禁全數通過。

# deepseek-harness.yaml
model: deepseek-reasoner
max_turns: 8
temperature: 0.0
workspace:
  isolation: git-worktree
  timeout_sec: 120
verification_gates:
  - name: typecheck
    run: "mypy --strict src/"
  - name: unit_tests
    run: "pytest -q --maxfail=3"
on_failure: inject_stderr_and_retry
DeepSeek Harness · 工具迴圈

沙箱工具調用與死迴圈攔截器

透過對每次生成的 Patch 計算雜湊指紋並限制日誌上下文長度,防止 Agent 在同一錯誤假設上重複提交相同修改。

# Harness Tool-Loop Guard
def step_guard(history, patch_diff, exit_code):
    digest = sha256(patch_diff.encode()).hexdigest()
    if digest in history.seen_patches:
        return "ABORT_DUPLICATE_PATCH: Revise root-cause hypothesis in <think>."
    history.seen_patches.add(digest)
    return "PASS" if exit_code == 0 else "RETRY_WITH_TRUNCATED_STDERR" 
DeepSeek-V3.2 · KV 快取優化

前綴 KV 快取固定法(降低 90% 輸入成本)

DeepSeek API 支援自動硬碟 KV 快取。將專案目錄樹、工具 Schema 與 Harness 規範固定於 messages[] 開頭,僅在尾部追加增量測試輸出,即可穩定觸發快取命中。

messages = [
  {"role": "system", "content": STATIC_HARNESS_SPEC + FROZEN_REPO_MAP}, # 100% Hit ($0.028/1M)
  *prior_turn_messages,                                                 # Prefix KV-Cache
  {"role": "user", "content": latest_pytest_stderr_diff}                # Dynamic Tail Only
]
DeepSeek-V3.2 · 精準修改

原子化 SEARCH / REPLACE 差異修補區塊

對超過 200 行的檔案避免全量重寫。要求 Harness 輸出精確的 SEARCH/REPLACE 區塊,並在寫入磁碟前通過 AST 語法樹校驗。

<<<<<<< SEARCH
async def acquire(self, timeout: float = 5.0) -> Conn:
    return await asyncio.wait_for(self._queue.get(), timeout)
=======
async def acquire(self, timeout: float = 5.0) -> Conn:
    conn = await asyncio.wait_for(self._queue.get(), timeout)
    if conn.is_closed():
        return await self._create_replacement()
    return conn
>>>>>>> REPLACE
DeepSeek Harness · 自動化評測

多執行緒軌跡評分與回歸測試門禁

針對凍結的程式碼快照並行執行 pass@1 評測套件,自動統計 KV 快取命中率、推理Token 消耗與修補成功率。

$ deepseek-harness eval \
    --suite benchmarks/swe_verified_subset.jsonl \
    --model deepseek-reasoner \
    --workers 8 \
    --assert-cache-hit-rate 0.75 \
    --report-out reports/r1-eval-summary.json
DeepSeek-V3.2 · DeepSeek-R1 · KV-Cache Hit Optimization

DeepSeek 模型價格與前綴 KV 快取計費總覽

DeepSeek 官方模型靜態價格表(含快取命中 / 未命中單價)與 DeepSeek Harness 月度用量估算器。

預估月度消耗與建議部署方案 1,200 Credits / mo Pro ($10/mo)
模型名稱上下文視窗最大輸出 輸入 (快取命中)輸入 (快取未命中)輸出單價建議 Harness 用途
DeepSeek-V3.2 (deepseek-chat) 128K 8K $0.028 / 1M $0.27 / 1M $1.10 / 1M High-throughput coding, tool calling & prefix-cached harness loops
DeepSeek-R1 (deepseek-reasoner) 128K 32K (64K CoT) $0.14 / 1M $0.55 / 1M $2.19 / 1M Architecture verification, multi-step debugging & SWE-bench repair
DeepSeek-Coder-V2-Instruct 128K 8K $0.014 / 1M $0.14 / 1M $0.28 / 1M Repository FIM completion, AST refactoring & unit test generation
DeepSeek-R1-Distill-Qwen-32B 128K 16K Local / BYOK $0.08 / 1M $0.24 / 1M Low-latency local harness verification & deterministic CI gates