Previous section: AI trading agent implementation
Project repository: Web3 AI trading agent
Understanding stateless agent architecture
A stateless agent operates without persistent memory between trading decisions, treating each market evaluation as an independent event.Stateless vs stateful design
Stateless operation principles A stateless agent processes each trading decision independently:- No historical context — each decision starts with a clean slate
- Current market focus — analyzes only present conditions
- Independent reasoning — no bias from previous trades
- Fresh perspective — uninfluenced by past successes or failures
- Simplified debugging — easier to trace decision logic
- Predictable behavior — consistent responses to similar market conditions
- Reduced complexity — fewer variables affect decision making
- No context window overflow — no warm-up period required
- No learning from experience — cannot improve from past mistakes
- Missing trend analysis — cannot identify longer-term patterns
- No strategy persistence — cannot maintain consistent approaches
Core stateless components
Ollama language model integration
Local AI decision engine Ollama Ollama provides the intelligence layer for trading decisions. MLX-LMYou can skip Ollama and run directly off MLX-LM by setting
USE_MLX_MODEL = True
in config.py
.- No LLM API dependence — no external LLM API calls; you run it all locally
- Consistent latency — predictable response times for time-sensitive decisions
- Cost efficiency — zero per-request charges for AI inference
Rebalancing logic system
The rebalancing system maintains target portfolio proportions through automated trading: 50/50 allocation strategy The default configuration maintains equal ETH and USDC holdings:- Reduced volatility — diversification across two assets
- Capture opportunities — profit from price movements in either direction
- Risk management — prevents overexposure to single asset
- Portfolio analysis — calculate current ETH-USDC ratio
- Deviation measurement — compare against target allocation
- Threshold evaluation — determine if rebalancing is required
- If rebalancing needed — execute trade directly without LLM consultation
- If no rebalancing needed — query LLM for trading decisions
The default
REBALANCE_THRESHOLD = 0.5
(50% deviation) is set high to disable automatic rebalancing and route most decisions through the LLM for learning purposes.Real-time market data collection
Data collection through Chainstack nodes The agent gathers live market data through your Chainstack BASE mainnet node: Pool state monitoring Data collected every trading cycle:- ETH price — current ETH/USDC exchange rate from pool
- 10-minute metrics — recent swap events analysis for volume and price changes; you can change the time cycle in
uniswap_v4_stateless_trading_agent.py
- Swap activity — transaction count and volume over last 10 minutes
Automated swap execution
Uniswap V4 integration The execution engine handles all blockchain interactions:- Transaction construction — builds optimal swap parameters
- Gas optimization — calculates efficient gas prices and limits
- Slippage protection — prevents excessive price impact
- Error handling — retries failed transactions with adjusted parameters
Configuration and deployment
Edit yourconfig.py
file with these:
Step-by-step deployment
Follow this sequence to launch your stateless trading agent.1. Ollama service initialization
Ensure Ollama is running and responsive:Again, feel free to use other models instead of Fin-R1 if you find it hallucinates or you do not like the responses in general.
2. Foundry environment setup
Launch your local blockchain fork in a separate terminal:3. Agent execution
Launch the stateless trading agent:If you are getting a message that the model failed to respond with
Error getting LLM decision
, test your model’s response time manually and increase the TRADE_INTERVAL
in config.py
if your LLM takes longer than the configured interval to respond.ollama list
.
MLX-LM:
mlx_lm.manage --scan --pattern ""
.
If responses consistently take longer than your TRADE_INTERVAL
setting, increase the interval to allow sufficient processing time.
Model response quality
If you are getting inconsistent or poor trading decisions:
- Experiment with different model temperatures (0.1-0.7) in the agent script
- Shop around for models at https://ollama.com/library/ or https://huggingface.co/models
- Adjust prompt engineering in the agent script
- Verify market data quality and completeness with
fetch_pool_data.py
,fetch_pool_stats.py
,fetch_pools_stats_from_fork.py