Skip to main content

Introduction

The Finance Agent system implements Retrieval-Augmented Generation (RAG) with semantic data source routing, research planning, and iterative self-improvement for financial Q&A. This powers the chat and analysis features on Finance Agent.

Key Concepts

Semantic Routing

Routes to data sources based on question intent, not keywords

Research Planning

Agent explains reasoning before searching (“I need to find…”)

Multi-Source RAG

Combines earnings transcripts, SEC filings, and news

Self-Reflection

Evaluates answer quality and iterates until confident

Architecture

The agent follows a 6-stage pipeline with three specialized data source tools:
                             AGENT PIPELINE
═══════════════════════════════════════════════════════════════════════

┌──────────┐    ┌───────────────────┐    ┌──────────────────────────┐
│ Question │───►│ Question Analyzer │───►│  Semantic Data Routing   │
└──────────┘    │  (LLM via config) │    │                          │
                │                   │    │  • Earnings Transcripts  │
                │ Extracts:         │    │  • SEC 10-K Filings      │
                │ • Tickers         │    │  • Real-Time News        │
                │ • Time periods    │    │  • Hybrid (multi-source) │
                │ • Intent          │    └────────────┬─────────────┘
                └───────────────────┘                 │

                ┌─────────────────────────────────────────────────────┐
                │              RESEARCH PLANNING                       │
                │  Agent generates reasoning: "I need to find..."     │
                └────────────────────────┬────────────────────────────┘

                ┌─────────────────────────────────────────────────────┐
                │                  RETRIEVAL LAYER                     │
                │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐  │
                │  │  Earnings   │  │  SEC 10-K   │  │   Tavily    │  │
                │  │ Transcripts │  │   Filings   │  │    News     │  │
                │  │             │  │             │  │             │  │
                │  │ Vector DB   │  │ Section     │  │  Live API   │  │
                │  │ + Hybrid    │  │ Routing +   │  │             │  │
                │  │   Search    │  │ Reranking   │  │             │  │
                │  └──────┬──────┘  └──────┬──────┘  └──────┬──────┘  │
                └─────────┴───────────┬────┴────────────────┴─────────┘
                                      │ ▲
                                      │ │ Re-query with
                                      │ │ follow-up questions
                                      ▼ │
                ┌─────────────────────────────────────────────────────┐
                │               ITERATIVE IMPROVEMENT                  │
                │                                                      │
                │    ┌──────────┐    ┌──────────┐    ┌──────────┐     │
                │    │ Generate │───►│ Evaluate │───►│ Iterate? │─────┼───┐
                │    │  Answer  │    │ Quality  │    │          │     │   │
                │    └──────────┘    └──────────┘    └──────────┘     │   │
                │                                         │ NO        │   │ YES
                └─────────────────────────────────────────┼───────────┘   │
                                                          ▼               │
                                                   ┌─────────────┐        │
                                                   │   ANSWER    │        │
                                                   │ + Citations │        │
                                                   └─────────────┘        │
                                                          ▲               │
                                                          └───────────────┘

Semantic Data Source Routing

The agent routes questions based on intent, not just keywords. This is a key differentiator from simple keyword matching.

Data Source Tools

The main agent orchestrates access to three specialized data source tools:
Currently: 10-K only (annual reports) | Coming: 10-Q, 8-KBest for:
  • Annual/full-year financial data, audited figures
  • Balance sheets, income statements, cash flow statements
  • Executive compensation, CEO pay, stock awards (ONLY in 10-K!)
  • Risk factors, legal proceedings, regulatory matters
  • Detailed business descriptions, segment breakdowns
  • Multi-year historical comparisons
  • Total assets, liabilities, debt structure
Implementation: sec_filings_service_smart_parallel.pySee SEC Agent for detailed documentation.
Best for:
  • Questions explicitly requesting multiple perspectives
  • Comparing official filings with recent developments
  • Comprehensive analysis needing historical + current data
Automatically combines sources when needed.

Routing Decision Process

The LLM considers:
  1. Intent - What is the user trying to learn?
  2. Time Period - Annual=10K, Quarterly=Transcripts, Recent=News
  3. Formality - Official/Audited=10K, Commentary=Transcripts, Current=News
  4. Completeness - Would combining sources provide a better answer?

Routing Examples

QuestionRouted ToReasoning
”What was Apple’s Q4 2024 revenue?”TranscriptsQuarterly data, recent results
”What is Tim Cook’s compensation?“10-KExecutive compensation only in SEC filings
”Show me Microsoft’s balance sheet”10-KFinancial statements from annual reports
”What did management say about AI?”TranscriptsManagement commentary from earnings calls
”What’s the latest news on NVIDIA?”NewsRecent developments
”Compare 10-K risks with recent news”HybridNeeds multiple sources

Answer Modes

The agent configures iteration depth and quality thresholds based on question complexity:
ModeIterationsConfidenceWhen Used
direct270%Quick factual lookups
standard380%Default balanced analysis
detailed490%Comprehensive research
deep_search1095%Reserved (not emitted by current combined reasoning stage)
The combined reasoning stage currently outputs direct, standard, or detailed only. deep_search is reserved for future expansion.

Database Schema

Configuration

Environment Variables

OPENAI_API_KEY=...           # LLM provider key (OpenAI)
CEREBRAS_API_KEY=...         # LLM provider key (Cerebras)
TAVILY_API_KEY=...           # Real-time news search
DATABASE_URL=postgresql://...# Main database
PG_VECTOR=postgresql://...   # Vector search database
LOGFIRE_TOKEN=...            # Observability (optional)

RAG Configuration

{
    "chunks_per_quarter": 15,         # Results per quarter
    "max_quarters": 12,               # Max 3 years of data
    "max_tickers": 8,                 # Max companies per query

    # Hybrid search weights
    "keyword_weight": 0.3,
    "vector_weight": 0.7,

    # Models
    "cerebras_model": "qwen-3-235b-a22b-instruct-2507",
    "openai_model": "gpt-5-nano-2025-08-07",
    "evaluation_model": "qwen-3-235b-a22b-instruct-2507",
    "embedding_model": "all-MiniLM-L6-v2",
    "llm_provider": "cerebras",  # or "openai" | "auto"
}

Key Components

Core Files

FileDescription
__init__.pyPublic API — exports Agent, RAGAgent, create_agent()
agent_config.pyAgent configuration and iteration settings
prompts.pyCentralized LLM prompt templates (including planning)
rag/rag_agent.pyOrchestration engine with pipeline stages
rag/question_analyzer.pyLLM-based semantic routing (provider via config)

Data Sources (Tools)

FileToolDescription
rag/search_engine.pyTranscript SearchHybrid vector + keyword search
rag/sec_filings_service_smart_parallel.py10-K AgentPlanning-driven parallel retrieval
rag/tavily_service.pyNews SearchReal-time news via Tavily API

Supporting Components

FileDescription
rag/response_generator.pyLLM response generation, evaluation, planning
rag/database_manager.pyPostgreSQL/pgvector operations
rag/conversation_memory.pyMulti-turn conversation state
rag/config.pyRAG configuration

Usage Example

from agent import create_agent

agent = create_agent()

# Earnings transcript question (automatic routing)
async for event in agent.execute_rag_flow(
    question="What did $AAPL say about iPhone sales in Q4 2024?",
    stream=True
):
    if event['type'] == 'reasoning':
        print(f"Planning: {event['message']}")
    elif event['type'] == 'result':
        print(f"Answer: {event['data']['answer']}")

# 10-K question (automatically routes to SEC filings)
result = await agent.execute_rag_flow_async(
    question="What was Tim Cook's compensation in 2023?"
)

# News question (automatically routes to Tavily)
result = await agent.execute_rag_flow_async(
    question="What's the latest news on $NVDA?"
)

# Multi-ticker comparison
async for event in agent.execute_rag_flow(
    question="Compare $MSFT and $GOOGL cloud revenue",
    stream=True,
    max_iterations=4
):
    print(event)

Streaming Events

The agent streams real-time progress events to the frontend:
Event TypeDescription
progressGeneric progress updates
analysisQuestion analysis complete
reasoningAgent’s research planning statement
news_searchNews search results
10k_search10-K SEC search results
iteration_startBeginning of iteration N
agent_decisionAgent’s quality assessment
iteration_followupFollow-up questions being searched
iteration_searchNew chunks found
iteration_completeIteration finished
resultFinal answer with citations
rejectedQuestion rejected (out of scope)
errorError occurred

Limitations

  • Requires $TICKER format for company identification
  • Quarter availability varies by company
  • Companies describe fiscal years differently
  • No real-time stock price data
  • 10-K data limited to 2024-25 filings currently

Next Steps

Pipeline Stages

Deep dive into the 6-stage pipeline execution

Iterative Improvement

Learn how self-reflection and quality evaluation work

SEC Agent

Specialized 10-K retrieval with 91% accuracy

Build docs developers (and LLMs) love