AI-Powered Financial Intelligence Agent | Multi-Source Data Integration | Production-Ready Architecture
A sophisticated financial analysis agent that answers complex financial questions by automatically routing queries to the right data sources and synthesizing results with confidence scoring.
Ask it: "What were Apple's top 3 competitors and their Q3 revenues?"
It will:
- Identify the query requires competitor data + financials
- Fetch competitors from market data APIs
- Extract quarterly revenue from SEC XBRL filings
- Return formatted answer with confidence scores and audit trail
3-7 seconds cold start | <1 second cached
# Clone repo
git clone https://github.com/yourusername/sagard-analyst-sentinel.git
cd sagard-analyst-sentinel
# Create virtual environment
python3.10 -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Set up environment
cp .env.example .env
# Edit .env with your API keys:
# - GOOGLE_API_KEY (free from aistudio.google.com)
# - SEC_API_USER_AGENT (your name and email)
# - POLYGON_API_KEY (optional, free tier available)# Start Streamlit dashboard
streamlit run app.py
# Navigate to http://localhost:8501
# Enter a financial query and click "Analyze"What was Apple's revenue last quarter?
β Confidence: 100% | Source: SEC 10-Q
Find Tesla's top 5 competitors
β Confidence: 85% | Source: Polygon.io (Professional API)
Top 10 healthcare companies by market cap
β Confidence: 80% | Source: yfinance
How is AI disrupting the financial sector?
β Confidence: 70% | Source: Web research synthesis
User Query (Streamlit UI)
β
LangGraph State Machine
βββ Planner (Gemini 2.0 Flash)
β ββ Decides: Which tools to use?
β
βββ Executor (Tool Router)
β ββ SEC Analyzer (edgartools) β 10-Q financials
β ββ Competitor Finder (Polygon + yfinance) β Market data
β ββ Top Companies (yfinance) β Rankings
β ββ AI Research (Tavily + Gemini) β Web synthesis
β
βββ Reporter (Formatter)
ββ Answer + Metrics + Audit Trail
β
User sees: Answer | Confidence | Latency | Execution Log
| Decision | Why | Benefit |
|---|---|---|
| LangGraph | Industry-standard state machine for agents | Explicit flow, testable, easy to extend |
| Gemini 2.0 Flash | 78% SWE-bench, 3x faster than alternatives | Fast planning, low cost ($0.001/1K tokens) |
| SEC XBRL | Official structured data from SEC | 100% accurate, no OCR errors |
| Cascading Sources | Polygon (professional) β yfinance (free fallback) | Always works, no single point of failure |
| Confidence Scoring | Track data quality by source | Users know: "Should I trust this number?" |
| Type Safety | TypedDict + pydantic validation | Catch errors early, prevent bugs |
- LangGraph 0.2.50 - Agentic workflow orchestration
- LangChain Core 0.3.28 - LLM integrations
- Python 3.10+ - Modern runtime
- Gemini 2.0 Flash - Query decomposition and planning
- edgartools 2.28.0 - SEC XBRL extraction (10-Q/10-K)
- Polygon.io API - Professional market data
- yfinance 0.2.50 - Free market data (fallback)
- Tavily 0.5.0 - AI-optimized web search
- Streamlit 1.41.1 - Web dashboard
- pydantic 2.10.4 - Data validation
- JSON Caching - 24-hour TTL in
.cache/ - python-dotenv - Environment configuration
User enters natural language query. Gemini LLM reads system prompt describing 5 available tools and their purposes, then decides which tools to call.
Example:
User: "What was Apple's revenue last quarter?"
Planner reasoning:
β User asking about financial metrics
β Decision tree: financials β use SEC analyzer
β Extract parameter: ticker = "AAPL"
Output: {"tool_name": "get_quarterly_financials", "parameters": {"ticker": "AAPL"}}
Why LLM instead of rules?
- Handles natural language variations (synonyms, rephrasing)
- Intelligent parameter extraction
- Supports complex multi-tool queries
- Easy to extend (add tool = update system prompt)
Router matches tool name to Python function, executes with error isolation.
Each tool returns ToolResult:
{
"success": bool,
"data": dict,
"confidence": float, # 0.0-1.0 based on source reliability
"source": str, # Where data came from
"error": Optional[str]
}Confidence by Source:
- 1.0 = SEC XBRL (official, audited)
- 0.85 = Polygon.io (professional API)
- 0.8 = yfinance (reliable, unofficial)
- 0.7 = Web research (synthesized)
- 0.0 = Tool error (failed execution)
Format raw data for display, calculate metrics.
What user sees:
Confidence: 100%
Answer: Apple's revenue was $94.93B (Q3 2024)
Latency: 3.2 seconds
Tools used: SEC Analyzer
Audit Trail (expandable):
- Query analyzed
- Tool routing decision
- SEC EDGAR 10-Q fetched
- XBRL parsed
- Revenue tag extracted
- Result formatted
- One tool failure β system failure
- Failed tools return error ToolResult
- Executor continues with other tools
- Overall confidence reflects partial failures
Example:
Query: "Top tech companies and their AI disruption impact"
If Competitor Finder fails:
- Top Companies succeeds (confidence 0.8)
- AI Research succeeds (confidence 0.7)
- Overall confidence = 0.75 (partial success)
- User sees answer with caveat
sagard-analyst-sentinel/
βββ app.py # Streamlit dashboard
βββ agent/
β βββ graph.py # LangGraph state machine
β βββ state.py # AgentState schema
β βββ __init__.py
βββ nodes/
β βββ planner.py # Gemini planning node
β βββ executor.py # Tool routing & execution
β βββ reporter.py # Formatting & metrics
β βββ __init__.py
βββ tools/
β βββ sec_analyzer.py # SEC XBRL extraction
β βββ competitor_finder.py # Market data with cascading sources
β βββ top_companies.py # Company rankings
β βββ ai_disruption.py # Web research synthesis
β βββ __init__.py
βββ tests/
β βββ test_complete_flow.py # End-to-end tests
β βββ test_sec_tool.py # SEC analyzer tests
β βββ test_tool_routing.py # Tool matching tests
βββ .cache/ # Caching (24h TTL)
βββ requirements.txt
βββ .env.example
βββ README.md
βββ docs/
βββ ARCHITECTURE.md # Detailed system design
βββ CONFIDENCE_SCORING.md # Data quality framework
βββ COMPETITOR_FINDER.md # Competitor matching logic
| Phase | Time | What Happens |
|---|---|---|
| Input Processing | 0.1s | Query validation |
| Planning (Gemini) | 1-2s | LLM decides tools |
| Execution (APIs) | 2-5s | Fetch data from sources |
| Formatting | 0.1s | Format answer + metrics |
| Total (cold) | 3-7s | All steps |
| Total (cached) | <1s | Cache hit, no APIs |
- First query: API calls happen (3-7s)
- Same query within 24h: Instant from cache (<1s)
- Demo: Pre-populate
.cache/with common queries for zero latency
Current: Single-threaded, perfect for demo/prototype
Phase 2 (Production):
- Async tool execution (concurrent API calls)
- Redis distributed cache
- PostgreSQL for historical queries
- Load balancer for multiple instances
- Circuit breakers for API failures
β Multi-source data integration (SEC + Market Data + Web)
β Cascading data sources (professional β free fallback)
β Confidence scoring by source reliability
β Complete audit trail (full execution trace)
β Error isolation (partial failures handled gracefully)
β Type-safe state management (TypedDict + pydantic)
β Caching with TTL
β Execution logging
β Web dashboard (Streamlit)
β REST API (/api/v1/analyze)
β Database backend (PostgreSQL)
β Async/parallel tool execution
β Multi-LLM support (Gemini OR Claude)
β Advanced retry logic (exponential backoff)
β Distributed caching (Redis)
β Structured logging (ELK stack)
β Monitoring & metrics (Prometheus + Grafana)
What was Apple's revenue last quarter?
β Pulls from SEC 10-Q (Confidence: 100%)
Show me Tesla's net income for 2023
β Aggregates 4 quarterly 10-Qs (Confidence: 100%)
What's Microsoft's operating margin?
β Calculates from 10-Q data (Confidence: 100%)
Find Tesla's main competitors
β Uses Polygon + yfinance (Confidence: 85%)
Top 10 healthcare companies by market cap
β Ranks companies by market data (Confidence: 80%)
Who competes with Apple in consumer electronics?
β Market data + industry research (Confidence: 82%)
How is AI disrupting healthcare?
β Web research synthesis (Confidence: 70%)
What are the latest trends in fintech?
β Web search + LLM analysis (Confidence: 70%)
Show me companies investing in quantum computing
β Web + market data synthesis (Confidence: 65%)
# All tests
pytest
# Specific test
pytest tests/test_sec_tool.py
# With coverage
pytest --cov=agent --cov=nodes --cov=tools- End-to-end tests - Full workflow from query to answer
- Unit tests - Individual tools (SEC, Polygon, yfinance)
- Tool routing tests - LLM decision logic
- Error handling tests - Failures and edge cases
When implemented:
# Query the agent
curl -X POST http://localhost:8000/api/v1/analyze \
-H "Content-Type: application/json" \
-d '{
"query": "What was Apple revenue last quarter?",
"include_audit_trail": true
}'
# Response
{
"success": true,
"answer": "Apple's revenue was $94.93B in Q3 2024",
"confidence": 1.0,
"latency_ms": 3200.5,
"tools_used": ["sec_analyzer"],
"audit_trail": {...}
}- State Machine (LangGraph) - Explicit workflow orchestration
- Tool Router - Strategy pattern for tool selection
- Cascading Fallbacks - Resilience through data source redundancy
- Type-Safe State - TypedDict for compile-time safety
- Error Isolation - Graceful degradation on partial failures
- Confidence Scoring - Explicit data quality tracking
- Clean architecture (separation of concerns)
- Error handling (tool-level isolation)
- Type safety (TypedDict + pydantic)
- Caching strategy (file-based, 24h TTL)
- Audit trail (complete transparency)
- Confidence scoring (data quality tracking)
- Unit tests (in progress)
- REST API (Phase 2)
- Database backend (Phase 2)
- Monitoring & logging (Phase 2)
This project demonstrates:
- Systems Design Thinking - Multi-layer architecture, not spaghetti code
- Production Mindset - Error handling, reliability, transparency
- Domain Knowledge - Understands financial data (SEC XBRL, market data)
- Real Integration - Connects to actual APIs (Polygon, SEC, Tavily), not mock data
- Code Quality - Type safety, separation of concerns, clean patterns
- Multi-source data integration
- Confidence scoring and data quality
- LLM tool calling and planning
- Error handling and robustness
- LangGraph orchestration patterns
- Cascading data sources
- Caching strategies
- Type-safe state management
- Scalability thinking (Phase 2 roadmap)
- Solves real problem (financial analysis)
- User-friendly interface (Streamlit)
- Audit trail and transparency (compliance-minded)
- Extensible architecture (easy to add features)
Not hardcoded if/else rules for tool selection. Gemini LLM understands system prompt describing tools and decides which to use. This makes it flexible, maintainable, and handles complex queries.
Polygon.io is professional but rate-limited (5 calls/min free tier). yfinance is free and unlimited but unofficial. By cascading (try Polygon β fallback to yfinance), we get professional data when possible, always have a backup, and users never see "API failed."
In finance, users need to know: "Should I trust this?" SEC data is 1.0 because it's official and audited. Polygon is 0.85 (professional but has rate limits). yfinance is 0.8 (reliable but unofficial). Web research is 0.7 (synthesized from noisy sources). Transparency builds trust.
Python is dynamically typed, which makes typos easy. TypedDict for AgentState and pydantic for ToolResult enforce contracts between components. Catches bugs before they reach production.
Phase 2 (Coming Soon):
- REST API for programmatic access
- Database backend (PostgreSQL)
- Async tool execution (10x faster)
- Advanced retry logic
- Distributed caching (Redis)
Phase 3 (Future):
- Multi-LLM support
- Custom fine-tuning on financial data
- Real-time monitoring dashboard
- Enterprise features (auth, rate limits, etc)
Made with π₯ for Georgia Tech & Sagard