How to Configure Multiple LLM Providers in TradingAgents
TradingAgents uses a factory pattern in tradingagents/llm_clients/factory.py to dynamically instantiate provider-specific clients based on a configuration dictionary, enabling seamless switching between OpenAI, Google, Anthropic, and custom providers.
The TradingAgents framework from TauricResearch abstracts Large Language Model (LLM) interactions behind a unified client interface. By configuring multiple LLM providers, you can optimize cost and performance by routing deep reasoning tasks to powerful models while delegating quick summarizations to lighter alternatives.
Architecture Overview
The system isolates provider selection through three coordinated layers:
Configuration Layer
A centralized DEFAULT_CONFIG dictionary in tradingagents/default_config.py defines the active provider and model assignments. Key fields include llm_provider (the provider name), deep_think_llm (the model for complex reasoning), quick_think_llm (the model for fast responses), and provider-specific flags like google_thinking_level or openai_reasoning_effort.
Factory Layer
The create_llm_client() function in tradingagents/llm_clients/factory.py receives the provider name, model identifier, optional base_url, and additional keyword arguments. It returns an instance of the appropriate subclass of BaseLLMClient. If the requested provider is not supported, the factory raises a ValueError.
Graph Initialization Layer
The TradingGraph class in tradingagents/graph/trading_graph.py orchestrates the instantiation of both deep-thinking and quick-thinking LLMs. It extracts provider-specific kwargs via _get_provider_kwargs() and passes them to the factory, ensuring that each provider receives its required parameters (such as thinking_level for Gemini or reasoning_effort for OpenAI).
Supported LLM Providers
TradingAgents includes specialized client implementations for major providers, all inheriting from BaseLLMClient in tradingagents/llm_clients/base_client.py.
OpenAI and Compatible Services
The OpenAIClient in tradingagents/llm_clients/openai_client.py handles native OpenAI models as well as compatible services like Ollama, OpenRouter, and xAI. It can force the Responses API for native OpenAI models when use_responses_api=True. Provider-specific base URLs and API key environment variables are defined in the _PROVIDER_CONFIG mapping within the same file.
Google Gemini
The GoogleClient in tradingagents/llm_clients/google_client.py creates a ChatGoogleGenerativeAI instance. It maps the generic thinking_level configuration to the correct Gemini API parameter: thinking_level for Gemini 3 and thinking_budget for Gemini 2.5.
Anthropic Claude
The AnthropicClient in tradingagents/llm_clients/anthropic_client.py builds a ChatAnthropic instance and forwards provider-specific kwargs such as effort to control reasoning intensity.
Configuration Methods
You can configure multiple LLM providers either programmatically or through the interactive CLI.
Custom Configuration Dictionary
Pass a dictionary to the TradingGraph constructor to switch providers dynamically:
custom_config = {
"llm_provider": "google", # could be "openai", "anthropic", etc.
"deep_think_llm": "gemini-1.5-pro",
"quick_think_llm": "gemini-1.5-flash",
"backend_url": None, # use default for the provider
"google_thinking_level": "high", # provider‑specific flag
"openai_reasoning_effort": None,
"anthropic_effort": None,
}
When TradingGraph processes this config, _get_provider_kwargs injects thinking_level="high" and the factory builds a GoogleClient. The deep-thinking LLM becomes a NormalizedChatGoogleGenerativeAI ready to generate Gemini responses.
Interactive CLI Selection
When launching the CLI from cli/main.py, the select_llm_provider() function in cli/utils.py prompts you to choose from OpenAI, Google, Anthropic, xAI, OpenRouter, or Ollama. The selected provider name and backend URL are stored in the runtime configuration and passed to the graph construction.
Implementation Examples
Running Two Providers Simultaneously
You can instantiate different providers for different tasks within the same session:
from tradingagents.llm_clients import create_llm_client
# Deep reasoning – OpenAI GPT‑5.2
deep_client = create_llm_client(
provider="openai",
model="gpt-5.2",
base_url="https://api.openai.com/v1",
reasoning_effort="high",
)
deep_llm = deep_client.get_llm()
# Quick summarisation – Anthropic Claude
quick_client = create_llm_client(
provider="anthropic",
model="claude-3-5-sonnet-20240620",
effort="medium",
)
quick_llm = quick_client.get_llm()
# Use them independently
deep_response = deep_llm.invoke("Analyse the latest macro data.")
quick_response = quick_llm.invoke("Summarise the above analysis in three bullet points.")
print(deep_response.content)
print(quick_response.content)
Both deep_llm and quick_llm expose the same .invoke() interface, and each automatically normalizes response content to a plain string via normalize_content().
Adding a New Provider (Mistral)
To extend the system for a new provider like Mistral:
# 1️⃣ tradingagents/llm_clients/mistral_client.py
from langchain_mistral import ChatMistral
from .base_client import BaseLLMClient, normalize_content
from .validators import validate_model
class NormalizedChatMistral(ChatMistral):
def invoke(self, input, config=None, **kwargs):
return normalize_content(super().invoke(input, config, **kwargs))
class MistralClient(BaseLLMClient):
def get_llm(self):
llm_kwargs = {"model": self.model}
# Forward any user‑provided kwargs (e.g., timeout)
llm_kwargs.update(self.kwargs)
return NormalizedChatMistral(**llm_kwargs)
def validate_model(self):
return validate_model("mistral", self.model)
# 2️⃣ Update the factory (tradingagents/llm_clients/factory.py)
if provider_lower == "mistral":
return MistralClient(model, base_url, **kwargs)
# 3️⃣ (Optional) expose a default URL via environment variable or _PROVIDER_CONFIG
After these additions, setting "llm_provider": "mistral" in your configuration dictionary will instantiate the new client automatically.
Summary
- TradingAgents isolates LLM selection behind a factory pattern in
tradingagents/llm_clients/factory.py. - Configuration is centralized in
tradingagents/default_config.pyusing thellm_providerkey and provider-specific flags likegoogle_thinking_leveloropenai_reasoning_effort. - The
TradingGraphclass intradingagents/graph/trading_graph.pyuses_get_provider_kwargs()to inject the correct parameters for each provider when building deep-thinking and quick-thinking LLM instances. - Supported providers include OpenAI, Google Gemini, Anthropic Claude, and compatible services (Ollama, OpenRouter, xAI) via the
OpenAIClient. - Adding a new provider requires subclassing
BaseLLMClient, updating the factory, and optionally exposing configuration inTradingGraph.
Frequently Asked Questions
How do I switch between OpenAI and Google Gemini in TradingAgents?
Modify the llm_provider value in your configuration dictionary to "openai" or "google", and set the corresponding model identifiers in deep_think_llm and quick_think_llm. For Google, ensure you include google_thinking_level in your config; for OpenAI, use openai_reasoning_effort to control reasoning intensity.
Can I use multiple LLM providers simultaneously in the same trading session?
Yes. You can bypass the TradingGraph initialization and directly call create_llm_client() from tradingagents/llm_clients multiple times with different provider names. Each call returns an independent client instance with a normalized get_llm() interface, allowing you to route complex analysis to one provider and quick summaries to another.
Where are provider-specific parameters like thinking_level defined?
Provider-specific parameters are stored in the DEFAULT_CONFIG dictionary in tradingagents/default_config.py. The TradingGraph class extracts these via the _get_provider_kwargs() method and forwards them to the factory. For example, google_thinking_level becomes thinking_level when building a GoogleClient, while openai_reasoning_effort is passed directly to OpenAIClient.
How do I add support for a new LLM provider like Mistral?
Create a new client class in tradingagents/llm_clients/ that subclasses BaseLLMClient and implements get_llm() and validate_model(). Wrap the provider's LangChain chat model with normalize_content() to ensure consistent output. Then add a new branch in create_llm_client() in tradingagents/llm_clients/factory.py to instantiate your class when the provider name matches. Finally, update _get_provider_kwargs() in tradingagents/graph/trading_graph.py if your provider requires special configuration flags.
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:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →