anomalyco / anomalyco/opencode

Reasoning from OpenAI-compatible providers that inline <think> tags leaks into text content

Open
#43,770 1 comment 0 reactions 1 assignee View on GitHub

@neriousy is already working on this.

Since Aug 21, 2026.

Dominant language
TypeScript
Stars
209k
Forks
27.5k
PR merge metrics
PR metrics pending

Description

Summary

For OpenAI-compatible providers whose models emit reasoning inline in content wrapped in <think>...</think> tags, opencode stores the raw tagged text verbatim as a text part. The UI then renders the model's chain-of-thought as regular assistant output instead of a collapsible thinking block.

Environment

  • opencode 1.18.19
  • Provider: custom OpenAI-compatible (poolside, endpoint https://inference.poolside.ai/v1)
  • Model: poolside/laguna-s-2.1
  • Model definition declares interleaved reasoning via interleaved: { field: "reasoning_content" }

Observed behavior

  • Many requests correctly populate the separate reasoning channel and are displayed as thinking.
  • Intermittently, a response arrives with the entire reasoning trace inline in content:
<long reasoning monologue ...></think><final answer>

opencode saves this whole string as one text part. The affected message reports reasoning: 0 tokens at message level, while the session total still shows accumulated reasoning tokens — so token accounting is also distorted (reasoning counted as output).

Expected behavior

Inline <think> blocks should be extracted into separate reasoning parts so they render as thinking, not as body text.

Suggested fix

Wrap affected provider/model instances with the AI SDK's extraction middleware, e.g.:

import { wrapLanguageModel, extractReasoningMiddleware } from 'ai'

const model = wrapLanguageModel({
  model: openaiCompatibleModel,
  middleware: extractReasoningMiddleware({ tagName: 'think' }),
})

Options:

  1. Apply extractReasoningMiddleware automatically for providers/models known to inline think tags (similar to how DeepSeek R1 is handled).
  2. Expose a per-model config flag (e.g. reasoning.extract: "think-tags") in the model/provider schema so users can enable it for any OpenAI-compatible endpoint.
  3. A safe fallback could also apply startWithReasoning: true semantics for models that omit the opening tag.

Impact

  • Users cannot review the agent's reasoning process (it appears as unreadable body text mixed with output).
  • Reasoning/output token attribution is wrong on affected messages.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.