openai-compatible provider: DeepSeek thinking mode intermittent 400 reasoning_content must be passed back
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
- Preserve (not blank)
reasoning_contentwhen compacting/persisting assistant history for reasoning models. - Compact sessions well before they approach the gateway's strict-validation ceiling (observed failing at ~861K tokens while config says "compacts at 960K").
- 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, baseURLhttps://token.sensenova.cn/v1deepseek-v4-flash(thinking on by default;deepseek-v4-flash-0718alias 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
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 (withpromptCacheKey) → HTTP 400 "If thinking mode and tool_calls..." - Replaying the same body with
promptCacheKeyremoved → HTTP 200 ✅
So the flow is:
setCacheKey: truemakes every request carrypromptCacheKey: <session-id>.- 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_contentmatches the originally generated content. - MiMoCode's context management (compaction/checkpointing/storage) stores many older assistant messages with
reasoning_contentstripped to""(observed 33 of 275 tool-call turns empty in the failing body). - 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:
- Don't strip
reasoning_contentto""when persisting/compacting assistant history for models with reasoning enabled (the value loss in storage is the underlying data bug). - If
setCacheKey/promptCacheKeyis 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.
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
- The affected session grew to ~860K+ tokens / 3.2 MB per request (a very long-lived Compose session with hundreds of tool-call turns).
- MiMoCode's context management (compaction / persistence) stores many older assistant messages with
reasoning_contentstripped to""(33 of 275 tool-call turns in the failing body). - When a request is already near the context window limit, the DeepSeek/SenseNova gateway enforces strict validation on
thinking + toolsrequests: every assistant message'sreasoning_contentmust be present and consistent. The emptied values then trip the400001"reasoning_content must be passed back" error. - 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
- Do not strip
reasoning_contentto""when compacting/persisting assistant history for reasoning models — preserve it, or drop the messages entirely rather than sending empty reasoning under atoolsrequest. - 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.)
- 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.