
Build your first advanced AI equity research agent
AI can already analyze stock price data, but what about building research reports for comparable analysis?
You may have heard of AI agents and agentic workflows—these are structured steps designed to achieve a specific goal. Each step is connected through specialized prompts that guide the AI’s reasoning and decision-making.
2025 is the year of agents.
In today’s newsletter, you’ll get Python code to build that builds an agent to extract financial reports from Tesla and Ford and recommends which stock to buy.
There’s a lot of code in today’s newsletter. Just copy and paste it, get everything running, then go line by line to learn how it works.
Let’s go!
Build your first advanced AI equity research agent
There’s some set up you’ll need to take care of.
First, head over to LlamaCloud and get your self a free account. You’ll need your organization ID, a project ID, and your API key.
Once done, create a .env file in the directory with this notebook. Add the following to it:
Next, create a folder called data. Download the following two files:
https://digitalassets.tesla.com/tesla-contents/image/upload/IR/TSLA-Q2-2024-Update.pdf
Name them tesla_q2_earnings.pdf and ford_q2_earnings_press_release.pdf, respectively, and put them in the data folder.
Finally, create a file called modeling_assumptions.txt in the data directory and add the following to it:
Ok, you’re all set.
Let’s get to the code!
Imports and set up
Import the LlamaIndex libraries we’ll need for the research agent.
These libraries provide tools for data modeling, API interactions, workflow management, and natural language processing. We use them to create a structured workflow for financial analysis and report generation.
Define the data models
We create data models to structure our financial information and analysis outputs. Pydantic is great for making sure the output of agents conforms to what we expect.
We define several data models using Pydantic. These models help structure our financial data and analysis outputs.
They include classes for raw financials, initial financial data output, financial model output, comparative analysis, and the final equity research memo. Each model specifies the expected fields and their descriptions.
Set up the LlamaExtract agent
Make sure you have your organization ID and project ID from LlamaCloud.
We set up the LlamaExtract agent for our automotive sector analysis. This involves initializing the agent with specific project and organization IDs, handling any existing agents, and creating a new agent with a balanced extraction mode.
The agent is configured to use our InitialFinancialDataOutput schema for data extraction.
Define workflow events and classes
This is where the magic happens. This code sets up the events and workflow that runs the agents.
We define custom events and a workflow class for our automotive sector analysis. The workflow includes steps for parsing financial decks, generating financial models, and performing comparative analysis.
Each step is defined as an asynchronous method within the AutomotiveSectorAnalysisWorkflow class.
Now let’s run it!
You can visualize the agent workflow with this code. It will produce an HTML file that you can open in any browser.
The result looks something like this.
.png)
Now run the agent workflow. The workflow processes earnings reports for Tesla and Ford, generates financial models, and produces a comparative analysis. The final result is an equity research memo that provides insights and recommendations based on the analysis of both companies.
The result looks something like this.
.png)
And to get the final analysis.
And you end up with something like this.
.png)
Your next steps
There is a lot of code here. The good news is you can make slight modifications to run the analysis against different pairs of companies. Try downloading PDFs from NVIDIA and AMD, altering the code, and seeing what you get. If you run into any errors about modules not found, just pip install them.