DeepSeek V4 + Claude Code thinking mode 400 错误修复方案

DeepSeek V4 + Claude Code thinking mode 400 错误修复方案

DeepSeek V4 + Claude Code thinking mode 400 错误修复方案

问题描述

使用 DeepSeek V4 Pro / V4 Flash 的 thinking mode + tool calls 时,第一轮工具调用后的每次请求都返回 400 错误:

复制代码

API Error: 400 The content[].thinking in the thinking mode must be passed back to the API.

根因

复制代码

Claude Code → DeepSeek API(返回 reasoning_content / thinking 块)

↓

Claude Code 保存对话历史时丢弃非标准的 thinking 字段

↓

下一轮请求 → DeepSeek API:检测到 assistant 消息缺少 thinking 块 → 400

DeepSeek V4 要求 multi-turn 对话中,每个 role: assistant 的消息必须保留 thinking 块。Claude Code / cc-switch 在序列化历史时把它丢弃了。

解决方案

在 Claude Code 和 DeepSeek API 之间插入一个本地 Node.js 代理,自动补全被丢弃的 thinking 字段。

拓扑

复制代码

Claude Code → cc-switch → 本地代理 (127.0.0.1:15722) → api.deepseek.com

↑

注入 thinking 块

步骤

  1. 创建代理脚本 ds-proxy.js
    js
    复制代码
    const http = require('http');
    const { Readable } = require('stream');

const DEEPSEEK_BASE = 'https://api.deepseek.com';

const PORT = 15722;

function fixMessages(data) {

if (!data.messages && !Array.isArray(data.messages)) return;

for (const msg of data.messages) {

if (msg.role !== 'assistant') continue;

if (Array.isArray(msg.content)) {

const hasThinking = msg.content.some(b => b.type === 'thinking');

if (!hasThinking) {

msg.content.unshift({ type: 'thinking', thinking: '' });

}

} else {

msg.reasoning_content = msg.reasoning_content || '';

}

}

}

const server = http.createServer(async (req, res) => {

console.log(${req.method} ${req.url});

res.setHeader('Access-Control-Allow-Origin', '*');

复制代码
if (req.method === 'OPTIONS') {
    res.writeHead(204);
    res.end();
    return;
}

if (req.method === 'GET') {
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({ id: 'root', object: 'list' }));
    return;
}

if (req.method !== 'POST') {
    res.writeHead(404);
    res.end('Not Found');
    return;
}

let body = '';
req.on('data', chunk => body += chunk);
req.on('end', async () => {
    try {
        const data = JSON.parse(body);
        fixMessages(data);

        const target = DEEPSEEK_BASE + req.url;
        console.log('->', target);

        const apiRes = await fetch(target, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': req.headers.authorization,
                'anthropic-version': req.headers['anthropic-version'] || ''
            },
            body: JSON.stringify(data)
        });

        console.log('<-', apiRes.status);
        const headers = {};
        apiRes.headers.forEach((v, k) => { headers[k] = v; });
        res.writeHead(apiRes.status, headers);

        if (apiRes.body) {
            Readable.fromWeb(apiRes.body).pipe(res);
        } else {
            res.end();
        }
    } catch (err) {
        console.error('Proxy error:', err.message);
        res.writeHead(500);
        res.end(JSON.stringify({ error: 'Proxy Error: ' + err.message }));
    }
});

});

server.listen(PORT, () => {

console.log('DS4 Proxy on http://127.0.0.1:' + PORT);

});

  1. 启动代理

powershell

复制代码

node ds-proxy.js

保持在后台运行。

  1. 配置 cc-switch
    DeepSeek provider 的 Base URL 设为:

复制代码

http://127.0.0.1:15722/anthropic

或跳过 cc-switch,直连代理:

powershell

复制代码

$env:ANTHROPIC_BASE_URL = "http://127.0.0.1:15722/anthropic"

$env:ANTHROPIC_API_KEY = "你的DeepSeek API Key"

claude

  1. 重启 Claude Code

代理处理逻辑

场景 操作

Anthropic 格式(content 为数组) 在 content 最前面插入 {type:"thinking",thinking:""}

OpenAI 格式 注入 reasoning_content: ""

连通性检测(GET) 返回 {"id":"root","object":"list"} 模拟 API

相关推荐
圆圆讲门店2 分钟前
挑选同城获客服务机构时需要考量的核心因素都有哪些?
大数据·网络·人工智能·python
揽秀亭长3 分钟前
论文降AI率有哪些方法?论文修改流程详解
人工智能·深度学习
爱上纯净的蓝天6 分钟前
只输出选项和概率的模型:判别层的接口契约与阈值工程
人工智能·大模型·llm·模型评估·架构设计
@呱呱爱学习9 分钟前
2026年09月28日 | 呱呱AI日报】GitHub 发布 Copilot WSL 教程,介绍在 Ubuntu 中运行编程 Agent
人工智能·github·copilot
lie..16 分钟前
30天从零开始学AI应用开发(Day 12):像产品一样稳:流式输出、报错重试、花钱控制
开发语言·人工智能·php
镜象科技35 分钟前
AI临床心理大模型:离临床最近的AI长什么样?
人工智能
zhanghaha131435 分钟前
AI Agent_4 Agent工作原理详解:从“会聊天“到“会办事“
人工智能
DeepAgent1 小时前
AI Agent 工程实践(49):一次真实优化——从 Agent v1 到 v2
开发语言·人工智能·agent
小蒋观天下1 小时前
两轮车检测AI摄像头——2026-2030年未来市场规模、增长逻辑与行业天花板
大数据·人工智能·安全·计算机视觉·ai大模型
美狐美颜sdk1 小时前
直播APP接入美颜SDK后出现卡顿怎么办?从性能瓶颈寻找解决方案
android·人工智能·音视频·美颜sdk·直播美颜sdk