DeepSeek-R1 & DeepSeek-V3.2 · Deterministic Agent & Eval Harness

Master DeepSeek Reasoning & Build Reliable DeepSeek Harnesses

Production-tested prompt patterns for DeepSeek-R1 <think> trajectories, sandboxed DeepSeek Harness tool loops, 90% KV-cache prefix optimization, and automated SWE-bench verification gates.

DeepSeek-R1 · Reasoning

Zero-Shot <think> Steering & Invariant Constraints

DeepSeek-R1 performs best without verbose few-shot examples. Supply explicit system invariants, boundary conditions, and a verification checklist so its <think> trajectory self-audits before emitting code.

[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 · Core

Deterministic Harness Config (harness.yaml)

A DeepSeek Harness wraps model inference in an isolated git worktree and container sandbox, feeding compiler and pytest stderr back into the loop until all verification gates pass.

# 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 · Agent Loop

Sandboxed Tool-Use & Loop-Detection Harness

Prevent infinite agent retry loops in DeepSeek-V3.2 by enforcing state-hash deduplication and bounding tool output to the top and bottom 60 log lines.

# 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-Cache

90% Cost Reduction via Prefix KV-Cache Pinning

DeepSeek automatically caches shared prompt prefixes on disk. Keep repository maps, tool schemas, and harness instructions strictly static at the start of messages[] and append dynamic diffs only at the tail.

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 · Code Editing

Atomic SEARCH / REPLACE Diff Blocks

Avoid full-file rewrites on files over 200 lines. Instruct the harness to emit exact SEARCH/REPLACE blocks validated by an AST parser before disk write.

<<<<<<< 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 · Evaluation

Parallel Trajectory Grading & Regression Gate

Run automated pass@1 regression suites across frozen repository snapshots and record KV-cache hit ratio, reasoning token budget, and patch cleanliness.

$ 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 Model Pricing & Prefix KV-Cache Reference

Static reference pricing for DeepSeek official API models and interactive monthly token & KV-cache cost estimator for DeepSeek Harness workloads.

Estimated Monthly Token Units & Recommended Setup 1,200 Credits / mo Pro ($10/mo)
ModelContextMax Output Input (Cache Hit)Input (Cache Miss)OutputRecommended Harness Role
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