Skip to content

Commit 03dc8f7

Browse files
authored
Adds example for financial agent (#255)
This example shows how you might compose a richer financial research agent using the Agents SDK. The pattern is similar to the `research_bot` example, but with more specialized sub‑agents and a verification step. The flow is: 1. **Planning**: A planner agent turns the end user’s request into a list of search terms relevant to financial analysis – recent news, earnings calls, corporate filings, industry commentary, etc. 2. **Search**: A search agent uses the built‑in `WebSearchTool` to retrieve terse summaries for each search term. (You could also add `FileSearchTool` if you have indexed PDFs or 10‑Ks.) 3. **Sub‑analysts**: Additional agents (e.g. a fundamentals analyst and a risk analyst) are exposed as tools so the writer can call them inline and incorporate their outputs. 4. **Writing**: A writer agent brings together the search snippets and any sub‑analyst summaries into a long‑form markdown report plus a short executive summary. 5. **Verification**: A final verifier agent audits the report for obvious inconsistencies or missing sourcing.
2 parents cef3d53 + 0dec571 commit 03dc8f7

File tree

12 files changed

+399
-0
lines changed

12 files changed

+399
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Financial Research Agent Example
2+
3+
This example shows how you might compose a richer financial research agent using the Agents SDK. The pattern is similar to the `research_bot` example, but with more specialized sub‑agents and a verification step.
4+
5+
The flow is:
6+
7+
1. **Planning**: A planner agent turns the end user’s request into a list of search terms relevant to financial analysis – recent news, earnings calls, corporate filings, industry commentary, etc.
8+
2. **Search**: A search agent uses the built‑in `WebSearchTool` to retrieve terse summaries for each search term. (You could also add `FileSearchTool` if you have indexed PDFs or 10‑Ks.)
9+
3. **Sub‑analysts**: Additional agents (e.g. a fundamentals analyst and a risk analyst) are exposed as tools so the writer can call them inline and incorporate their outputs.
10+
4. **Writing**: A senior writer agent brings together the search snippets and any sub‑analyst summaries into a long‑form markdown report plus a short executive summary.
11+
5. **Verification**: A final verifier agent audits the report for obvious inconsistencies or missing sourcing.
12+
13+
You can run the example with:
14+
15+
```bash
16+
python -m examples.financial_research_agent.main
17+
```
18+
19+
and enter a query like:
20+
21+
```
22+
Write up an analysis of Apple Inc.'s most recent quarter.
23+
```
24+
25+
### Starter prompt
26+
27+
The writer agent is seeded with instructions similar to:
28+
29+
```
30+
You are a senior financial analyst. You will be provided with the original query
31+
and a set of raw search summaries. Your job is to synthesize these into a
32+
long‑form markdown report (at least several paragraphs) with a short executive
33+
summary. You also have access to tools like `fundamentals_analysis` and
34+
`risk_analysis` to get short specialist write‑ups if you want to incorporate them.
35+
Add a few follow‑up questions for further research.
36+
```
37+
38+
You can tweak these prompts and sub‑agents to suit your own data sources and preferred report structure.

examples/financial_research_agent/__init__.py

Whitespace-only changes.

examples/financial_research_agent/agents/__init__.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from pydantic import BaseModel
2+
3+
from agents import Agent
4+
5+
# A sub‑agent focused on analyzing a company's fundamentals.
6+
FINANCIALS_PROMPT = (
7+
"You are a financial analyst focused on company fundamentals such as revenue, "
8+
"profit, margins and growth trajectory. Given a collection of web (and optional file) "
9+
"search results about a company, write a concise analysis of its recent financial "
10+
"performance. Pull out key metrics or quotes. Keep it under 2 paragraphs."
11+
)
12+
13+
14+
class AnalysisSummary(BaseModel):
15+
summary: str
16+
"""Short text summary for this aspect of the analysis."""
17+
18+
19+
financials_agent = Agent(
20+
name="FundamentalsAnalystAgent",
21+
instructions=FINANCIALS_PROMPT,
22+
output_type=AnalysisSummary,
23+
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from pydantic import BaseModel
2+
3+
from agents import Agent
4+
5+
# Generate a plan of searches to ground the financial analysis.
6+
# For a given financial question or company, we want to search for
7+
# recent news, official filings, analyst commentary, and other
8+
# relevant background.
9+
PROMPT = (
10+
"You are a financial research planner. Given a request for financial analysis, "
11+
"produce a set of web searches to gather the context needed. Aim for recent "
12+
"headlines, earnings calls or 10‑K snippets, analyst commentary, and industry background. "
13+
"Output between 5 and 15 search terms to query for."
14+
)
15+
16+
17+
class FinancialSearchItem(BaseModel):
18+
reason: str
19+
"""Your reasoning for why this search is relevant."""
20+
21+
query: str
22+
"""The search term to feed into a web (or file) search."""
23+
24+
25+
class FinancialSearchPlan(BaseModel):
26+
searches: list[FinancialSearchItem]
27+
"""A list of searches to perform."""
28+
29+
30+
planner_agent = Agent(
31+
name="FinancialPlannerAgent",
32+
instructions=PROMPT,
33+
model="o3-mini",
34+
output_type=FinancialSearchPlan,
35+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from pydantic import BaseModel
2+
3+
from agents import Agent
4+
5+
# A sub‑agent specializing in identifying risk factors or concerns.
6+
RISK_PROMPT = (
7+
"You are a risk analyst looking for potential red flags in a company's outlook. "
8+
"Given background research, produce a short analysis of risks such as competitive threats, "
9+
"regulatory issues, supply chain problems, or slowing growth. Keep it under 2 paragraphs."
10+
)
11+
12+
13+
class AnalysisSummary(BaseModel):
14+
summary: str
15+
"""Short text summary for this aspect of the analysis."""
16+
17+
18+
risk_agent = Agent(
19+
name="RiskAnalystAgent",
20+
instructions=RISK_PROMPT,
21+
output_type=AnalysisSummary,
22+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from agents import Agent, WebSearchTool
2+
from agents.model_settings import ModelSettings
3+
4+
# Given a search term, use web search to pull back a brief summary.
5+
# Summaries should be concise but capture the main financial points.
6+
INSTRUCTIONS = (
7+
"You are a research assistant specializing in financial topics. "
8+
"Given a search term, use web search to retrieve up‑to‑date context and "
9+
"produce a short summary of at most 300 words. Focus on key numbers, events, "
10+
"or quotes that will be useful to a financial analyst."
11+
)
12+
13+
search_agent = Agent(
14+
name="FinancialSearchAgent",
15+
instructions=INSTRUCTIONS,
16+
tools=[WebSearchTool()],
17+
model_settings=ModelSettings(tool_choice="required"),
18+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from pydantic import BaseModel
2+
3+
from agents import Agent
4+
5+
# Agent to sanity‑check a synthesized report for consistency and recall.
6+
# This can be used to flag potential gaps or obvious mistakes.
7+
VERIFIER_PROMPT = (
8+
"You are a meticulous auditor. You have been handed a financial analysis report. "
9+
"Your job is to verify the report is internally consistent, clearly sourced, and makes "
10+
"no unsupported claims. Point out any issues or uncertainties."
11+
)
12+
13+
14+
class VerificationResult(BaseModel):
15+
verified: bool
16+
"""Whether the report seems coherent and plausible."""
17+
18+
issues: str
19+
"""If not verified, describe the main issues or concerns."""
20+
21+
22+
verifier_agent = Agent(
23+
name="VerificationAgent",
24+
instructions=VERIFIER_PROMPT,
25+
model="gpt-4o",
26+
output_type=VerificationResult,
27+
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from pydantic import BaseModel
2+
3+
from agents import Agent
4+
5+
# Writer agent brings together the raw search results and optionally calls out
6+
# to sub‑analyst tools for specialized commentary, then returns a cohesive markdown report.
7+
WRITER_PROMPT = (
8+
"You are a senior financial analyst. You will be provided with the original query and "
9+
"a set of raw search summaries. Your task is to synthesize these into a long‑form markdown "
10+
"report (at least several paragraphs) including a short executive summary and follow‑up "
11+
"questions. If needed, you can call the available analysis tools (e.g. fundamentals_analysis, "
12+
"risk_analysis) to get short specialist write‑ups to incorporate."
13+
)
14+
15+
16+
class FinancialReportData(BaseModel):
17+
short_summary: str
18+
"""A short 2‑3 sentence executive summary."""
19+
20+
markdown_report: str
21+
"""The full markdown report."""
22+
23+
follow_up_questions: list[str]
24+
"""Suggested follow‑up questions for further research."""
25+
26+
27+
# Note: We will attach handoffs to specialist analyst agents at runtime in the manager.
28+
# This shows how an agent can use handoffs to delegate to specialized subagents.
29+
writer_agent = Agent(
30+
name="FinancialWriterAgent",
31+
instructions=WRITER_PROMPT,
32+
model="gpt-4.5-preview-2025-02-27",
33+
output_type=FinancialReportData,
34+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import asyncio
2+
3+
from .manager import FinancialResearchManager
4+
5+
6+
# Entrypoint for the financial bot example.
7+
# Run this as `python -m examples.financial_bot.main` and enter a
8+
# financial research query, for example:
9+
# "Write up an analysis of Apple Inc.'s most recent quarter."
10+
async def main() -> None:
11+
query = input("Enter a financial research query: ")
12+
mgr = FinancialResearchManager()
13+
await mgr.run(query)
14+
15+
16+
if __name__ == "__main__":
17+
asyncio.run(main())

0 commit comments

Comments
 (0)