How to Configure and Switch Between Different LLM Providers (Google, DeepSeek, DashScope, OpenAI) in TradingAgents-CN

TradingAgents-CN supports a plug-and-play architecture for large-language-model (LLM) providers, allowing you to configure Google, DeepSeek, DashScope, or OpenAI once and use them seamlessly across the web UI, CLI, and Python API.

The open-source TradingAgents-CN repository implements a unified adapter pattern that abstracts provider-specific implementations. This guide explains how to configure and switch between supported LLM providers using the actual source code paths and configuration mechanisms found in the project.

Understanding the LLM Provider Architecture

TradingAgents-CN decouples the analysis engine from specific LLM implementations through adapter classes located in tradingagents/llm_adapters/. Each provider extends a base OpenAI-compatible interface, allowing the system to route requests to Google Gemini, DeepSeek, DashScope (Aliyun), or standard OpenAI endpoints using identical method signatures.

The chosen provider is stored in the browser's localStorage for web sessions and synchronized to Streamlit's session state. This persistence logic lives in web/components/sidebar.py【/web/components/sidebar.py#L17-L22】. For CLI usage, the provider is captured by the interactive helper select_llm_provider() defined in cli/utils.py【/cli/utils.py#L36-L44】.

Configuring Providers via the Web UI

Where Provider Settings Are Stored

When you select a provider in the sidebar, the application updates st.session_state.llm_provider and writes the choice back to localStorage via the "保存配置" utility. On the next page load, the stored value is restored, ensuring the provider stays consistent across browser sessions.

Step-by-Step Web Configuration

  1. Launch the web interface by running python run_web.py.
  2. Locate the LLM提供商 selector in the left-hand sidebar (implemented in sidebar.py【/web/components/sidebar.py#L17-L22】).
  3. Choose from the supported options:
Display name Internal value Typical models
🇨🇳 阿里百炼 dashscope qwen-turbo, qwen-plus-latest, qwen-max
🚀 DeepSeek V3 deepseek deepseek-chat
🌟 Google AI google Google Gemini models
🤖 OpenAI openai gpt-3.5-turbo, gpt-4
🔧 自定义OpenAI端点 custom_openai User-defined endpoints
  1. Click "保存配置" to persist your selection. The page will reload with the new provider active for all subsequent analyses.

Configuring Providers via the CLI

Interactive Provider Selection

Start the interactive setup wizard:

python -m cli.main config

During step 6, the function select_llm_provider() is invoked (see cli/utils.py【/cli/utils.py#L36-L44】). It presents the same list of providers as the web UI and returns a tuple (display_name, base_url).

If you select Custom OpenAI endpoint, an additional prompt requests the full URL and stores it in the environment variable CUSTOM_OPENAI_BASE_URL.

Runtime Configuration Storage

The resulting provider-value pair is written into the global DEFAULT_CONFIG object and persisted to ~/.tradingagents/config.json. All subsequent CLI commands (cli.mainrun_stock_analysis) read this configuration to instantiate the correct LLM adapter.

Programmatic Configuration (Python API)

When invoking the analysis engine directly, pass the provider and model name to run_stock_analysis in web/utils/analysis_runner.py【/web/utils/analysis_runner.py#L100-L108】:

from web.utils.analysis_runner import run_stock_analysis

result = run_stock_analysis(
    stock_symbol="TSLA",
    analysis_date="2024-09-30",
    analysts=["quick_think", "deep_think"],
    research_depth=3,
    llm_provider="deepseek",          # ← choose provider here

    llm_model="deepseek-chat",        # model name supported by the provider

    market_type="美股",
)

print(analysis["summary"])

All downstream LLM adapters read the provider name and instantiate the appropriate client (ChatDeepSeek, ChatDashScopeOpenAI, ChatGoogleOpenAI, etc.).

Supported LLM Providers and API Keys

Each provider requires a dedicated API key environment variable. The validation logic resides in web/utils/api_checker.py【/web/utils/api_checker.py#L11-L15】 and the respective adapter files.

Provider Environment Variable Validation Location
DashScope (Aliyun) DASHSCOPE_API_KEY tradingagents/llm_adapters/dashscope_openai_adapter.py【/tradingagents/llm_adapters/dashscope_openai_adapter.py#L55-L66】
DeepSeek DEEPSEEK_API_KEY tradingagents/llm_adapters/deepseek_adapter.py
Google GOOGLE_API_KEY tradingagents/llm_adapters/google_openai_adapter.py
OpenAI OPENAI_API_KEY Standard OpenAI client validation

If an API key is missing or invalid, the UI displays a warning via api_checker.py, and the adapter raises a clear exception during initialization.

Switching Providers at Runtime

  • Web Interface – Change the selector in the sidebar; the page reload automatically applies the new provider to subsequent analyses.
  • CLI – Restart the interactive wizard or manually edit ~/.tradingagents/config.json to set "llm_provider": "google" (or any other value).
  • Python API – Supply a different llm_provider argument to run_stock_analysis or any higher-level helper.

The adapters are stateless; they fetch the API key from the environment each time they are instantiated, allowing you to toggle providers freely without restarting the entire application (provided the correct key is present).

Extending to New Providers

To add a new LLM vendor, implement these three steps:

  1. Create an OpenAI-compatible adapter by subclassing ChatOpenAI or the generic OpenAICompatibleBase in tradingagents/llm_adapters/.
  2. Register the provider by adding the display name and internal value to the provider lists in both web/components/sidebar.py and cli/utils.py.
  3. Define model mappings by registering the available models in the new adapter (mirroring the pattern in dashscope_openai_adapter.pyDASHSCOPE_OPENAI_MODELS).

The rest of the pipeline—including progress tracking, token usage monitoring, and configuration persistence—automatically picks up the new provider without additional changes.

Summary

  • TradingAgents-CN uses a unified adapter pattern to support multiple LLM providers through a single interface.
  • Configure providers via the web sidebar (web/components/sidebar.py), CLI wizard (cli/utils.py), or Python API (run_stock_analysis).
  • Supported providers include DashScope, DeepSeek, Google, and OpenAI, each requiring a specific environment variable (DASHSCOPE_API_KEY, DEEPSEEK_API_KEY, GOOGLE_API_KEY, OPENAI_API_KEY).
  • Switch providers at runtime by changing the sidebar selector, editing the CLI config file, or passing a new llm_provider argument to the analysis runner.
  • Extend the system by implementing new adapters in tradingagents/llm_adapters/ and registering them in the UI and CLI configuration files.

Frequently Asked Questions

How do I add a custom OpenAI-compatible endpoint in TradingAgents-CN?

Select "自定义OpenAI端点" (Custom OpenAI endpoint) from the provider dropdown in the web sidebar or CLI wizard. You will be prompted to enter the full base URL (e.g., https://api.mycustomllm.com/v1). The system stores this in the CUSTOM_OPENAI_BASE_URL environment variable and uses it to initialize the OpenAI client with your custom endpoint.

Can I use different LLM providers for different analysis tasks in the same session?

No. TradingAgents-CN maintains a single active provider per session stored in st.session_state.llm_provider (web) or the global DEFAULT_CONFIG (CLI). While you can switch providers between runs by updating the configuration, you cannot assign different providers to individual analysts within a single run_stock_analysis call. All downstream components use the same adapter instance for consistency.

What happens if my API key is invalid or missing?

The system validates API keys before initializing the LLM client. In the web interface, web/utils/api_checker.py displays a warning banner if a required key is missing or contains placeholder text. In the Python API, the adapter (e.g., dashscope_openai_adapter.py) raises a clear ValueError during initialization if the corresponding environment variable (DASHSCOPE_API_KEY, DEEPSEEK_API_KEY, etc.) is empty or invalid, preventing cryptic downstream errors.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →