feat(llm): let each model tier run on its own provider (#1440)

- quick_think_provider / deep_think_provider and their backend URLs (with TRADINGAGENTS_* variables) choose a tier's provider; unset, a tier uses llm_provider and backend_url
- a tier on another provider gets that provider's own settings and endpoint, and the run settings and report header name each tier's provider
- the CLI checks each tier provider's model variable and key before the run
This commit is contained in:
Yijia-Xiao
2026-10-03 21:27:43 +00:00
parent f65fcabb60
commit cc39a35c42
11 changed files with 225 additions and 32 deletions
+46
View File
@@ -185,3 +185,49 @@ def test_an_unknown_analyst_error_names_the_analysts_as_users_know_them():
with pytest.raises(ValueError) as caught:
prompts.parse_analysts("macro", AssetType.STOCK)
assert "sentiment" in str(caught.value) and "social" not in str(caught.value)
def _selections_with_env(monkeypatch, keys=None):
for name, value in UNATTENDED_ENV.items():
monkeypatch.setenv(name, value)
monkeypatch.setattr(selections, "fetch_announcements", lambda: None)
monkeypatch.setattr(selections, "display_announcements", lambda *a: None)
checked = keys if keys is not None else []
monkeypatch.setattr(selections, "ensure_api_key", lambda provider: checked.append(provider))
return checked
@pytest.mark.unit
def test_a_tier_on_another_provider_needs_its_own_model(monkeypatch, capsys):
"""Otherwise the CLI would take the main provider's model for it (#1440)."""
_selections_with_env(monkeypatch)
monkeypatch.setitem(selections.DEFAULT_CONFIG, "deep_think_provider", "anthropic")
monkeypatch.delenv("TRADINGAGENTS_DEEP_THINK_LLM", raising=False)
with pytest.raises(typer.Exit):
selections._prompt_selections({}, FLAGS)
assert "TRADINGAGENTS_DEEP_THINK_LLM" in capsys.readouterr().out
@pytest.mark.unit
def test_each_tier_provider_s_key_is_checked_before_the_run(monkeypatch):
checked = _selections_with_env(monkeypatch)
monkeypatch.setitem(selections.DEFAULT_CONFIG, "deep_think_provider", "anthropic")
monkeypatch.setenv("TRADINGAGENTS_DEEP_THINK_LLM", "claude-opus-5-5")
selections._prompt_selections({}, FLAGS)
assert checked == ["openai", "anthropic"]
@pytest.mark.unit
def test_only_the_providers_the_tiers_use_need_a_key(monkeypatch):
"""Both tiers on a local Ollama: the main provider's key is not needed."""
checked = _selections_with_env(monkeypatch)
for tier in ("quick", "deep"):
monkeypatch.setitem(selections.DEFAULT_CONFIG, f"{tier}_think_provider", "ollama")
monkeypatch.setenv(f"TRADINGAGENTS_{tier.upper()}_THINK_LLM", "llama3.1")
selections._prompt_selections({}, FLAGS)
assert checked == ["ollama"]
+1 -1
View File
@@ -124,7 +124,7 @@ def _graph(tmp_path, monkeypatch, model, debug=False, **config):
cfg = copy.deepcopy(DEFAULT_CONFIG)
cfg.update(results_dir=str(tmp_path / "results"), data_cache_dir=str(tmp_path / "cache"),
memory_log_path=str(tmp_path / "log.md"), **config)
monkeypatch.setattr(trading_graph, "create_llm_client", lambda **k: _Client(model))
monkeypatch.setattr(trading_graph, "create_tier_client", lambda config, tier, **k: _Client(model))
return trading_graph.TradingAgentsGraph(config=cfg, debug=debug)
+90
View File
@@ -0,0 +1,90 @@
"""Each model tier can name its own provider (#1440).
The quick tier (analysts, researchers, debaters, trader) and the deep tier
(research and portfolio managers) default to ``llm_provider``. A tier that names
another provider gets that provider's own settings and endpoint, never the main
provider's URL, and the run's settings and report say which provider each used.
"""
import pytest
from tradingagents import default_config
from tradingagents.llm_clients import factory
def _config(**overrides):
config = {**default_config.DEFAULT_CONFIG, "llm_provider": "openai", "backend_url": "https://api.openai.com/v1",
"quick_think_llm": "gpt-6-luna", "deep_think_llm": "gpt-6-sol",
"openai_reasoning_effort": "low", "google_thinking_level": "high"}
config.update(overrides)
return config
def _captured(monkeypatch):
made = []
monkeypatch.setattr(factory, "create_llm_client",
lambda provider, model, base_url=None, **kw: made.append((provider, model, base_url, kw)))
return made
@pytest.mark.unit
def test_a_tier_without_its_own_provider_uses_the_run_s(monkeypatch):
made = _captured(monkeypatch)
factory.create_tier_client(_config(), "deep")
provider, model, base_url, kw = made[0]
assert (provider, model, base_url) == ("openai", "gpt-6-sol", "https://api.openai.com/v1")
assert kw["reasoning_effort"] == "low"
@pytest.mark.unit
def test_a_tier_on_another_provider_gets_that_provider_s_settings_and_not_the_main_url(monkeypatch):
made = _captured(monkeypatch)
factory.create_tier_client(_config(deep_think_provider="google", deep_think_llm="gemini-3.8-flash"), "deep")
provider, model, base_url, kw = made[0]
assert (provider, model) == ("google", "gemini-3.8-flash")
assert base_url is None # the OpenAI URL must not follow it
assert kw["thinking_level"] == "high" and "reasoning_effort" not in kw
@pytest.mark.unit
def test_a_tier_url_is_used_for_that_tier_only(monkeypatch):
made = _captured(monkeypatch)
config = _config(quick_think_provider="openai_compatible", quick_think_backend_url="http://localhost:8000/v1",
quick_think_llm="local-model")
factory.create_tier_client(config, "quick")
factory.create_tier_client(config, "deep")
assert made[0][:3] == ("openai_compatible", "local-model", "http://localhost:8000/v1")
assert made[1][:3] == ("openai", "gpt-6-sol", "https://api.openai.com/v1")
@pytest.mark.unit
def test_the_run_settings_and_report_name_each_tier_s_provider():
from tradingagents.graph.trading_graph import TradingAgentsGraph
from tradingagents.reporting import _header
graph = object.__new__(TradingAgentsGraph)
graph.selected_analysts = ("market",)
graph.config = _config(deep_think_provider="anthropic", deep_think_llm="claude-opus-5-5")
settings = graph.run_settings()
assert settings["quick_think_provider"] == "openai" and settings["deep_think_provider"] == "anthropic"
header = _header("NVDA", {}, settings)
assert "deep anthropic claude-opus-5-5" in header and "quick openai gpt-6-luna" in header
@pytest.mark.unit
def test_the_tier_settings_are_read_from_the_environment(monkeypatch):
monkeypatch.setenv("TRADINGAGENTS_DEEP_THINK_PROVIDER", "anthropic")
monkeypatch.setenv("TRADINGAGENTS_QUICK_THINK_BACKEND_URL", "http://localhost:8000/v1")
config = default_config.build_default_config()
assert config["deep_think_provider"] == "anthropic"
assert config["quick_think_backend_url"] == "http://localhost:8000/v1"
assert config["quick_think_provider"] is None