Previous section: AI trading agent: Stateless agent
Project repository: Web3 AI trading agent
Learning capabilities
The stateful agent implements several learning mechanisms: Historical performance analysis- Tracks profit/loss for each trade type (ETH->USDC vs USDC->ETH)
- Calculates success rates and identifies most profitable strategies
- Analyzes recent performance trends to detect improving or declining performance
- Identifies price levels where trades were most successful
- Correlates market volatility with trading outcomes
- Detects market conditions similar to past successful trades
- Includes recent trade history in LLM prompts
- Provides performance insights: “ETH selling trades averaged +2.3% profit (5 trades)”
- Highlights similar market conditions: “Similar conditions: 3 trades, avg profit +1.8%”
- Preserves key learning insights during context summarization
- Retains top-performing trades and recent trading history
- Maintains performance metrics across context resets
Technical implementation
The stateful agent implements sophisticated technical mechanisms to manage memory, context windows, and learning persistence.Context window management
Token estimation and monitoring The agent continuously tracks context usage through advanced token estimation:- Warning threshold: 90% of context capacity (configurable via
CONTEXT_WARNING_THRESHOLD
) - Automatic trigger: Summarization activates when threshold exceeded
- Cooldown period: 2-minute between summarizations (configurable via
SUMMARIZATION_COOLDOWN
) - Recursive protection: Prevents summarization loops during processing
Configuration integration
Model-specific context handling The agent automatically adapts to different model capabilities throughconfig.py
:
- Context warning threshold:
CONTEXT_WARNING_THRESHOLD = 0.9
(90%) - Summarization cooldown:
SUMMARIZATION_COOLDOWN = 2 * 60
(2 minutes) - Test mode: Reduced context capacity for testing summarization logic
- Model selection: Automatic adaptation to selected model’s capabilities
For troubleshooting, to force an actual transaction even if the LLM model advises to hold based on the market observation, you can set
REBALANCE_THRESHOLD = 0
to force a trade without asking an LLM instance.Observation mode capabilities
Data collection without trading The agent supports observation-only mode for market analysis and strategy development. You can start the stateful agent in observation mode for a number of cycles and then it’ll switch to active trading but will act based on the collected observation data. Collect market data for 5000 tokens before trading:Memory optimization techniques
Learning insight compression To prevent context explosion, the agent employs sophisticated compression:- Learning insights: Top 2 most valuable patterns (compressed to 200 characters)
- Performance metrics: Last 3 results for each trade type
- Market states: Most recent 2 market conditions
- Trading decisions: Best performing trade + most recent trade
- Strategy information: Current strategy parameters and timing
- Compression limits: All insights truncated to manageable sizes
- Retention limits: Fixed maximum items preserved per category
- Cooldown enforcement: Minimum time between summarization events
- Recursive protection: Flags prevent summarization during summarization
- Priority-based selection: Keeps most valuable data, discards redundant information
The agent provides real-time visibility into memory usage in the terminal printouts.
Core stateful components
Memory management system
Trading history database The agent maintains comprehensive trading records in memory using theTradingDecision
on market states and trading decisions.
Context window management
Intelligent memory allocation The system optimizes memory usage based on the model’s context capacity and the configured thresholds inconfig.py
. The agent automatically determines optimal memory allocation using the existing configuration parameters.
Adaptive summarization algorithm
When approaching context limits, the agent compresses data through the _summarize_and_restart_context()
method.
Strategy persistence engine
Basic strategy tracking The agent maintains simple strategy information inTradingContext
.
Strategy functionality
The system provides basic strategy management:
- Strategy generation — creates initial strategy based on observation mode
- Strategy timing — tracks duration and elapsed time using MIN/MAX_STRATEGY_DURATION
- Performance tracking — separates rebalancing ROI from LLM trading ROI
- Strategy display — shows current strategy information in portfolio table
Basic analytics engine
Simple performance tracking The agent tracks basic performance metrics throughTradingContext
for rebalancing script PnL and LLM-driven PnL.
Configuration and deployment
Stateful agents require careful configuration to balance memory usage, performance tracking, and strategic consistency.Step-by-step stateful agent deployment
Deploy your memory-enabled trading agent with proper initialization and monitoring.1. Environment preparation
Ensure your base environment is ready: Verify Ollama is running with adequate memory:2. Stateful agent initialization
Launch the stateful trading agent with various configuration options: Basic trading mode:Next steps
With your stateful agent successfully managing memory and learning from trading history, you’re ready to explore advanced model integrations for enhanced decision-making capabilities.Next section: AI trading agent: Grok-4 integration with OpenRouter