Skip to content

fix(llm): cap max_tokens and enable streaming to prevent hangs with post-7/31 deepseek-v4-flash (#1204) - #1205

Open
ariesy wants to merge 2 commits into
TauricResearch:mainfrom
ariesy:fix/deepseek-v4-flash-max-tokens
Open

fix(llm): cap max_tokens and enable streaming to prevent hangs with post-7/31 deepseek-v4-flash (#1204)#1205
ariesy wants to merge 2 commits into
TauricResearch:mainfrom
ariesy:fix/deepseek-v4-flash-max-tokens

Conversation

@ariesy

@ariesy ariesy commented Aug 6, 2026

Copy link
Copy Markdown

Summary

Fixes #1204. TradingAgents hangs indefinitely with the post-7/31 stable deepseek-v4-flash (long reasoning chains). Root cause is two-fold:

  1. No max_tokens sent. _PASSTHROUGH_KWARGS in llm_clients/openai_client.py does not forward max_tokens. With the new long-reasoning v4-flash, an unbounded generation exceeds the gateway's ~3 min idle timeout and the connection is dropped — the process sits in wait_woken forever. When a bounded default does apply, the reasoning chain can consume the entire budget and content comes back empty.
  2. No streaming for OpenAI-compatible backends. The OpenCode Go gateway (and similar proxies) closes connections on long non-streamed responses; streaming keeps the socket active.

Verified against opencode.ai/zen/go/v1 with deepseek-v4-flash:

Prompt max_tokens Result
trivial 100 OK (reasoning 26 tokens)
complex 500 empty content (reasoning ate all 500)
complex 2048 OK (reasoning 529 + content 55)
complex unset no response in 90s (HTTP 000 timeout)

Changes

  • tradingagents/default_config.py: add TRADINGAGENTS_MAX_TOKENS env override + max_tokens: 8192 default.
  • tradingagents/graph/trading_graph.py: forward max_tokens from config into LLM client kwargs.
  • tradingagents/llm_clients/openai_client.py:
    • add max_tokens to _PASSTHROUGH_KWARGS;
    • enable streaming when base_url points at opencode.ai (keeps the socket alive; langchain aggregates chunks back to a single AIMessage);
    • use DeepSeekChatOpenAI for opencode.ai + deepseek models so reasoning_content is captured on the streaming path via _convert_chunk_to_generation_chunk and round-tripped on the next turn.

Verification

  • Unit tests: 94 passed (incl. test_deepseek_reasoning, test_capabilities, test_env_overrides, test_openai_compatible_provider, test_llm_max_retries, test_temperature_config).
  • End-to-end: full 159566.SZ run (all 4 analysts + research + trader + risk debate) completes; previously hung 3/3 times at the analyst phase.
  • max_tokens default (8192) and env override (TRADINGAGENTS_MAX_TOKENS=16384) both verified.

Related

ariesy added 2 commits August 5, 2026 17:04
…long reasoning chains (TauricResearch#1204)

DeepSeek V4 Flash (7/31 stable) emits much longer reasoning chains
(500+ tokens for complex tasks). TradingAgents sent no max_tokens, so:
- with a bounded default, the reasoning chain consumed the whole budget
  and content came back empty (agents got nothing to parse);
- with no bound, a single generation could exceed the gateway's ~3 min
  idle timeout and drop the connection, hanging the run.

Add TRADINGAGENTS_MAX_TOKENS (default 8192) and forward it through
_get_provider_kwargs -> _PASSTHROUGH_KWARGS into every chat client.
…o backend (TauricResearch#1204)

max_tokens alone does not fix hangs with the post-7/31 deepseek-v4-flash:
long reasoning chains make single non-streamed generations exceed the
OpenCode Go gateway's ~3 min idle timeout, which drops the connection
and leaves the process in wait_woken.

- Enable streaming when base_url points at opencode.ai (keeps the socket
  active; langchain aggregates chunks back to a single AIMessage)
- Use DeepSeekChatOpenAI for opencode.ai + deepseek models so
  reasoning_content is captured on the streaming path via
  _convert_chunk_to_generation_chunk and round-tripped on the next turn
rajatvarna added a commit to rajatvarna/TradingAgents that referenced this pull request Aug 10, 2026
feat: DeepSeek max_tokens and streaming reasoning round-trip (TauricResearch#1205)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: TradingAgents hangs with post-7/31 deepseek-v4-flash — long reasoning chains exhaust token budget / trip gateway idle timeout

1 participant