- You’ll integrate OpenRouter’s Kimi K2 model with your trading agent for enhanced agentic intelligence in market analysis and decision-making.
- You’ll run safe paper trading using Foundry Anvil fork to test strategies before deploying to live markets.
- You’ll configure environment variables and API keys for OpenRouter access while maintaining all existing agent features.
- By the end, you’ll have a trading agent powered by Kimi K2’s state-of-the-art mixture-of-experts capabilities.
Previous section: AI trading agent: Stateful agent
Project repository: Web3 AI trading agent
Remember that this is a NOT FOR PRODUCTION tutorial. In a production deployment, don’t store your private key in a config.py file.
About Kimi K2
With the company name Moonshot AI—the company behind Kimi-K2—you know you just have to try their flagship model as the trading agent LLM. Kimi K2 at 1T parameters and an MoE architecture is theoretically decent fit for the trading agent, and of course is very fun to experiment with. Check out Kimi K2 at Hugging Face.Quick highlights
- 1 trillion total parameters with 32 billion activated parameters
- Mixture-of-experts (MoE) design with 384 experts, selecting 8 per token
- 128K context length for comprehensive market analysis (although note that our OpenRouter implementation has 65k context size)
- Trained with the innovative Muon optimizer for exceptional stability
- Superior performance in mathematical reasoning
- Natural language understanding for market sentiment analysis
Prerequisites
Before starting, ensure you have:- Python 3.8+ installed
- All dependencies from
requirements.txt
installed - Foundry installed (
curl -L https://foundry.paradigm.xyz | bash && foundryup
) - OpenRouter account with API key
- Chainstack BASE RPC endpoint
OpenRouter setup
Create OpenRouter account
- Visit https://openrouter.ai/.
- Sign up for an account.
- Add credits to your account. Each trading decision will cost approximately $0.02-0.05 per decision.
- Navigate to https://openrouter.ai/keys.
- Create a new API key and copy it.
Configure model and API key
Editconfig.py
and make these changes:
The Kimi K2 model is already pre-configured in your
config.py
file with the correct OpenRouter endpoint and context capacity settings.Understanding trading environments
The agent can run in two environments: Foundry fork mode (USE_FORK = True
)
- Safe for testing and experimentation
- Uses paper money (no real funds at risk)
- Real market data from BASE mainnet
- Real smart contract interactions
- Connects to:
http://localhost:8545
(Anvil fork)
USE_FORK = False
)
- Uses real money and real transactions
- All trades are permanent and irreversible
- Gas fees apply to every transaction
- Connects to: Your Chainstack BASE RPC endpoint
Always start with fork mode to test your strategies before using real funds.
Set up RPC endpoints for mainnet mode
If you plan to use mainnet mode (USE_FORK = False
), configure your BASE RPC endpoints in config.py
:
For fork mode (
USE_FORK = True
), the agent automatically uses http://localhost:8545
and these endpoints are not needed.Licensing considerations
Important Licensing Information: Kimi K2 is released under a Modified MIT License with specific commercial usage requirements.
Modified MIT License Terms
The Kimi K2 model uses a Modified MIT License with the following key requirement: Commercial usage clause: If your trading application (or any derivative works) is used for commercial products or services that have:- More than 100 million monthly active users, OR
- More than 20 million US dollars (or equivalent) in monthly revenue
Configure trading parameters
Set your trading wallet private key inconfig.py
:
Use a test wallet with minimal funds. This is for educational purposes only.
Start Foundry Anvil fork
Launch Anvil fork
Open a new terminal and start the Anvil fork of BASE mainnet:Fund your trading account
If your trading account needs more ETH, use Anvil’s built-in accounts:Verify configuration
Test OpenRouter connection
Create a test script to verify your OpenRouter setup:Test Anvil connection
Verify the local fork is working:Check trading account balance
Verify your trading account has sufficient funds:Run the trading agent
Basic trading mode
Start the agent in normal trading mode:Observation mode
Start with observation mode to see how Kimi K2 analyzes the market without executing trades:- Collect market data for 10 cycles
- Have Kimi K2 analyze each market state
- Generate an initial trading strategy
- Switch to active trading
Test mode with reduced context
Test the context management system:Custom trading parameters
Modify trading behavior with command-line arguments:Configuration optimization
Inconfig.py
, you can adjust Kimi K2-specific settings:
Kimi K2’s agentic intelligence makes it particularly well-suited for autonomous trading scenarios, but always start with careful testing and small amounts.
This is for educational and testing purposes only. Use test wallets with minimal funds. Never use production private keys. Monitor OpenRouter costs regularly. Be aware of licensing requirements for commercial usage. The fork environment uses test funds, but configuration errors could affect real accounts.