DEV Community

Cover image for Building Captain Cool AI: A Multi-Agent IPL Cricket Strategist Powered by Gemini 2.5
Sanket Maldhure
Sanket Maldhure

Posted on

Building Captain Cool AI: A Multi-Agent IPL Cricket Strategist Powered by Gemini 2.5

🏏 Building "Captain Cool AI": A Multi-Agent IPL Cricket Strategist

Have you ever watched an intense Indian Premier League (IPL) match, seen a team fail to defend 20 runs in the final two overs, and screamed at the TV: "Why on earth did the captain bowl a spinner there?!"

Cricket is a sport governed by numbers, but won on tactical psychology, match-ups, and gut feeling. In a high-stakes death over, a captain's dressing room is a pressure cooker.

To recreate this tactical brilliance, I built Captain Cool AI β€” a high-fidelity, multi-agent strategist system that simulates an intense debate between data analysts, tactical masterminds, contrarians, and legendary commentators to make the ultimate decision in real-time.

Here is how I built it using React, FastAPI, and the power of Gemini 2.5 Flash.


🧠 The Multi-Agent Dressing Room (Architecture)

Rather than asking a single LLM to make a decision, Captain Cool AI divides the cognitive load among four specialized agents, each with a custom persona and system instruction.

graph TD
    A[Live Match URL / Offline Scenario] --> B[StatsMaster AI]
    B -->|Calculates Probabilities & Bowler Stats| C[The Strategist - MS Dhoni AI]
    C -->|Proposes 18th Over Bowling Change| D[The Contrarian - Devil's Advocate]
    D -->|Challenges Risks & Offers Alternative| E[Harsha Bhogle AI - Commentator]
    E -->|Synthesizes Debate & Explains Final Decision| F[Premium React Dashboard UI]
Enter fullscreen mode Exit fullscreen mode

1. πŸ“Š Agent 1: StatsMaster AI (The Analyst)

  • Role: Objectively crunches the numbers. It calculates required run rates, wickets remaining, and pressure scores.
  • Tools: Equipped with Python function tools like calculate_win_probability and get_bowler_recommendation.
  • Tone: Clinical, precise, and completely emotionless.

2. 🎯 Agent 2: The Strategist (The Captain)

  • Role: Formulates the actual decision, modeled after MS Dhoni's tactical genius.
  • Input: Analyzes the current match state + StatsMaster's analytical data.
  • Tone: Decisive, leadership-oriented, and focused on psychology and match-ups.

3. 😈 Agent 3: The Contrarian (The Devil's Advocate)

  • Role: Challenges the Strategist’s decision. If the Captain wants to bowl a fast bowler, the Contrarian instantly asks, "But what if the batsman targets the short boundary?" or "Are you burning your best death bowler too early?"
  • Tone: Skeptical, analytical, and constructive.

4. 🎀 Agent 4: Harsha Bhogle AI (The Commentator)

  • Role: Acts as the bridge to the audience. It synthesizes the intense dressing-room debate, highlights the tactical trade-offs, and explains the final captain's call.
  • Tone: Energetic, dramatic, and filled with classic cricket metaphors.

πŸ› οΈ The Tech Stack

To provide a premium and production-ready experience, I migrated the original prototype to a modern decoupled stack:

  • Frontend: React (Vite) styled with a customized glassmorphic dark-mode CSS theme, featuring micro-animations, progress trackers, and beautiful layout grids.
  • Backend: FastAPI (Python), utilizing asynchronous routing for high performance.
  • AI Core: Gemini 2.5 Flash using the brand new google-genai SDK.

πŸ›‘οΈ Building for Production: Resiliency & Quota Fallbacks

One of the coolest technical challenges of this project was handling API rate limits. During heavy testing on the Gemini Free Tier, it’s common to hit the 429 Resource Exhausted quota limit.

To prevent the web application from crashing or throwing generic errors, I engineered a dual-layered premium fallback system:

1. Smart URL Slug Parser Fallback

If the live match scraping fails because the Gemini API is rate-limited, the backend automatically intercepts the exception, parses the team names out of the URL slug (e.g. kolkata-knight-riders-vs-sunrisers-hyderabad), and maps them to a database of popular IPL franchises:

# Extracting teams from Cricinfo/Cricbuzz slugs
slug_match = re.search(r'/([^/]+-vs-[^/]+)', url_lower)
if slug_match:
    parts = re.split(r'-vs-|vs', slug_match.group(1))
    # ... resolves to 'KKR' and 'SRH'
Enter fullscreen mode Exit fullscreen mode

2. Dynamic Mock Debate Generator

Instead of returning a static, hardcoded fallback response (like a fixed RCB vs CSK text), the system takes the parsed teams, batsmen, and bowlers, and dynamically injects them into a templated debate script.

If the API key is exhausted and you paste a Mumbai Indians vs Delhi Capitals match, the entire simulated debate will dynamically re-render to feature MI and DC players, delivering a seamless experience with zero downtime!


πŸš€ See It in Action

The user interface was built to give the ultimate first impression. Vibrant dark gradients, interactive toggles to switch between Offline Simulation and Live URL Scrape Mode, and an animated progress bar that lets fans track each agent's thoughts in real-time.

Captain Cool AI Dashboard Layout

The live React dashboard showing StatsMaster, The Strategist, The Contrarian, and Harsha Bhogle AI in perfect harmony.


πŸ“‚ Source Code & Contribution

The complete source code is fully open-source and version-controlled:

⭐ GitHub Repository: sanketmaldhure/ipl-captain-ai-gemini-sanket

What are your thoughts on using multi-agent debates for complex decision-making in sports? Let me know in the comments below! 🏏

Top comments (0)