Stock Portfolio Agent
This project shows how to create an AI agent that streams the portfolio analysis workflow in real-time. Built with CrewAI (backend), React/Next.js (frontend), the CopilotKit's AG-UI Protocol enables users to watch the agent fetch stock data, calculate allocations, and generate insights live.
Tech stack:
- React + Next.js for the frontend UI
- FastAPI + Uvicorn for the backend API
- CopilotKit + AG UI Protocol for streaming agent events
- CrewAI for the agent workflow
- yfinance and pandas for market data and analysis
Setup
-
Install Dependencies:
uv sync # Install frontend dependencies cd frontend npm install cd .. -
Environment Variables:
agent/.env(backend):OPENAI_API_KEY=your-keyfrontend/.env(frontend):OPENAI_API_KEY=your-openai-key NEXT_PUBLIC_CREWAI_URL=http://127.0.0.1:8000/crewai-agent
-
Configure Backend URL (optional): The frontend expects the backend to run locally. If you change host/port, update the frontend API call configuration accordingly.
-
Run the App:
# Start backend uv run python agent/main.py # In another terminal, start frontend cd frontend npm run dev
Usage
-
Open the UI:
- Visit
http://localhost:3000.
- Visit
-
Run a Stock Analysis:
- Use the prompt/input in the UI to ask for a portfolio analysis (e.g., "Analyze AAPL and MSFT with $10k each").
- Watch live progress: messages, tool calls, and intermediate results stream into the UI via AG UI Protocol events.
-
Review Results:
- View allocation summaries, charts, and insights rendered by the frontend components.
Development Notes
- If your editor reports missing imports, ensure it points to the same Python environment where you installed dependencies (
uv,venv,conda, etc.). Runninguv syncin the repo root is recommended, but any standard Python environment manager works. - The FastAPI app is in
agent/main.py; core workflow logic is inagent/stock_analysis.py.
📬 Stay Updated with Our Newsletter!
Get a FREE Data Science eBook 📖 with 150+ essential lessons in Data Science when you subscribe to our newsletter! Stay in the loop with the latest tutorials, insights, and exclusive resources. Subscribe now!
Contribution
Contributions are welcome! Please open an issue or submit a PR.
