Anthropic: preserve visible output when thinking consumes a tiny remaining budget

Aperta
#1,136 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
c
Ambito
ai, api, backend

Direzione di ricerca

Start in the server's Anthropic /v1/messages handling and trace the existing decoder, batched decode path, model syntax prompts, and stream state. Run ./ds4_test --server and use the listed threshold, recovery, suffix-failure, and OpenAI-compatibility cases to define coverage. Done means the Anthropic near-limit request returns visible text while the stated safety constraints and normal reasoning behavior remain intact.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Problem

For Anthropic /v1/messages requests on reasoning models, ds4 clamps max_tokens to the remaining context room. When only a small budget remains (for example 180 tokens), the model can spend the whole budget inside <think> and return no visible text. Claude CLI automatic compaction then reports an empty summary instead of recovering.

The current Anthropic overflow error also does not use the wording Anthropic clients recognise for a truncated-history retry.

Proposed behavior

Add --anthropic-thinking-min-budget N with default 1024; 0 disables it.

On the first decode pass only, when all of these hold:

  • endpoint is Anthropic
  • reasoning mode is active
  • real decode budget is <= N
  • no recovery completion or DSML recovery is active
  • the stream is untouched

append the model syntax's think-close suffix tokens to the live KV state, update thinking and Anthropic stream state, then decode visible text. Derive the suffix from the model syntax's thinking and non-thinking generation prompts; do not hand-write model-specific token ids.

Also return Anthropic overflow text exactly as:

prompt is too long: N tokens > M maximum

OpenAI-compatible endpoints must remain byte-identical.

Safety constraints

  • Do not re-render, re-tokenise, re-parse, or cold-prefill the prompt.
  • Use the decoder's real position, not rendered prompt length.
  • Append pre-resolved suffix ids through the existing batched decode path under the model lock; do not call the session evaluator directly.
  • Run only once and never after tool/recovery re-entry or after any stream output.
  • Re-derive thinking and stream state after the suffix.
  • If suffix evaluation fails partway, invalidate the session and clear live slot state before returning the existing error.
  • A syntax without a pure close suffix is a no-op.
  • Do not expose hidden reasoning as visible text.

Acceptance

  • Unit tests cover threshold values 180, 1024, 1025, disabled 0, reasoning off, recovery paths, already-streamed output, partial suffix failure, and unchanged OpenAI output.
  • Qwen's derived suffix detokenises to exactly \n</think>\n\n.
  • A near-limit Anthropic request with max_tokens=180 returns a non-empty text block and no thinking block.
  • Repeating a long prompt preserves KV reuse; continuation after a tail-closed answer does not cold-prefill.
  • A normal high-budget reasoning request still emits thinking normally.
  • ./ds4_test --server passes.

This change is intentionally limited to the server.

Lingua principale
C
Stelle
24.4k
Fork
2.4k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di antirez/ds4

Tutte le issue di antirez/ds4

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.