Skip to content

Setup

MemoryComponents

Bases: BaseModel


              flowchart TD
              src.tradingagents.graph.setup.MemoryComponents[MemoryComponents]

              

              click src.tradingagents.graph.setup.MemoryComponents href "" "src.tradingagents.graph.setup.MemoryComponents"
            

Groups all memory components for the trading agents.

bull

bull: FinancialSituationMemory = Field(
    ..., title="Bull Memory", description="Memory store for the bull researcher agent"
)

bear

bear: FinancialSituationMemory = Field(
    ..., title="Bear Memory", description="Memory store for the bear researcher agent"
)

trader

trader: FinancialSituationMemory = Field(
    ..., title="Trader Memory", description="Memory store for the trader agent"
)

invest_judge

invest_judge: FinancialSituationMemory = Field(
    ..., title="Investment Judge Memory", description="Memory store for the investment judge agent"
)

risk_manager

risk_manager: FinancialSituationMemory = Field(
    ..., title="Risk Manager Memory", description="Memory store for the risk manager agent"
)

GraphSetup

Bases: BaseModel


              flowchart TD
              src.tradingagents.graph.setup.GraphSetup[GraphSetup]

              

              click src.tradingagents.graph.setup.GraphSetup href "" "src.tradingagents.graph.setup.GraphSetup"
            

Handles the setup and configuration of the agent graph.

Methods:

Name Description
validate_selected_analysts

Validate analyst names and normalize duplicate selections.

setup_graph

Set up and compile the agent workflow graph.

quick_thinking_llm

quick_thinking_llm: SkipValidation[ChatModel] = Field(
    ...,
    title="Quick Thinking LLM",
    description="LLM instance used for analyst and researcher nodes",
)

deep_thinking_llm

deep_thinking_llm: SkipValidation[ChatModel] = Field(
    ...,
    title="Deep Thinking LLM",
    description="LLM instance used for manager and judge nodes requiring deeper reasoning",
)

tool_nodes

tool_nodes: dict[str, ToolNode] = Field(
    ...,
    title="Tool Nodes",
    description="Mapping of analyst type to its corresponding LangGraph ToolNode",
)

memories

memories: MemoryComponents = Field(
    ..., title="Memory Components", description="All agent memory stores grouped together"
)

conditional_logic

conditional_logic: ConditionalLogic = Field(
    default_factory=ConditionalLogic,
    title="Conditional Logic",
    description="Logic instance that determines graph edge routing",
)

validate_selected_analysts

validate_selected_analysts(selected_analysts: list[str]) -> list[str]

Validate analyst names and normalize duplicate selections.

Source code in src/tradingagents/graph/setup.py
@staticmethod
def validate_selected_analysts(selected_analysts: list[str]) -> list[str]:
    """Validate analyst names and normalize duplicate selections."""
    normalized = []
    for analyst in selected_analysts:
        value = analyst.strip().lower()
        if value and value not in normalized:
            normalized.append(value)

    unknown = [analyst for analyst in normalized if analyst not in SUPPORTED_ANALYSTS]
    if unknown:
        raise ValueError(
            "Unknown analyst(s): "
            f"{', '.join(unknown)}. Supported analysts: {', '.join(SUPPORTED_ANALYSTS)}."
        )
    if not normalized:
        raise ValueError("Trading Agents Graph Setup Error: no analysts selected!")
    return normalized

setup_graph

setup_graph(selected_analysts: list[str] | None = None) -> CompiledStateGraph

Set up and compile the agent workflow graph.

Parameters:

Name Type Description Default

selected_analysts

list[str] | None

Analyst types to include. Defaults to all supported analysts when None. Options are: - "market": Market analyst - "social": Social media analyst - "news": News analyst - "fundamentals": Fundamentals analyst

None

Returns:

Name Type Description
CompiledStateGraph CompiledStateGraph

The compiled workflow graph.

Raises:

Type Description
ValueError

If no analysts are selected.

Source code in src/tradingagents/graph/setup.py
def setup_graph(self, selected_analysts: list[str] | None = None) -> CompiledStateGraph:
    """Set up and compile the agent workflow graph.

    Args:
        selected_analysts (list[str] | None, optional): Analyst types to
            include. Defaults to all supported analysts when None. Options are:
            - "market": Market analyst
            - "social": Social media analyst
            - "news": News analyst
            - "fundamentals": Fundamentals analyst

    Returns:
        CompiledStateGraph: The compiled workflow graph.

    Raises:
        ValueError: If no analysts are selected.
    """
    if selected_analysts is None:
        selected_analysts = list(SUPPORTED_ANALYSTS)
    selected_analysts = self.validate_selected_analysts(selected_analysts)

    analyst_nodes, delete_nodes, tool_nodes = self._build_analyst_nodes(selected_analysts)

    # Create researcher and manager nodes
    bull_researcher_node = create_bull_researcher(self.quick_thinking_llm, self.memories.bull)
    bear_researcher_node = create_bear_researcher(self.quick_thinking_llm, self.memories.bear)
    research_manager_node = create_research_manager(
        self.deep_thinking_llm, self.memories.invest_judge
    )
    trader_node = create_trader(self.quick_thinking_llm, self.memories.trader)

    # Create risk analysis nodes
    aggressive_analyst = create_aggressive_debator(self.quick_thinking_llm)
    neutral_analyst = create_neutral_debator(self.quick_thinking_llm)
    conservative_analyst = create_conservative_debator(self.quick_thinking_llm)
    risk_manager_node = create_risk_manager(self.deep_thinking_llm, self.memories.risk_manager)

    # Create workflow
    workflow = StateGraph(AgentState)

    # Add analyst nodes to the graph
    for analyst_type, node in analyst_nodes.items():
        workflow.add_node(f"{analyst_type.capitalize()} Analyst", node)
        workflow.add_node(f"Msg Clear {analyst_type.capitalize()}", delete_nodes[analyst_type])
        workflow.add_node(f"tools_{analyst_type}", tool_nodes[analyst_type])

    # Add other nodes
    workflow.add_node("Bull Researcher", bull_researcher_node)
    workflow.add_node("Bear Researcher", bear_researcher_node)
    workflow.add_node("Research Manager", research_manager_node)
    workflow.add_node("Trader", trader_node)
    workflow.add_node("Aggressive Analyst", aggressive_analyst)
    workflow.add_node("Neutral Analyst", neutral_analyst)
    workflow.add_node("Conservative Analyst", conservative_analyst)
    workflow.add_node("Risk Judge", risk_manager_node)

    # Define edges - start with the first analyst
    first_analyst = selected_analysts[0]
    workflow.add_edge(START, f"{first_analyst.capitalize()} Analyst")

    # Connect analysts in sequence
    self._add_analyst_edges(workflow, selected_analysts)

    # Add research team edges
    workflow.add_conditional_edges(
        "Bull Researcher",
        self.conditional_logic.should_continue_debate,
        {"Bear Researcher": "Bear Researcher", "Research Manager": "Research Manager"},
    )
    workflow.add_conditional_edges(
        "Bear Researcher",
        self.conditional_logic.should_continue_debate,
        {"Bull Researcher": "Bull Researcher", "Research Manager": "Research Manager"},
    )
    workflow.add_edge("Research Manager", "Trader")
    workflow.add_edge("Trader", "Aggressive Analyst")

    # Add risk management edges
    workflow.add_conditional_edges(
        "Aggressive Analyst",
        self.conditional_logic.should_continue_risk_analysis,
        {"Conservative Analyst": "Conservative Analyst", "Risk Judge": "Risk Judge"},
    )
    workflow.add_conditional_edges(
        "Conservative Analyst",
        self.conditional_logic.should_continue_risk_analysis,
        {"Neutral Analyst": "Neutral Analyst", "Risk Judge": "Risk Judge"},
    )
    workflow.add_conditional_edges(
        "Neutral Analyst",
        self.conditional_logic.should_continue_risk_analysis,
        {"Aggressive Analyst": "Aggressive Analyst", "Risk Judge": "Risk Judge"},
    )

    workflow.add_edge("Risk Judge", END)

    # Compile and return
    return workflow.compile()