cicboy's picture
Fix app crash: lazy-init OpenAI client, fix delegation typo, add load_dotenv
26b7ac7
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Project Overview
CrewAI-based multi-agent cryptocurrency analysis system with a Gradio web interface. Six specialized AI agents execute sequentially to fetch live market data, analyze historical trends, assess news sentiment, compute composite analytics, formulate trading strategy, and generate a narrative Markdown report.
## Commands
```bash
# Setup
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
# Run locally (launches Gradio on 0.0.0.0:7860)
python app.py
```
No test suite or linter is configured. Testing is manual via the Gradio UI.
## Required Environment Variables
Set these before running (or use a `.env` file):
- `OPENAI_API_KEY` β€” GPT-4 access for LLM agents and sentiment analysis
- `SERPER_API_KEY` β€” Serper search API for fetching crypto news
- `COINGECKO_API_KEY` β€” CoinGecko market data (optional but recommended)
## Architecture
**Entry point**: `app.py` β€” defines all agents, tasks, the CrewAI Crew, and the Gradio UI.
### Agent Pipeline (sequential execution via CrewAI)
```
User Input (crypto name, currency, lookback days)
β†’ Market Agent (gpt-4o-mini) + MarketDataTool β†’ live price, 24h volume
β†’ Historical Agent (gpt-4o-mini) + HistoricalDataTool β†’ price history, % change, volatility, trend
β†’ Sentiment Agent (gpt-4.1) + SentimentTool β†’ news headlines, sentiment score, confidence
β†’ Analytics Agent (gpt-4o-mini) + AnalyticsTool β†’ composite score, alignment, effective sentiment
β†’ Strategy Agent (gpt-4.1) β†’ trading bias, risk guidance
β†’ Reporting Agent (gpt-4.1) β†’ final Markdown report (narrative prose, no bullets)
```
### Tools (`tools/`)
Each tool extends CrewAI's `BaseTool` and returns a structured JSON dict:
| Tool | File | External API | Key Behavior |
|------|------|-------------|--------------|
| `MarketDataTool` | `market_data.py` | CoinGecko `/simple/price` | Fetches live price + 24h volume |
| `HistoricalDataTool` | `historical_data_tool.py` | CoinGecko `/market_chart` | Computes % change, volatility (stdev of daily returns), trend classification |
| `SentimentTool` | `sentiment_tool.py` | Serper + OpenAI GPT-4.1 | Fetches ~12 headlines, analyzes sentiment with LLM, validates/bounds scores |
| `AnalyticsTool` | `analytics_tool.py` | None (aggregation) | Composite score = `(pct_change/10) + (effective_sentiment*1.5) - (volatility/100)`, bounded [-1,1] |
### Data Flow
Each tool returns a dict that downstream agents consume. The Analytics Agent aggregates all three data sources (market, historical, sentiment) into a single scored assessment. The Strategy and Reporting agents have no dedicated tools β€” they reason over prior agent outputs.
## Deployment
Supports HuggingFace Spaces (Gradio SDK). Set API keys as Space secrets. The `README.md` contains the HuggingFace Spaces metadata card.
## Key Patterns
- All tools use 10-second HTTP timeouts and return error dicts on failure
- Sentiment tool has multi-layer fallbacks: graceful neutral defaults if API keys missing, JSON extraction with substring fallback, bounds validation on all scores
- LLM model selection is hardcoded per agent in `app.py` (mix of `gpt-4o-mini` for data tasks, `gpt-4.1` for reasoning tasks)
- The `generate_report()` function in `app.py` is the Gradio callback that instantiates and kicks off the Crew