The goal: multi-agent debate with guardrails
You've built single-agent bots. Now you'll build a system where multiple agents with opposing viewpoints argue about a stock ticker. The debate is adversarial β each agent is forced to argue their side and cite real data β but orchestrated: a moderator synthesizes both sides and the system enforces that no agent makes claims without evidence.
Here's what it looks like when three agents debate HDFCBANK.NS:
DEBATE: HDFCBANK.NS ββββββββββββββββββββββββββββββββββ Turn 1 β Bull argues: "HDFC's NIM has expanded 12 bps in Q1, driven by rising deposit rates. This is bullish. Source: Q1 earnings call." Turn 1 β Bear counters: "But loan growth slowed to 8% YoY vs 12% last year. Rising rates hit retail borrowers. Source: Q1 briefing data." Turn 2 β Bull replies: "Strong liability franchise β CASA ratio improved to 42%. Sticky deposits fuel resilience. Source: balance sheet analysis." ... MODERATOR SUMMARY: Bull's strongest point: NIM expansion + deposit stability Bear's strongest point: Loan growth deceleration signals market slowdown Verdict: Mixed signals. Monitor next quarter's growth numbers.
This is multi-agent orchestration. Each agent reasoned separately, cited evidence, and the moderator made a call. Over the next 30 minutes, you'll build the full system.
Architecture: 3 agents + turn structure + guardrails
The system has three components: two debating agents, one moderator, plus a critic that enforces rules.
Arjun is building an investment research tool for retail traders in Hyderabad. Every morning, his bot debates 5 stocks (RELIANCE.NS, HDFCBANK.NS, TCS.NS, etc.) automatically. Each stock gets a 3-turn debate. Instead of reading conflicting newsletters, Arjun sees a structured debate where both sides cite data. He decides based on who won the argument.
Step 1: Define the debate turn with Pydantic
Each agent's argument is a structured "DebateTurn". It must include the claim, evidence from yfinance, and a citation.
Step 2: Fetch ticker data and recent metrics
Before agents debate, fetch real price data and calculate key metrics. Agents will cite these in their arguments.
Step 3: System prompts that enforce debate rules
Each agent gets a system prompt that forces them to argue their side AND cite a metric from the data. No hand-waving allowed.
Step 4: Orchestrator that runs 3 turns of debate
The orchestrator fetches data, calls each agent 3 times, collects turns, validates them with Pydantic, and passes to the moderator.
Step 5: Guardrailsβenforce debate rules with a critic
A critic agent checks each turn for valid citations and rules violations. If an agent tries to argue without evidence, the critic rejects it.
citation is missing or references a metric not in the data, parsing fails and we retry or skip. No naked claims get through.
Full worked example: HDFCBANK.NS debate
Here's what a real 3-round debate output looks like, with the moderator's synthesis:
DEBATE SESSION: HDFCBANK.NS Timestamp: 2026-04-16 ROUND 1: ββββββββββββββββββββββββββββββββββ Bull: "HDFC's net interest margin (NIM) expanded 12 bps in Q1 2026, beating consensus. This shows pricing power in a rising rate environment." Evidence: NIM up 12 bps YoY Citation: Q1 earnings release Bear: "But loan growth slowed to 8% YoY, down from 12% last year. In a declining rate cycle, this slowdown signals demand weakness." Evidence: Loan growth 8% vs 12% prior year Citation: Management guidance ROUND 2: ββββββββββββββββββββββββββββββββββ Bull: "CASA ratio improved to 42%, meaning deposits are sticky. In rising rate regimes, stable deposits are goldβthey fund growth without chasing rate-sensitive corporates." Evidence: CASA 42% (up from 39% last year) Citation: Balance sheet analysis Bear: "Deposit betas are rising. RBI's tightening cycle is now over. When rates peak and reverse, HDFC's NIM will compress. All the expansion we saw this quarter will reverse." Evidence: Volatility 14.5% indicates uncertainty Citation: Market regime shift (RBI guidance) ROUND 3: ββββββββββββββββββββββββββββββββββ Bull: "Stock is trading near 52-week lows (βΉ1,650 vs βΉ1,900 high). Valuations have compressed. At 1.8x book, the bank is cheap vs 2.1x historical average." Evidence: P/E near 10-year low Citation: Market data Bear: "Cheap for a reason. Credit growth stalling, NIM expansion was one-time, and the bank's exposure to real estate is risingβrate cuts will hurt demand. Wait for better entry." Evidence: Volatility 14.5%, recent price decline -8% month Citation: Sector analysis MODERATOR SYNTHESIS: ββββββββββββββββββββββββββββββββββ Bull's strongest point: "NIM expansion + CASA stability show pricing power and resilience in the current rate environment." Bear's strongest point: "Loan growth deceleration suggests the broad economy is slowing, limiting upside regardless of NIM." Key disagreement: Whether NIM expansion is sustainable through a rate cycle reversal. Neutral takeaway: HDFC has near-term strength (margins, deposits) but medium-term headwinds (loan growth). Buy for value, but monitor credit growth closely. Fair price: βΉ1,750ββΉ1,900 depending on Q2 guidance.
Stretch: add a skeptic agent
Some teams add a third agentβa "Skeptic" who pokes holes in BOTH sides. This agent doesn't argue for bull or bear; it finds flaws in the logic, missing data, or risky assumptions. If you build this, you now have a 4-agent system: Bull + Bear + Moderator + Skeptic.
How multi-agent systems work in production
This debate system is a toy, but the pattern is industry-standard. Real financial firms use multi-agent systems to debate portfolio decisions (why buy now vs wait?), cybersecurity teams use multiple agents to think through attack vectors, and medical teams use agent debate to diagnose tricky cases. The key insight: a single agent can be smart, but two agents with opposite biases arguing in front of a referee make better decisions. You've just built that referee.
Task: Modify the DebateTurn model to include a sentiment_score: float field (range -1.0 to +1.0, where -1 is very bearish, +1 is very bullish). After each agent's turn, estimate their sentiment (e.g., the bull agent's turn with "stock is cheap" = +0.8, the bear agent's "growth stalling" = -0.7).
Then compute an overall debate sentiment as the average of all 6 turns. If it's positive, the debate is bullish; if negative, bearish.
Hint: Add sentiment_score: float to DebateTurn, then in the system prompt, ask each agent to assign a sentiment to their own claim (e.g., "On a scale of -1 to +1, how bullish/bearish is your argument?"). Parse it from the JSON. At the end, average all 6 scores to get debate_sentiment. If > 0, print "Overall debate is BULLISH"; if < 0, "BEARISH".
Test your knowledge
You've built a multi-agent system.
Now build ones that scale to production.
Join the Best AI Program: The AI-Era Agentic Engineer β 5 months, fully online, with placement assistance. Week 1 is free.
Explore The AI-Era Agentic Engineer β