DEV Community

Neeraj Ciju
Neeraj Ciju

Posted on

I Built an Agentic AI Stock Research Terminal with LangChain

What if you could type a stock ticker and get more than just its current price?

I wanted to build something that could combine real financial data, fundamental valuation, and recent market information into one place.

So I built StockAny AI — an AI-powered equity research and valuation terminal.

👉 GitHub: https://github.com/iPrq/Stock-Market-Analyser

The idea is simple:

Enter a ticker → fetch financial data → calculate intrinsic value → research recent developments → generate an investment thesis.

And the interesting part is that the AI isn't responsible for doing everything.

The financial calculations happen in the backend, while Gemini is used to interpret the data and research recent developments.


What does StockAny AI actually do?

For any stock ticker, the application performs four main steps:

  1. Fetches live financial data
  2. Calculates an estimated intrinsic value using a DCF model
  3. Calculates the margin of safety
  4. Uses Gemini + web search to generate an investment thesis

The final result gives you things like:

  • Current stock price
  • Estimated intrinsic value
  • Margin of safety
  • BUY / HOLD / SELL recommendation
  • AI-generated investment thesis
  • Bull case
  • Bear case
  • Recent developments and catalysts
  • Sources used for the research

The goal wasn't to build another stock-price dashboard.

I wanted it to feel more like a mini equity research terminal.


The Architecture

The application is split into two major parts:

                 ┌─────────────────────┐
                 │    Next.js Frontend │
                 │   React 19 + TS     │
                 └──────────┬──────────┘
                            │
                     POST /api/analyze
                            │
                            ▼
                 ┌─────────────────────┐
                 │    FastAPI Backend  │
                 │      Python         │
                 └──────────┬──────────┘
                            │
             ┌──────────────┴──────────────┐
             │                             │
             ▼                             ▼
      Financial Data                 DCF Valuation
        FMP API                     Intrinsic Value
             │                             │
             └──────────────┬──────────────┘
                            ▼
                  ┌──────────────────┐
                  │ Gemini 3.1 Flash │
                  │      Lite        │
                  └────────┬─────────┘
                           │
                           ▼
                    Tavily Web Search
                           │
                           ▼
                  Structured Analysis
                           │
                           ▼
                    Next.js Results
Enter fullscreen mode Exit fullscreen mode

The frontend is built with Next.js 16, React 19 and Tailwind CSS v4, while the backend uses Python, FastAPI and Uvicorn.


Why not just ask an LLM?

This was one of the most important design decisions.

If you simply ask an LLM:

"Is NVIDIA a good investment?"

you'll get an answer, but there are several problems.

The model shouldn't be responsible for inventing financial numbers or performing the entire valuation itself.

Instead, StockAny separates the responsibilities.

The backend handles the numbers

Financial Modeling Prep provides data such as:

  • Stock price
  • Market capitalization
  • P/E ratio
  • Free cash flow
  • Shares outstanding
  • Sector information

The backend then uses this data for the actual valuation.

Gemini handles interpretation

Gemini is given the financial context and uses Tavily to research recent information such as:

  • Earnings
  • Company developments
  • Competitive threats
  • Catalysts
  • Recent news

This creates a much more useful separation:

Financial APIs
      ↓
Reliable numerical data
      ↓
DCF calculation
      ↓
Financial context
      ↓
Gemini + web research
      ↓
Human-readable investment thesis
Enter fullscreen mode Exit fullscreen mode

Building the DCF Model

The core of the valuation system is a two-stage Discounted Cash Flow model.

The first stage projects free cash flow for five years.

The default assumptions are:

Growth rate:       8%
Discount rate:     9%
Terminal growth:  2.5%
Projection period: 5 years
Enter fullscreen mode Exit fullscreen mode

The projected cash flows are discounted back to their present value.

Then we calculate a terminal value based on perpetual growth.

Conceptually:

              FCF₁     FCF₂     FCF₃     FCF₄     FCF₅
               │        │        │        │        │
               ▼        ▼        ▼        ▼        ▼
             Discount each cash flow to present value
                              │
                              ▼
                     Terminal Value
                              │
                              ▼
                  Discount terminal value
                              │
                              ▼
                ┌───────────────────────┐
                │ Enterprise/Equity     │
                │ Value Estimate        │
                └───────────┬───────────┘
                            ▼
                     Shares Outstanding
                            │
                            ▼
                   Intrinsic Value/Share
Enter fullscreen mode Exit fullscreen mode

The intrinsic value per share is then compared with the current market price.


Margin of Safety

One of the most useful outputs is the margin of safety.

The calculation is:

Margin of Safety =
(Intrinsic Value - Current Price)
--------------------------------- × 100
          Current Price
Enter fullscreen mode Exit fullscreen mode

For example, if a stock is trading at $100 and the calculated intrinsic value is $130:

(130 - 100) / 100 × 100
= 30%
Enter fullscreen mode Exit fullscreen mode

The application can then visually show the gap between the current market price and the estimated intrinsic value.

This gives the AI something quantitative to reason about instead of simply asking it to make a prediction.


Adding Web Research with Tavily

Financial numbers alone aren't enough.

A company's valuation can look attractive while something important has changed recently.

For example:

  • A major competitor launches a new product
  • Earnings guidance changes
  • A company loses an important customer
  • Regulation affects an industry
  • Management announces a major acquisition

So I added Tavily Search to the pipeline.

The AI can search for recent information about the company before generating its thesis.

The resulting workflow becomes:

Financial fundamentals
        +
DCF valuation
        +
Recent web information
        ↓
    Gemini
        ↓
Investment thesis
Enter fullscreen mode Exit fullscreen mode

This is also why the output includes source links.

The goal isn't to have an AI confidently hallucinate an explanation.

The goal is to give it real inputs and let it synthesize them.


Structured AI Output

Instead of asking Gemini to return a giant block of text, the backend expects structured information.

Something conceptually like:

{
  "recommendation": "BUY",
  "thesis": "...",
  "bull_case": "...",
  "bear_case": "...",
  "sources": []
}
Enter fullscreen mode Exit fullscreen mode

This makes the frontend much easier to build.

The UI can independently render:

┌─────────────────────────────┐
│          BUY                │
│                             │
│ Current Price     $100      │
│ Intrinsic Value   $130      │
│ Margin of Safety  +30%      │
└─────────────────────────────┘

Investment Thesis
─────────────────────────────
...

Bull Case
─────────────────────────────
...

Bear Case
─────────────────────────────
...

Sources
─────────────────────────────
...
Enter fullscreen mode Exit fullscreen mode

This is much more flexible than trying to parse arbitrary AI-generated text on the frontend.


Tech Stack

The project currently uses:

Frontend

  • Next.js 16
  • React 19
  • TypeScript
  • Tailwind CSS v4

Backend

  • Python
  • FastAPI
  • Uvicorn

AI

  • Gemini 2.5 Flash Lite
  • LangChain

Data & Research

  • Financial Modeling Prep
  • Tavily Search

The project also uses Inter for typography and a video background for the landing page.


Project Structure

The repository is organized roughly like this:

Stock-Market-Analyser/
│
├── app/
│   ├── main.py
│   ├── requirements.txt
│   └── pyproject.toml
│
└── stockany/
    ├── app/
    │   ├── page.tsx
    │   ├── layout.tsx
    │   └── globals.css
    │
    ├── public/
    │   └── *.mp4
    │
    ├── next.config.ts
    └── package.json
Enter fullscreen mode Exit fullscreen mode

The FastAPI backend contains the API routes, LangChain tooling and DCF logic, while the Next.js application handles the user interface and results page.


Running It Locally

You'll need API keys for:

  • Financial Modeling Prep
  • Google AI Studio / Gemini
  • Tavily

Create:

app/.env
Enter fullscreen mode Exit fullscreen mode

and add:

FMP_API_KEY=your_fmp_key_here
GOOGLE_API_KEY=your_gemini_key_here
TAVILY_API_KEY=your_tavily_key_here
Enter fullscreen mode Exit fullscreen mode

Then start the backend:

cd app

pip install -r requirements.txt

uvicorn main:app --reload
Enter fullscreen mode Exit fullscreen mode

The FastAPI server will run on:

http://localhost:8000
Enter fullscreen mode Exit fullscreen mode

Then start the frontend:

cd stockany

npm install
npm run dev
Enter fullscreen mode Exit fullscreen mode

and open:

http://localhost:3000
Enter fullscreen mode Exit fullscreen mode

The complete setup instructions are available in the repository.


What I Learned

The biggest lesson from this project was that AI applications don't necessarily need to let the LLM do everything.

A much better approach is often:

Traditional software
        +
APIs
        +
Deterministic calculations
        +
LLM reasoning
        +
Web research
Enter fullscreen mode Exit fullscreen mode

The DCF calculation is deterministic.

Financial data comes from an API.

Web research comes from a search system.

The LLM sits on top of those components and turns the information into something humans can understand.

That architecture makes the application considerably more grounded than simply prompting an LLM for a stock prediction.


What's Next?

There are several things I'd like to improve:

Historical valuation

Instead of only looking at the current valuation, I'd like to compare the stock against its historical multiples.

More valuation models

Adding:

  • P/E valuation
  • EV/EBITDA
  • Price-to-Free-Cash-Flow
  • Comparable company analysis

would make the valuation more robust.

Better AI reasoning

The next step would be giving the model access to more structured financial statements and letting it explicitly explain which assumptions drive the valuation.

Portfolio analysis

Eventually, I'd like to allow users to enter multiple tickers and compare them side-by-side.


Final Thoughts

StockAny AI started as an experiment in combining financial analysis with agentic AI, but it ended up teaching me something more important about building AI products.

The interesting part isn't just calling an LLM API.

It's designing a system where the LLM has access to the right tools, the right data, and the right constraints.

For this project, that meant combining:

FMP → financial data

DCF → valuation

Tavily → recent information

Gemini → reasoning and synthesis

FastAPI → backend orchestration

Next.js → user experience

The result is a small but complete example of how traditional software and generative AI can work together.

If you want to check out the implementation:

👉 GitHub: https://github.com/iPrq/Stock-Market-Analyser
👉 Youtube: https://youtu.be/XbrxvaP-FYk

I'd love to hear what you'd add to the project next.


Disclaimer: StockAny AI is an educational and informational project. Its outputs are not financial advice, and investment decisions should always involve independent research.

Top comments (0)