Skip to content

Hedge fund multi agent system: Build a meta-tool calling multi-agentic system using Strands SDK #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
WEAVE_API_KEY=
TAVILY_API_KEY=
FINANCIAL_DATASET_API=
# initialize the required langsmith variables
LANGCHAIN_TRACING_V2="true"
LANGCHAIN_API_KEY=""
LANGCHAIN_PROJECT=""
19 changes: 19 additions & 0 deletions 02-samples/05-hedge-fund-analyst-multi-agent-system/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
214 changes: 214 additions & 0 deletions 02-samples/05-hedge-fund-analyst-multi-agent-system/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
# Build a meta-tool calling multi-agentic system using Strands SDK

This comprehensive example demonstrates a sophisticated Multi-Agent Collaboration (MAC) system designed for hedge fund analysis using the Strands Agents SDK. The system implements a hierarchical architecture with specialized financial analysis agents that work together to provide comprehensive investment insights.

View the architecture diagram below that shows how you can build a mutli-agentic system using an **[agent-as-tools](https://strandsagents.com/0.1.x/user-guide/concepts/multi-agent/agents-as-tools/)** pattern, with **meta-tooling**, **human-in-the-loop**, observability, detailed traces and logging enabled for a financial system:

🚨**THIS AGENTIC SYSTEM USES **[CLAUDE 4 SONNET](https://www.anthropic.com/news/claude-4), released on 05/22**

![architecture](img/arch.png)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make it architecture.png. Trying to be consistent for all samples


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

### Lead Analyst Agent (Uses Claude 4 Sonnet under the hood)

1. **Role**: Meta-coordinator that uses specialized agents as tools
2. **Capabilities**: Synthesizes insights from all sub-agents
3. **Features**: Dynamic tool creation for custom analysis requirements

### Specialized Sub-Agents

1. **Fundamental Analyst Agent**
- **Model**: Claude 3 Haiku
- **Purpose**: Performs fundamental financial statement analysis
- **Capabilities**:
1. Income statement analysis
2. Balance sheet examination
3. Cash flow statement evaluation
4. Financial ratio calculations
5. Valuation metrics assessment



2. **Technical Analyst Agent**
- **Model**: Amazon Nova Lite
- **Purpose**: Conducts technical analysis of stock price movements
- **Capabilities**:
1. Historical price data retrieval
2. Technical indicator calculations (`RSI`, `MACD`, `SMA`, `EMA`)
3. Chart pattern analysis
4. Trading signal generation



3. **Market Analyst Agent**
- **Model**: Claude 3 Haiku
- **Purpose**: Provides market context and sentiment analysis
- **Capabilities**:
1. Options chain analysis
2. Insider trading information
3. Market news aggregation
4. Sentiment analysis

## Key Features

1. **Financial Data Integration**:

The system integrates with comprehensive financial data APIs to access:
- Real-time and historical stock prices
- Financial statements (income, balance sheet, cash flow)
- Options chain data
- Insider trading transactions
- Market news and sentiment data

2. **Multi-Model Support for multiple agents as tools and code generation**

Utilizes multiple foundation models through AWS Bedrock:

- ***Amazon Nova Lite*** for routing and technical analysis
- ***Claude 3 Haiku*** for fundamental and market analysis
- ***Configurable model parameters for each agent***
- This **repository uses Claude 4 Sonnet** as the underlying model for the lead analyst agent. This agent makes decision for which agent to be used as a tool, if meta tooling is required, etc.

### Advanced Tool Ecosystem
There are custom tools that are used across different agents in this ecosystem:

1. `get_income_statements`: Retrieves company income statement data
2. `get_balance_sheets`: Fetches balance sheet information
3. `get_cash_flow_statements`: Obtains cash flow data
4. `get_stock_prices`: Historical price data with date range filtering
5. `get_current_stock_price`: Real-time price information
6. `get_technical_indicators`: Calculates RSI, MACD, SMA, EMA indicators
7. `get_options_chain`: Options contract data with filtering capabilities
8. `get_insider_trades`: Insider trading transaction information
9. `get_news`: Financial news and market sentiment data

### Code Generation and Execution

1. `generate_and_execute_code`: Dynamic Python code generation and safe execution
- Uses LiteLLM with AWS Bedrock for code generation
- Implements security measures and timeout controls
- Supports data visualization and custom analysis

### Pre-built Utility Tools

This Agentic system uses pre-built tools offered by Strands agent tools along with the custom tools created. The pre-built tools that are used are as follows across the four different agents:

1. `calculator`: Mathematical operations
2. `file_read`: File parsing capabilities
3. `mem0_memory`: Memory management for context retention
4. `python_repl`: Python code execution environment
5. `current_time`: Date and time utilities
6. `journal`: Structured task and log management

### Human-in-the-Loop Workflow
Implements human approval mechanisms for sensitive operations. If there is a tool that is getting sensitive news for example, the tool execution will halt and ask the human for approval for calling the tool. There are several design patterns for adding a human in the loop, but this is one of them being ***reviewing tool calls***. You can also use the `HITL` workflow to edit the tool call context, ask for approval and more.

1. Insider trading data requests require explicit approval
2. News data access with human oversight
3. Interactive approval prompts in Jupyter notebook environment

### Meta-Tooling Capabilities

The Lead Analyst Agent features advanced meta-tooling abilities:

1. **Dynamic Tool Creation**: Generates custom tools on-demand
2. **Tool Loading**: Integrates newly created tools into the system
3. **Shell Access**: Command execution for testing and validation
4. **Editor Integration**: Code modification and tool development

## Getting Started

Follow the steps below to get started with running this in your environment:

### Environment Configuration

1. Make sure you are in your AWS environment. To set up your AWS environment, view the steps [here](https://aws.amazon.com/getting-started/guides/setup-environment/). Create `.env` file with required API keys and credentials. Copy the `.env.template` to `.env` and edit it:

```bash
# Copy the template into your .env file and edit the content below
cp .env.template .env
```

```bash
TAVILY_API_KEY=<your-tavily-api-key>
FINANCIAL_DATASET_API=<your-financial-dataset-api-key>
```

To get your API key for Tavily, view [here](https://tavily.com/). To get your API key for `financialdatasets.ai`, view [here](https://www.financialdatasets.ai/).

2. Install `uv` and Python dependencies:

```bash
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not tun on windows. Can we link to uv getting started guide instead.

curl -LsSf https://astral.sh/uv/install.sh | sh
export PATH="$HOME/.local/bin:$PATH"
```

3. Restore Python virtual environment from the `pyproject.toml` file.

```bash
uv venv && source .venv/bin/activate && uv pip sync pyproject.toml
```

4. Create a `conda` kernel. This is needed to run the notebook on your EC2 instance:

```bash
uv add zmq
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we add zmq separately and not part of pyproject.toml?

python -m ipykernel install --user --name=.venv --display-name="Python (uv env)"
```

5. Open the JupyterLab notebook to run and select the `Python (uv env)` if not selected automatically (if it is not, refresh the page or close the notebook and open it again and you should see it selected or be able to select it from the drop down).

## Configurations

All information to run this sample is fetched from a configuration file. This configuration file contains information about the model id information, inference parameters and other information for each agent in this multi agent system. View the file [here](config.yaml).

```yaml
general:
name: "Financial-multi-agentic-system"
description: "This agentic system shows multi-agent collaboration different financial agents"

# This contains the model information for various agents
# in our multi agentic system
model_information:
finance_agent_model_info:
model_id: us.amazon.nova-micro-v1:0
inference_parameters:
temperature: 0.1
technical_agent_model_info:
model_id: us.anthropic.claude-3-5-sonnet-20240620-v1:0
inference_parameters:
temperature: 0.1
max_tokens: 512
.
.
.
```

## Steps to run the solution

Next, to run the solution, open the [`multi-agentic-system-strands.ipynb`](multi-agentic-system-strands.ipynb), select the `Python (uv env)` environment and run all the cells to see the outputs.

## Results

View some of the results below:

1. **Human in the loop example**: View how the tool execution pauses and asks for the human in the loop input before resuming:

![hitl](img/hitl.png)

2. **On the fly research journal creation**: One of the agents has the capability to use built in tools and custom tools, and write a journal on the fly. View an example off a Journal created here:

![journal](img/journal.png)

## Observability

View the traces in `Langfuse` dashboard below:

![langfuse](img/trace1.png)

![langfuse](img/trace2.png)






51 changes: 51 additions & 0 deletions 02-samples/05-hedge-fund-analyst-multi-agent-system/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
general:
name: "Financial-multi-agentic-system"
description: "This agentic system shows multi-agent collaboration different financial agents"

# This contains the model information for various agents
# in our multi agentic system
model_information:
# Represents information about the finance agent
finance_agent_model_info:
# Represents the model id that the agent will use within
# the strands agents SDK
# The finance agent is not a complicated agent but calls a tool that requires
# code generation. We will power the agent with a small model (Nova Micro), but
# will use a larger, more comprehensive model for code generation (view the code gen
# model info section below)
model_id: us.amazon.nova-micro-v1:0
# Represents the inference parameters that the foundation model
# uses at runtime during inference
inference_parameters:
temperature: 0.1
technical_agent_model_info:
# Represents the model id that the agent will use within
# the strands agents SDK
model_id: us.anthropic.claude-3-5-sonnet-20240620-v1:0
# Represents the inference parameters that the foundation model
# uses at runtime during inference
inference_parameters:
temperature: 0.1
max_tokens: 512
market_analysis_agent:
# Represents the model id that the agent will use within
# the strands agents SDK
model_id: us.anthropic.claude-3-5-sonnet-20240620-v1:0
# Represents the inference parameters that the foundation model
# uses at runtime during inference
inference_parameters:
temperature: 0.1
max_tokens: 512

# This is the code generation model that is used within the tool
# to generate code, note that this might be different from the actual
# large language model that powers the agent
code_gen_model_info:
# Represents the model id that the agent will use within
# the strands agents SDK
model_id: us.anthropic.claude-3-5-sonnet-20240620-v1:0
# Represents the inference parameters that the foundation model
# uses at runtime during inference
inference_parameters:
temperature: 0.1
max_tokens: 2048
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent architecture, I am open to suggestions on how to make it easily readable.

At the moment it is very difficult to read on github readme.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this here? We might need an approval from legal to publish on opensource because this was generated by an LLM

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed, will delete it if this requires additional approval.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

## 15:48:08
## Apple Inc. (AAPL) Financial Analysis - May 23, 2025

Today I conducted a comprehensive financial analysis of Apple Inc. (AAPL). The company continues to demonstrate exceptional financial performance with FY2023 revenue of approximately $383 billion, a net margin of ~25%, and free cash flow of around $110 billion.

Key observations:
1. Services business is growing faster than hardware, improving overall margins
2. Cash position remains extremely strong (~$160B) despite massive shareholder returns
3. Gross margin has improved to ~43%, partly due to services mix shift
4. Share repurchases continue to enhance EPS growth beyond net income growth
5. Balance sheet remains strong with manageable debt levels

Investment considerations:
- Apple remains well-positioned for continued growth through its integrated ecosystem
- Services growth trajectory is critical to watch for margin expansion
- Regulatory risks around App Store practices could impact services revenue
- New product categories may provide additional growth vectors
- Valuation premium requires sustained execution

Actionable insights:
1. Monitor services growth rate in quarterly reports as key indicator
2. Watch for progress on supply chain diversification beyond China
3. Evaluate capital allocation decisions between buybacks and potential acquisitions
4. Track regulatory developments in key markets regarding App Store policies
5. Assess potential impact of AI integration across product lineup

Overall, Apple continues to demonstrate financial strength that supports its premium valuation, though growth sustainability at its current size remains a key question for long-term investors.

## 15:48:19 - Task
- [ ] Monitor Apple's next quarterly report for services segment growth rate

## 15:48:45 - Task
- [ ] Research potential impact of global regulatory developments on Apple's App Store revenue model

## 15:49:13 - Task
- [ ] Analyze Apple's capital allocation decisions in comparison to tech peers
Loading