MiMo-Code icon indicating copy to clipboard operation
MiMo-Code copied to clipboard

openai-compatible provider: DeepSeek thinking mode intermittent 400 reasoning_content must be passed back

Open NMDX0721 opened this issue 3 months ago • 2 comments

Summary

Intermittent HTTP 400 on DeepSeek V4 thinking-mode models (deepseek-v4-flash) via @ai-sdk/openai-compatible (token.sensenova.cn):

If thinking mode and tool_calls, `reasoning_content` must be passed back to the API.

(400001, non-retryable)

Root cause (confirmed by deterministic gateway replay — see final comment)

The error only appears on long sessions that have grown to ~860K+ tokens / multi-MB request bodies, near the gateway's context ceiling for thinking-mode models. At that size the gateway strictly validates thinking + tools requests and rejects assistant messages whose reasoning_content was stripped to "" by MiMoCode's context compaction/persistence.

Proof: replaying the exact failing body (1210 msgs) gives 400; reducing its size (drop first 10 msgs, shorten system, truncate reasoning) flips it to 200. Accepted tail-1200 variant measured 861,239 prompt tokens.

Earlier hypotheses in this thread (setCacheKey/promptCacheKey, missing interleaved) were tested against the live gateway and disproven.

What MiMoCode should fix

  1. Preserve (not blank) reasoning_content when compacting/persisting assistant history for reasoning models.
  2. Compact sessions well before they approach the gateway's strict-validation ceiling (observed failing at ~861K tokens while config says "compacts at 960K").
  3. Detect context-near-limit and compact proactively instead of letting the gateway 400.

User workaround

Start a new session (or force context compaction) once a session grows large.

Environment

  • MiMoCode 0.1.13, Windows
  • @ai-sdk/openai-compatible, baseURL https://token.sensenova.cn/v1
  • deepseek-v4-flash (thinking on by default; deepseek-v4-flash-0718 alias affected identically)

Related upstream

  • opencode #24104 / #24114 / #24124 (same gateway contract), PR #24146 (interleaved-only), #24150 (closed), #24218 (open)
  • DeepSeek thinking-mode tool-call doc: https://api-docs.deepseek.com/guides/thinking_mode#tool-calls

NMDX0721 avatar Aug 28 '26 11:08 NMDX0721

Root cause confirmed (update)

Further investigation with a direct gateway experiment conclusively identified the trigger:

The setCacheKey: true provider option is the trigger.

Test evidence (against token.sensenova.cn):

  • Replaying the exact failing request body (1156 messages, many assistant tool-call turns with reasoning_content: "") as-is (with promptCacheKey) → HTTP 400 "If thinking mode and tool_calls..."
  • Replaying the same body with promptCacheKey removed → HTTP 200 ✅

So the flow is:

  1. setCacheKey: true makes every request carry promptCacheKey: <session-id>.
  2. The SenseNova gateway uses that key to cache/verify the conversation. When the key is present, it validates that every historical assistant message's reasoning_content matches the originally generated content.
  3. MiMoCode's context management (compaction/checkpointing/storage) stores many older assistant messages with reasoning_content stripped to "" (observed 33 of 275 tool-call turns empty in the failing body).
  4. Cache-key validation then fails on those emptied turns → 400, non-retryable.

This also explains the intermittency: short sessions keep reasoning intact, so the cache validation passes; once the session grows and history gets compacted/checkpointed, emptied reasoning_content triggers the 400.

Workaround (confirmed working)

Remove "setCacheKey": true from the provider options (or set it to false). Requests then no longer carry promptCacheKey, the gateway skips the reasoning_content cache comparison, and the 400s stop.

What should be fixed in MiMoCode

Two angles:

  1. Don't strip reasoning_content to "" when persisting/compacting assistant history for models with reasoning enabled (the value loss in storage is the underlying data bug).
  2. If setCacheKey/promptCacheKey is enabled, ensure the cache key is invalidated or the reasoning_content is fully preserved — otherwise the client sends a cache key that no longer matches its own message payload.

NMDX0721 avatar Aug 28 '26 16:08 NMDX0721

FINAL root cause — confirmed by binary-search replay (supersedes earlier theories)

Earlier comments guessed setCacheKey/promptCacheKey was the trigger. That was wrong — a direct gateway replay experiment disproved it. The real trigger is now nailed down with deterministic binary-search replay against token.sensenova.cn.

Reproducible evidence (same failing request body, only message count varies)

The failing request body (1210 messages, ~3.18 MB) was extracted from the log and replayed with controlled edits:

Request variant Prompt tokens Result
FULL (1210 msgs) n/a (rejected) HTTP 400 reasoning_content
tail 1200 (drop first 10 msgs) 861,239 HTTP 200 ✅
drop first 5 / first 9 msgs — HTTP 200 ✅
FULL + shorten system message smaller HTTP 200 ✅
FULL + truncate every reasoning_content to 100 chars smaller HTTP 200 ✅

Every variant that merely reduces request-body size flips 400 → 200. The prompt-token count of the accepted tail-1200 variant is 861,239 — i.e. the failing session is already sitting at ~860K+ tokens, right at the gateway's effective context ceiling for this thinking-mode model.

What is actually happening

  1. The affected session grew to ~860K+ tokens / 3.2 MB per request (a very long-lived Compose session with hundreds of tool-call turns).
  2. MiMoCode's context management (compaction / persistence) stores many older assistant messages with reasoning_content stripped to "" (33 of 275 tool-call turns in the failing body).
  3. When a request is already near the context window limit, the DeepSeek/SenseNova gateway enforces strict validation on thinking + tools requests: every assistant message's reasoning_content must be present and consistent. The emptied values then trip the 400001 "reasoning_content must be passed back" error.
  4. Any reduction in total token count (drop a few messages, shorten system, truncate reasoning) drops the request below the gate and it succeeds.

This explains every symptom:

  • Intermittent / long-session-only: short sessions are far below the ceiling and never validated strictly.
  • Not fixed by interleaved — the field is already present; the problem is value loss + request size.
  • Not fixed by removing promptCacheKey — the earlier 200 was a coincidence of a smaller historical body; today's logs (no promptCacheKey) still show the 400.
  • New/short sessions are unaffected.

What should be fixed in MiMoCode

  1. Do not strip reasoning_content to "" when compacting/persisting assistant history for reasoning models — preserve it, or drop the messages entirely rather than sending empty reasoning under a tools request.
  2. Trigger context compaction well below the window limit for thinking-mode models, so a session never approaches the gateway's strict-validation ceiling. (Model shows "compacts at 960K" for a 1M window — too late; the 861K observed request was still failing.)
  3. Surface the real cause: the gateway's 400 message is misleading; MiMoCode should detect "context near limit" and compact before it hits this.

Immediate user workaround

Start a new session (or force context compaction) once a session grows large. Continuing a 860K+ token session will keep hitting this 400.

NMDX0721 avatar Aug 29 '26 06:08 NMDX0721