Run the stateful trading agent with Grok 4’s advanced reasoning capabilities using OpenRouter API and Foundry Anvil fork for safe paper trading before moving to live markets.
TLDR:
Previous section: AI trading agent: Stateful agent
Project repository: Web3 AI trading agent
Remember that this a NOT FOR PRODUCTION tutorial. In a production deployment, don’t store your private key in a config.py file.
This section demonstrates how to integrate OpenRouter’s Grok 4 model with your trading agent. Grok 4 provides advanced reasoning capabilities with a 256K token context window, allowing for sophisticated market analysis and trading decisions. You’ll run the agent on a Foundry Anvil fork for safe paper trading before moving to live markets.
Before starting, ensure you have:
requirements.txt
installedcurl -L https://foundry.paradigm.xyz | bash && foundryup
)Edit config.py
and make these changes:
Always keep USE_FORK = True
for testing. Only set it to False
when you’re ready to trade with real funds on BASE mainnet.
The agent can run in two environments:
Foundry fork mode (USE_FORK = True
)
http://localhost:8545
(Anvil fork)BASE mainnet mode (USE_FORK = False
)
Always start with fork mode to test your strategies before using real funds.
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.
Set your trading wallet private key in config.py
:
Use a test wallet with minimal funds. This is for educational purposes only.
Open a new terminal and start the Anvil fork of BASE mainnet:
You should see output like:
If your trading account needs more ETH, use Anvil’s built-in accounts:
Create a test script to verify your OpenRouter setup:
Verify the local fork is working:
This should return the latest BASE block number.
Verify your trading account has sufficient funds:
Start the agent in normal trading mode:
Start with observation mode to see how Grok 4 analyzes the market without executing trades:
This will:
Test the context management system:
The agent displays different output depending on the model type:
For Grok 4 (OpenRouter) - Natural Language Responses:
The agent supports sophisticated natural language parsing for Grok 4 responses, including:
Sell patterns:
Buy patterns:
Hold patterns:
Monitor your usage at https://openrouter.ai/activity to track:
Modify trading behavior with command-line arguments:
In config.py
, you can adjust:
Reduce API costs:
TRADE_INTERVAL
to reduce decision frequencyImprove response times:
After testing thoroughly with the fork, if you want to trade with real funds:
config.py
:
--iterations
to limit trading sessionsThis is for educational and testing purposes only. Use test wallets with minimal funds. Never use production private keys. Monitor OpenRouter costs regularly. The fork environment uses test funds, but configuration errors could affect real accounts.
Next section: AI trading agent: Fine-tuning overview
Run the stateful trading agent with Grok 4’s advanced reasoning capabilities using OpenRouter API and Foundry Anvil fork for safe paper trading before moving to live markets.
TLDR:
Previous section: AI trading agent: Stateful agent
Project repository: Web3 AI trading agent
Remember that this a NOT FOR PRODUCTION tutorial. In a production deployment, don’t store your private key in a config.py file.
This section demonstrates how to integrate OpenRouter’s Grok 4 model with your trading agent. Grok 4 provides advanced reasoning capabilities with a 256K token context window, allowing for sophisticated market analysis and trading decisions. You’ll run the agent on a Foundry Anvil fork for safe paper trading before moving to live markets.
Before starting, ensure you have:
requirements.txt
installedcurl -L https://foundry.paradigm.xyz | bash && foundryup
)Edit config.py
and make these changes:
Always keep USE_FORK = True
for testing. Only set it to False
when you’re ready to trade with real funds on BASE mainnet.
The agent can run in two environments:
Foundry fork mode (USE_FORK = True
)
http://localhost:8545
(Anvil fork)BASE mainnet mode (USE_FORK = False
)
Always start with fork mode to test your strategies before using real funds.
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.
Set your trading wallet private key in config.py
:
Use a test wallet with minimal funds. This is for educational purposes only.
Open a new terminal and start the Anvil fork of BASE mainnet:
You should see output like:
If your trading account needs more ETH, use Anvil’s built-in accounts:
Create a test script to verify your OpenRouter setup:
Verify the local fork is working:
This should return the latest BASE block number.
Verify your trading account has sufficient funds:
Start the agent in normal trading mode:
Start with observation mode to see how Grok 4 analyzes the market without executing trades:
This will:
Test the context management system:
The agent displays different output depending on the model type:
For Grok 4 (OpenRouter) - Natural Language Responses:
The agent supports sophisticated natural language parsing for Grok 4 responses, including:
Sell patterns:
Buy patterns:
Hold patterns:
Monitor your usage at https://openrouter.ai/activity to track:
Modify trading behavior with command-line arguments:
In config.py
, you can adjust:
Reduce API costs:
TRADE_INTERVAL
to reduce decision frequencyImprove response times:
After testing thoroughly with the fork, if you want to trade with real funds:
config.py
:
--iterations
to limit trading sessionsThis is for educational and testing purposes only. Use test wallets with minimal funds. Never use production private keys. Monitor OpenRouter costs regularly. The fork environment uses test funds, but configuration errors could affect real accounts.
Next section: AI trading agent: Fine-tuning overview