DEV Community

Arpan Dhara
Arpan Dhara

Posted on

Integrating TokenRouter with Tauric Research TradingAgents

If you're using Tauric Research's TradingAgents framework and want to integrate TokenRouter as an LLM provider, this guide walks through the complete process step by step.

The integration adds TokenRouter support to the TradingAgents interactive CLI and provides a predefined list of models that you can select directly from the interface.

TradingAgents Repository:
[https://github.com/TauricResearch/TradingAgents.git]


What We'll Do

In this tutorial, we'll:

  1. Define TokenRouter models in the model catalog
  2. Register the TokenRouter API endpoint
  3. Configure the TokenRouter API key
  4. Bypass strict model-name validation
  5. Add TokenRouter to the interactive CLI
  6. Configure the .env file
  7. Reinstall and restart TradingAgents

Let's get started.


Step 1 — Define TokenRouter Models in the Model Catalog

First, open:

tradingagents/llm_clients/model_catalog.py
Enter fullscreen mode Exit fullscreen mode

Near the top of the file, around the model definitions section, add a TokenRouter model dictionary.

_TOKENROUTER_MODELS: dict[str, list[ModelOption]] = {
    "quick": [
        ("GPT-4o Mini", "openai/gpt-4o-mini"),
        ("Claude 3.5 Haiku", "anthropic/claude-3-haiku"),
        ("DeepSeek V3", "deepseek/deepseek-chat"),
        ("Custom model ID", "custom"),
    ],
    "deep": [
        ("GPT-4o", "openai/gpt-4o"),
        ("Claude 3.5 Sonnet", "anthropic/claude-3-5-sonnet"),
        ("DeepSeek R1", "deepseek/deepseek-reasoner"),
        ("Custom model ID", "custom"),
    ],
}
Enter fullscreen mode Exit fullscreen mode

The quick and deep categories provide predefined model choices.

You can also use the custom option when you want to specify your own model ID.

Next, locate MODEL_OPTIONS, around the section where providers are mapped.

Add tokenrouter after bedrock:

"bedrock": _CUSTOM_ONLY,
"tokenrouter": _TOKENROUTER_MODELS,
Enter fullscreen mode Exit fullscreen mode

After this change, TradingAgents will know which models should be presented when TokenRouter is selected.


Step 2 — Register the TokenRouter API Endpoint

Now we need to tell TradingAgents where the TokenRouter API is located.

Open:

tradingagents/llm_clients/openai_client.py
Enter fullscreen mode Exit fullscreen mode

Find:

OPENAI_COMPATIBLE_PROVIDERS
Enter fullscreen mode Exit fullscreen mode

Add TokenRouter to the provider definitions:

"openrouter": ProviderSpec(
    base_url="https://openrouter.ai/api/v1"
),
"tokenrouter": ProviderSpec(
    base_url="https://api.tokenrouter.com/v1"
),
Enter fullscreen mode Exit fullscreen mode

TokenRouter is registered here as an OpenAI-compatible provider.


Step 3 — Configure the TokenRouter API Key Environment Variable

Next, open:

tradingagents/llm_clients/api_key_env.py
Enter fullscreen mode Exit fullscreen mode

Find:

PROVIDER_API_KEY_ENV
Enter fullscreen mode Exit fullscreen mode

Add the TokenRouter environment variable:

"openrouter": "OPENROUTER_API_KEY",
"tokenrouter": "TOKENROUTER_API_KEY",
Enter fullscreen mode Exit fullscreen mode

This tells TradingAgents which environment variable contains the TokenRouter API key.


Step 4 — Bypass Strict Model Name Validation

TradingAgents may normally validate model names against its predefined rules.

Because TokenRouter can provide different models through its API, we need to add it to the providers that allow arbitrary model names.

Open:

tradingagents/llm_clients/validators.py
Enter fullscreen mode Exit fullscreen mode

Locate:

_ANY_MODEL_PROVIDERS
Enter fullscreen mode Exit fullscreen mode

Then add tokenrouter:

_ANY_MODEL_PROVIDERS = (
    "ollama",
    "openrouter",
    "openai_compatible",
    "mistral",
    "kimi",
    "groq",
    "nvidia",
    "bedrock",
    "tokenrouter",
)
Enter fullscreen mode Exit fullscreen mode

This allows TokenRouter model IDs to pass through the model validation layer.


Step 5 — Add TokenRouter to the Interactive CLI

Now we'll make TokenRouter visible in the TradingAgents interactive CLI.

Open:

cli/utils.py
Enter fullscreen mode Exit fullscreen mode

Locate:

_llm_provider_table()
Enter fullscreen mode Exit fullscreen mode

Find the OpenRouter entry and add TokenRouter immediately after it:

("OpenRouter", "openrouter", "https://openrouter.ai/api/v1"),
("TokenRouter", "tokenrouter", "https://api.tokenrouter.com/v1"),
Enter fullscreen mode Exit fullscreen mode

After this change, TokenRouter should appear as a selectable provider when you launch the TradingAgents CLI.


Step 6 — Add Your TokenRouter API Key

Now we need to provide your TokenRouter API key.

Open the .env file in the root directory of the TradingAgents project.

Add:

TOKENROUTER_API_KEY=your_actual_tokenrouter_key_here
Enter fullscreen mode Exit fullscreen mode

Replace:

your_actual_tokenrouter_key_here
Enter fullscreen mode Exit fullscreen mode

with your actual TokenRouter API key.

Important: Never commit your .env file or API key to GitHub.

Make sure .env is included in your .gitignore.


Step 7 — Reinstall and Restart TradingAgents

After making all of the changes, save your files.

Open the VS Code terminal and reinstall the project in editable mode:

pip install -e .
Enter fullscreen mode Exit fullscreen mode

Then launch TradingAgents:

TradingAgents
Enter fullscreen mode Exit fullscreen mode

Restarting the application is important because Python needs to load the modified definitions from disk.


Final Project Changes

After completing the integration, you should have modified the following files:

tradingagents/
├── llm_clients/
│   ├── model_catalog.py
│   ├── openai_client.py
│   ├── api_key_env.py
│   └── validators.py
│
└── cli/
    └── utils.py

.env
Enter fullscreen mode Exit fullscreen mode

The main additions are:

TokenRouter model definitions
        ↓
TokenRouter API endpoint
        ↓
TOKENROUTER_API_KEY environment variable
        ↓
Model validation bypass
        ↓
TokenRouter CLI provider
        ↓
TradingAgents
Enter fullscreen mode Exit fullscreen mode

That's It!

TokenRouter is now integrated into the TradingAgents framework as an LLM provider.

You should now be able to select TokenRouter from the interactive CLI and use the predefined models or provide a custom model ID.

If you're working with the Tauric Research TradingAgents project, you can find the repository here:

Repository:
[YOUR_TRADINGAGENTS_REPOSITORY_LINK]


Quick Checklist

Before running TradingAgents, make sure you've completed everything:

  • [ ] Added _TOKENROUTER_MODELS
  • [ ] Added "tokenrouter" to MODEL_OPTIONS
  • [ ] Added the TokenRouter API endpoint
  • [ ] Added TOKENROUTER_API_KEY
  • [ ] Added TokenRouter to _ANY_MODEL_PROVIDERS
  • [ ] Added TokenRouter to the CLI provider table
  • [ ] Added your API key to .env
  • [ ] Ran pip install -e .
  • [ ] Restarted TradingAgents

If all of the above are complete, you should be ready to use TokenRouter with TradingAgents.


This integration guide is based on the TokenRouter integration procedure for the TradingAgents framework.

Top comments (0)