Build a Multi-AI Agents System for Financial Analysis
A hands on tutorial on how to build a multi AI Agents from scratch using Phidata and Python
In this blog post, I’ll walk you through how I built a multi AI Agents application for financial analysis using Phidata and Python. Check out the video tutorial here.
What is a Multi-Agent AI System?
A multi AI Agents system is a technique where multiple AI agents, each with a specific role, work together to achieve a common goal. Think of this as a team of experts: one agent might gather data, another analyzes trends, and a third synthesizes insights. By dividing tasks among specialized agents, the system becomes faster, more accurate, and more efficient.
In this project, I created three agents that use different LLM models:
Web Agent: It uses the Open AI GPT-4o model and searches the web for real-time news and public sentiment using DuckDuckGo.
Finance Agent: It uses the Open AI GPT-4o model to analyse financial data, such as stock prices and analyst recommendations, using Yahoo Finance.
Grok Agent: It uses XAI (grok-beta) to combine web insights and financial data which provide a comprehensive analysis.
These agents are orchestrated by a AI Agents Team as demonstrated in the picture. The Agents team manage the collaboration among AI Agents and delivers a unified, actionable report.
How a Agent works in Phidata?
User Query: The user asks the question
Query Analysis: The agent interprets the query to formulate a search strategy
Web Search: When given a query, the agent uses its search tool (DuckDuckGo here) to find relevant information on the web.
Data Processing: The agent then processes the search results, summarizes the information, and provides a response to the user.
Dynamic Response: The response is typically formatted in markdown and can be streamed to the user for real-time feedback.
Database in Phidata
When using phidata, agent sessions will be stored in SQLite format in the agents.db file on the application side. Here's how this works:
SqlAgentStorage is used to store agent sessions in a SQLite database.
The database file is typically named agents.db or something similar.
It's used to maintain the persistence of agent interactions across sessions.
Why Use Multi-Agent Systems for Financial Analysis?
The financial industry is complex, with data coming from multiple sources—news, social media such as x.com, stock prices, and more. A single AI model might struggle to handle this complexity, but a multi-agent system performs well. Here’s why:
Specialization: Each agent focuses on what it does best which guarantees high-quality results.
Efficiency: Tasks are divided and processed in parallel to save time.
Scalability: You can easily add more agents to handle additional tasks or data sources.
For example, if you want to analyze the impact of a recent news event on a company’s stock price, the Web Agent can gather the latest news, the Finance Agent can analyze stock trends, and the Grok Agent can combine these insights into a clear, actionable summary.
How I Built the Multi AI Agents system
Here’s a high-level overview of the steps I followed:
Set Up the Agents:
Each agent was created using Phidata library and specialized tools like DuckDuckGo for web search and Yahoo Finance for financial data.
The agents were designed to output their findings in a structured JSON format for easy integration.
web_agent = Agent( name="Web Agent", role="Searching the web for real-timeabout companies, and public sentiment.", model=OpenAIChat(id="gpt-4o"), tools=[DuckDuckGo()], instructions=["Search for the latest news articles, social media trends, and public discussions related to the user's query.\n" + "Respond in JSON format only, no additional text" + "{['symbol': 'The company stock ticker','score': Impact estimation (-10 to +10) based on the company stock price.,'event_summary': 'A brief description of the event.'}]}"], storage=SqlAgentStorage(table_name="web_agent", db_file="agents.db"), add_history_to_messages=False, markdown=False, ) finance_agent = Agent( name="Finance Agent", role="Accessing and analyzing financial data and trends from structured finance databases.", model=OpenAIChat(id="gpt-4o"), tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True, company_news=True)], instructions=["Use financial tools (e.g., Yahoo Finance) to gather stock prices, company-specific news, and analyst recommendations.\n" + "Respond in JSON format only, no additional text:\n" + "{['symbol': 'The company stock ticker','score': Impact estimation (-10 to +10) based on the company stock price.,'event_summary': 'Include quantitative data relevant to the user query.'}]}"], storage=SqlAgentStorage(table_name="finance_agent", db_file="agents.db"), add_history_to_messages=False, markdown=False, ) grok_agent = Agent( name="Grok Agent", role="Combining web search and x.com data.", model=xAI(id="grok-beta"), tools=[DuckDuckGo()], instructions=["Use both web tools and x.com public tweets to gather a comprehensive view of trends, and sentiment.\n" + "Respond in JSON format only, no additional text:\n" + "{['symbol': 'The company stock ticker','score': Impact estimation (-10 to +10) based on the event's likely effect on the company stock price.,'event_summary': 'Combine news and data.'}]}"], show_tool_calls=False, markdown=False, )
Orchestrate the Team:
The Team Agent coordinates the tasks of the Web Agent, Finance Agent, and Grok Agent.
It merges their outputs, removes duplicates, and generates a final report.
agent_team = Agent( team=[web_agent, finance_agent, grok_agent], name="Agents Team", role="Orchestrating tasks across specialized agents to produce the most accurate and comprehensive responses.", instructions=["Delegate tasks based on prompt requirements:\n" + "Web Agent: For real-time news and sentiment analysis.\n" + "Finance Agent: For financial data and analytics.\n" + "Grok Agent: For tunring insights into a well-structured response.\n" + "Respond in JSON format only, no additional text:\n" + "{\n" + ' "symbol": "TICKER",\n' + ' "score": NUMBER_BETWEEN_NEGATIVE_10_AND_10,\n' + ' "event_summary": "SUMMARY",\n' + ' "analysis": {\n' + ' "web_insights": "WEB_FINDINGS",\n' + ' "financial_data": "FINANCIAL_METRICS",\n' + ' "combined_web_analysis": "SYNTHESIS"\n' + " }\n" + "}\n" + "Deduplicate or reconcile overlapping findings.\n" + "Provide a clear, actionable summary for the user."], show_tool_calls=False, markdown=False, )
Create a User Interface:
Using Streamlit, I built a simple interface where users can input queries and select which agent or team to use.
# Streamlit App st.title("AI Agent Finance Team") st.write("Interact with your AI agents to get financial data and web search results.") # User input user_query = st.text_input("Enter your query:") # Dropdown to select the agent agent_selection = st.selectbox( "Select an Agent", options=["Web Agent", "Finance Agent", "Grok Agent", "Agents Team"], ) # Agent map agent_map = { "Web Agent": aiAgentFinanceTeam.web_agent, "Finance Agent": aiAgentFinanceTeam.finance_agent, "Grok Agent": aiAgentFinanceTeam.grok_agent, "Agents Team": aiAgentFinanceTeam.agent_team, } if st.button("Submit Query"): if user_query.strip(): selected_agent = agent_map[agent_selection] prompt = aiAgentFinanceTeam.generate_prompt(selected_agent.name, user_query) response = selected_agent.run(user_query) try: # Process response and send to Discord response_content = aiAgentFinanceTeam.process_and_send_response( response, selected_agent.name ) # Debug: Show extracted text st.write("Extracted text:") st.write(response_content) # Show Discord status st.success("Results sent to Discord successfully!") except Exception as e: st.error(f"Error processing response: {str(e)}") st.text("Raw response:") st.text(response) else: st.warning("Please enter a query before submitting.")
What did I learned?
Prompt Template is key
It is important to define the right prompt template for your usecases to be able to get the right outcome.
"Web Agent": """ Objective: Use web search to gather the most recent and relevant information on publicly traded companies based on the query. Required Format (JSON): [ {{ "symbol": "<COMPANY_TICKER>", "score": <IMPACT_SCORE>, "event_summary": "<EVENT_SUMMARY>" }} ]
Instructions is essential in Phidata
Instructions are important in Phidata as they are used alongside the "description" parameter to build the system prompt sent to the language model.
Define the Response Format
To receive the right output, you need to define the response format correctly, whether it is a JSON or text or html and so on.
{['symbol': 'The company stock ticker','score': 'Estimation betweebn (-10 to +10)'}
Why Multi AI Agents are the the next big thing?
Multi-agent AI systems aren’t just a technical fancy technique, they’re a game-changer for industries like finance, healthcare, and logistics. By automating complex tasks, they free up time for strategic decision-making and innovation.
Whether you’re a developer, data scientist, or finance professional, learning to build multi-agent systems can give you a competitive edge in the world of AI.
Don’t forget to subscribe to my newsletter for more tutorials, insights, and updates on AI and technology.