Date: October 17, 2025
Status:
The current swarm agents are NOT calling LLMs for analysis. They use hardcoded logic instead of AI-powered intelligence.
-
Market Analyst Agent (
src/agents/swarm/agents/market_analyst.py):- Uses simple if/else logic for recommendations
- No LLM calls
- Example: Lines 150-166 show hardcoded decision logic
-
Other Agents (Risk Manager, Options Strategist, etc.):
- All use placeholder/mock analysis
- Return static responses
- No actual AI reasoning
-
Firecrawl Integration:
- Marked as "TODO" in multiple files
- Not actually fetching web data
- Placeholder responses only
I've created a new LLM-powered agent architecture:
Features:
- ✅ Calls OpenAI GPT-4 API
- ✅ Calls Anthropic Claude API
- ✅ Calls LMStudio (local) API
- ✅ Automatic fallback chain (OpenAI → Anthropic → LMStudio)
- ✅ Firecrawl integration mixin (ready for web scraping)
Key Methods:
def call_llm(prompt, system_prompt, temperature, max_tokens) -> str:
"""Call LLM with automatic fallback"""
def search_web(query, max_results) -> Dict:
"""Search web using Firecrawl MCP"""
def get_news(symbol, days) -> Dict:
"""Get news for symbol using Firecrawl"""How It Works:
- Fetches real market data (yfinance)
- Gets news via Firecrawl (placeholder ready)
- Builds comprehensive prompt with all data
- Calls LLM (OpenAI/Anthropic/LMStudio) for analysis
- Parses LLM response into structured recommendations
- Returns AI-powered insights
Example Prompt Sent to LLM:
Analyze the current market conditions and provide insights:
MARKET DATA (1-Month Performance):
{
"SPY": {"name": "S&P 500", "change_pct_1m": 1.10, ...},
"QQQ": {"name": "NASDAQ", "change_pct_1m": 2.54, ...}
}
SECTOR PERFORMANCE (1-Month):
{
"Technology": {"change_pct_1m": 5.23},
"Healthcare": {"change_pct_1m": 4.95}
}
Portfolio Context:
- Total Value: $13,820.88
- Unrealized P&L: -$2,000.03 (-15.38%)
Provide analysis in JSON format with:
1. market_regime
2. trend
3. key_insights
4. top_sectors
...
LLM Response (parsed and structured):
{
"market_regime": "bull_market",
"trend": "bullish",
"confidence": 0.85,
"key_insights": [
"Technology sector showing strong momentum (+5.23%)",
"All major indices positive over 1-month period",
"Moderate volatility environment (VIX: 20)"
],
"top_sectors": ["Technology", "Healthcare", "Utilities"],
"risk_factors": ["Concentrated NVDA position (60% of portfolio)"]
}Add to .env file:
# OpenAI (GPT-4)
OPENAI_API_KEY=sk-your-key-here
# Anthropic (Claude)
ANTHROPIC_API_KEY=sk-ant-your-key-here
# LMStudio (local)
LMSTUDIO_API_BASE=http://localhost:1234/v1
LMSTUDIO_MODEL=local-modelModify src/api/swarm_routes.py to use LLM-powered agents:
from src.agents.swarm.agents.llm_market_analyst import LLMMarketAnalystAgent
# In create_swarm() function:
market_analyst = LLMMarketAnalystAgent(
agent_id="market_analyst_1",
shared_context=shared_context,
consensus_engine=consensus_engine,
preferred_model="openai" # or "anthropic" or "lmstudio"
)
coordinator.register_agent(market_analyst)Follow the same pattern for:
- Risk Manager Agent
- Options Strategist Agent
- Sentiment Analyst Agent
- Portfolio Optimizer Agent
Each should:
- Inherit from
LLMAgentBaseandFirecrawlMixin - Implement
get_system_prompt()with specialized instructions - Build comprehensive prompts with real data
- Call
self.call_llm()for analysis - Parse and structure LLM responses
- ✅ Mixin class created (
FirecrawlMixin) - ✅ Methods defined (
search_web,get_news,get_social_sentiment) - ⏳ TODO: Connect to actual Firecrawl MCP
The Firecrawl MCP tools are available in this environment. Update the FirecrawlMixin methods:
def search_web(self, query: str, max_results: int = 5) -> Dict[str, Any]:
"""Search web using Firecrawl MCP"""
# Call the actual Firecrawl MCP tool
from firecrawl_mcp import firecrawl_search
results = firecrawl_search(
query=query,
limit=max_results,
scrapeOptions={
"formats": ["markdown"],
"onlyMainContent": True
}
)
return {
'query': query,
'results': results,
'timestamp': datetime.utcnow().isoformat(),
'source': 'firecrawl'
}# Old market_analyst.py
if market_regime == 'bull_market' and volatility == 'low':
overall_action = 'buy'
confidence = 0.8
elif market_regime == 'bear_market':
overall_action = 'hedge'
confidence = 0.7Problems:
- No nuance or context
- Can't adapt to complex situations
- No reasoning provided
- Fixed confidence scores
# New llm_market_analyst.py
llm_response = self.call_llm(
prompt=comprehensive_market_analysis_prompt,
system_prompt=expert_analyst_instructions
)Benefits:
- ✅ Nuanced analysis based on all available data
- ✅ Adapts to complex market conditions
- ✅ Provides detailed reasoning
- ✅ Dynamic confidence based on data quality
- ✅ Can incorporate news, sentiment, and context
- ✅ Learns from patterns in data
from src.agents.swarm.agents.llm_market_analyst import LLMMarketAnalystAgent
from src.agents.swarm import SharedContext, ConsensusEngine
# Create components
shared_context = SharedContext()
consensus_engine = ConsensusEngine()
# Create LLM-powered agent
agent = LLMMarketAnalystAgent(
agent_id="test_analyst",
shared_context=shared_context,
consensus_engine=consensus_engine,
preferred_model="openai" # Requires OPENAI_API_KEY
)
# Run analysis
context = {
'portfolio': {
'total_portfolio_value': 13820.88,
'total_unrealized_pnl': -2000.03,
'positions': [...]
}
}
analysis = agent.analyze(context)
print(json.dumps(analysis, indent=2))
# Get recommendations
recommendation = agent.make_recommendation(analysis)
print(json.dumps(recommendation, indent=2))-
Set API Keys:
- Add OpenAI API key to
.env - Or add Anthropic API key
- Or configure LMStudio
- Add OpenAI API key to
-
Update Swarm Routes:
- Replace old agents with LLM-powered versions
- Test with portfolio analysis
-
Integrate Firecrawl:
- Update
FirecrawlMixinmethods - Connect to actual Firecrawl MCP tools
- Test web scraping
- Update
-
Create LLM Versions of All Agents:
- LLM Risk Manager
- LLM Options Strategist
- LLM Sentiment Analyst
- LLM Portfolio Optimizer
-
Add Caching:
- Cache LLM responses (5-minute TTL)
- Reduce API costs
- Faster repeated analyses
-
Add Monitoring:
- Track LLM API calls
- Monitor token usage
- Log response quality
-
Fine-Tuning:
- Collect high-quality analysis examples
- Fine-tune models on options trading
- Improve accuracy
-
Multi-Model Ensemble:
- Use GPT-4 for market analysis
- Use Claude for risk assessment
- Use LMStudio for quantitative analysis
- Combine insights
Problem: Agents were using hardcoded logic, not AI
Solution: Created LLM-powered agent base class
Status: ✅ Framework ready, needs API keys and integration
Impact: Will provide real AI-powered analysis instead of simple if/else logic
To enable: Set API keys in .env and update swarm routes to use LLMMarketAnalystAgent
Files Created:
src/agents/swarm/llm_agent_base.py- LLM integration base classsrc/agents/swarm/agents/llm_market_analyst.py- LLM-powered market analystLLM_INTEGRATION_GUIDE.md- This guide
Next: Create LLM versions of remaining agents and integrate Firecrawl MCP