惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
aimingoo的专栏
aimingoo的专栏
H
Help Net Security
L
LangChain Blog
M
MIT News - Artificial intelligence
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Check Point Blog
P
Proofpoint News Feed
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
U
Unit 42
I
InfoQ
月光博客
月光博客
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
博客园 - Franky
D
Docker
B
Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Captain Cool: A Gemini Multi-Agent IPL Match Strategist
Abhishek Swa · 2026-05-17 · via DEV Community

Cricket captaincy is not just "pick the best bowler." It is matchups, dew, field dimensions, batter psychology, bowler workload, Impact Player timing, and the courage to make a call before the obvious moment arrives.

Captain Cool is my attempt to turn that into an agentic AI system: a virtual IPL captain that reads the match state, runs an internal dugout debate, and returns the next tactical decision in cricket language.

The stack is intentionally Google-first:

  • Next.js frontend and API routes
  • Gemini via @google/genai
  • Model: gemini-2.5-flash
  • Gemini function calling for tools
  • CricAPI / CricketData live score integration
  • Free no-key Cricbuzz-backed fallback endpoint
  • Custom multi-agent orchestration

The multi-agent loop is custom TypeScript orchestration, but the tool use is real Gemini function calling.

What Captain Cool Does

The user enters or fetches the current match state:

  • innings, over, ball
  • score and wickets
  • striker and non-striker
  • bowling resources
  • pitch condition
  • dew factor
  • venue
  • target / required rate
  • Impact Player availability
  • optional live match ID or URL

Captain Cool replies with:

  • the next captaincy decision
  • cricket-language reasoning
  • visible dissent from the Devil's Advocate agent
  • confidence score
  • counterfactual
  • full debate transcript

Example output:

Captain's Call:
BOWL PATHIRANA - OVER 16

Why:
The wet ball has taken Jadeja's grip away. Pathirana's skiddy angle
is harder for Tilak to get underneath, and the field protects the
midwicket release shot.

Dissent:
The risk is burning Pathirana before the 18th. If this over leaks 14,
the death plan gets thin.

Enter fullscreen mode Exit fullscreen mode

Architecture

┌─────────────────────────────────────────────────────────────┐
│                         Next.js UI                           │
│  Premium cricket broadcast dashboard                         │
│  Manual match state + live match fetch + agent debate view    │
└──────────────────────────────┬──────────────────────────────┘
                               │
                               ▼
┌─────────────────────────────────────────────────────────────┐
│                    Next.js API Routes                        │
│                                                             │
│  /api/strategy                                               │
│  /api/cricket/current                                        │
│  /api/cricket/match?id=...                                   │
│  /api/cricket/free-score?match=...                           │
│  /api/live-matches                                           │
│  /api/live-strategy                                          │
└──────────────────────────────┬──────────────────────────────┘
                               │
                               ▼
┌─────────────────────────────────────────────────────────────┐
│                 Gemini Agent Orchestrator                    │
│                                                             │
│  1. Stats Analyst                                            │
│     ├─ fetchLiveMatchData tool, when match ID is present      │
│     └─ calculateMatchPressure tool                           │
│                                                             │
│  2. Tactical Strategist                                      │
│     └─ proposes first captaincy call                         │
│                                                             │
│  3. Devil's Advocate                                         │
│     └─ challenges the plan                                   │
│                                                             │
│  4. Tactical Strategist                                      │
│     └─ defends or revises                                    │
│                                                             │
│  5. Match Commentator                                        │
│     └─ formats fan-friendly final answer                     │
└──────────────────────────────┬──────────────────────────────┘
                               │
                               ▼
┌─────────────────────────────────────────────────────────────┐
│                         UI Result                            │
│  Captain's Call + Why + Dissent + Confidence + Debate         │
└─────────────────────────────────────────────────────────────┘

Enter fullscreen mode Exit fullscreen mode

Agent Design

The system uses four named Gemini-powered agents. Each agent is a separate Gemini call with its own system prompt and role.

1. Stats Analyst

The Stats Analyst does not make tactical decisions. It reads numbers, phase, pressure, match conditions, and live data.

Prompt excerpt:

You are a world-class cricket statistician — think of ESPNcricinfo's
StatsGuru merged with a baseball sabermetrician.

YOUR IDENTITY:
- You speak in numbers, trends, and matchups ONLY
- You NEVER suggest a decision or give an opinion
- You respond to queries with structured statistical evidence
- Every claim must include a number: average, strike rate, economy,
  sample size, historical trend
- If a CricAPI live match ID is present in the match state, your FIRST
  action must be calling fetchLiveMatchData before any analysis.

Enter fullscreen mode Exit fullscreen mode

Output shape:

STATISTICAL BRIEF:
• Matchup
• Venue Trend
• Phase Analysis
• Condition Factor
• Historical Parallel

Enter fullscreen mode Exit fullscreen mode

2. Tactical Strategist

The Strategist is the captaincy brain. It makes the first call: who bowls, how the field is set, whether to use the Impact Player, when to take the timeout, and what the contingency is.

Prompt excerpt:

You are the tactical brain of an IPL captaincy team. Think MS Dhoni's
instinct + Rohit Sharma's bowling changes + Hardik Pandya's aggression.

YOUR IDENTITY:
- You make BOLD, specific tactical decisions
- You think 2-3 overs ahead, not just the next ball
- You understand cricket psychology, momentum, and pressure
- You're not afraid to use unorthodox options

Enter fullscreen mode Exit fullscreen mode

Output shape:

TACTICAL DECISION:
• Next Bowler
• Field Setup
• Batting Move
• Impact Player
• Strategic Timeout
• Contingency

TACTICAL LOGIC:
...

Enter fullscreen mode Exit fullscreen mode

3. Devil's Advocate

This agent exists to disagree. It challenges the Strategist's first plan with the kind of objection an assistant coach would raise in a real dugout.

Prompt excerpt:

You are the cautious inner voice of a cricket captain — the part
that asks "what if this goes horribly wrong?" before every decision.

YOUR IDENTITY:
- Your job is to FIND FLAWS in every tactical proposal
- You think about WORST-CASE SCENARIOS
- You remember cricket history where similar decisions backfired
- You are NOT negative — you are rigorous.

Enter fullscreen mode Exit fullscreen mode

Critique framework:

1. Matchup counter
2. Resource mismanagement
3. Condition misread
4. Psychological backfire
5. Historical precedent
6. Opportunity cost

Enter fullscreen mode Exit fullscreen mode

4. Match Commentator

The Commentator turns the internal debate into something a non-technical fan can enjoy.

Prompt excerpt:

You are the voice of cricket — imagine Harsha Bhogle's storytelling
meets Ravi Shastri's energy meets Ian Bishop's gravitas.

YOUR IDENTITY:
- You translate complex tactical decisions into ELECTRIC commentary
- You make cricket strategy accessible to a 12-year-old fan
- You capture the DRAMA, the tension, the "what if"
- You speak like you're on live television

Enter fullscreen mode Exit fullscreen mode

Final output:

CAPTAIN COOL'S CALL
THE COMMENTARY
THE DUGOUT DEBATE
CONFIDENCE METER

Enter fullscreen mode Exit fullscreen mode

Gemini Function Calling

Captain Cool uses Gemini function calling for real tool use. The two important tools are:

Tool 1: fetchLiveMatchData

This tool fetches live match information before analysis when a live match ID or URL is available.

Schema:

export const fetchLiveMatchDataTool: FunctionDeclaration = {
  name: "fetchLiveMatchData",
  description:
    "Fetch live match score, status, teams, venue, scorecard, and innings details. Prefer CricAPI match IDs; also accepts a Cricbuzz URL or numeric match ID through a free no-key fallback.",
  parameters: {
    type: Type.OBJECT,
    properties: {
      matchId: {
        type: Type.STRING,
        description: "A CricAPI match ID, Cricbuzz match ID, or Cricbuzz live match URL.",
      },
    },
    required: ["matchId"],
  },
};

Enter fullscreen mode Exit fullscreen mode

Handler:

export async function handleFetchLiveMatchData(args: {
  matchId?: string;
}) {
  if (!args.matchId) {
    return { error: "Missing live match ID or URL." };
  }

  try {
    if (!/^https?:\/\//i.test(args.matchId) && process.env.CRICAPI_KEY) {
      return await getLiveCricketMatchDetails(args.matchId);
    }
  } catch {
    // Fall through to the free no-key provider.
  }

  return await getFreeLiveCricketScore(args.matchId);
}

Enter fullscreen mode Exit fullscreen mode

Tool 2: calculateMatchPressure

This local tool converts match state into tactical context.

It calculates:

  • current over
  • balls remaining
  • innings phase
  • current run rate
  • required run rate
  • runs needed
  • pressure score
  • pressure label
  • bowler overs remaining
  • tactical flags

Example output:

{
  "phase": "death",
  "currentRunRate": 9.16,
  "requiredRunRate": 9.78,
  "pressureScore": 72,
  "pressureLabel": "high",
  "tacticalFlags": [
    "Death-over specialists and boundary protection take priority.",
    "Dew reduces grip, so yorkers, hard lengths, and pace-off cutters become safer than big spin."
  ]
}

Enter fullscreen mode Exit fullscreen mode

Orchestration Flow

The orchestration is intentionally multi-turn:

Step 1:
Stats Analyst checks if liveMatchId exists.
If yes, Gemini calls fetchLiveMatchData.

Step 2:
Stats Analyst calls calculateMatchPressure.

Step 3:
Stats Analyst summarizes tactical context.

Step 4:
Tactical Strategist proposes the first plan.

Step 5:
Devil's Advocate challenges the plan.

Step 6:
Tactical Strategist defends or revises.

Step 7:
Match Commentator converts the whole debate into final fan-facing JSON.

Enter fullscreen mode Exit fullscreen mode

The important bit: this is not one Gemini call pretending to be multiple agents. Each role is a separate Gemini request with its own prompt and context.

Real-Time Data

Captain Cool supports three live-data paths:

1. CricAPI / CricketData

Used for current match lists and detailed scorecards.

Routes:

GET /api/cricket/current
GET /api/cricket/match?id=<match-id>

Enter fullscreen mode Exit fullscreen mode

Environment variable:

CRICAPI_KEY=...

Enter fullscreen mode Exit fullscreen mode

2. Free No-Key Fallback

For quick demos without a key, the app can fetch from a Cricbuzz-backed JSON endpoint.

Route:

GET /api/cricket/free-score?match=<id-or-url>

Enter fullscreen mode Exit fullscreen mode

This accepts:

  • Cricbuzz numeric match ID
  • Cricbuzz live match URL

3. SportMonks

SportMonks is available as a richer live source when a token is present.

Routes:

GET /api/live-matches
POST /api/live-strategy

Enter fullscreen mode Exit fullscreen mode

Environment variable:

SPORTMONKS_API_TOKEN=...

Enter fullscreen mode Exit fullscreen mode

UI Design

I wanted the interface to feel like a premium cricket broadcast truck, not a generic chatbot.

The dashboard includes:

  • dark navy broadcast-studio theme
  • pitch green and gold accents
  • scoreboard-style match state
  • live match ticker
  • pitch visualization
  • field placement diagram
  • agent debate bubbles
  • confidence gauge
  • live metrics sparklines
  • "Ask Captain" action button
  • "Free Fetch" live-score fallback

The UI sections are:

Top Bar:
THE DUGOUT, AI status, match clock, ticker

Left Panel:
Match state, scoreboard, pitch, batters, bowlers

Center Panel:
Live score fetcher, Captain's Call, controls

Right Panel:
Agent debate and confidence meter

Bottom:
Live metrics

Enter fullscreen mode Exit fullscreen mode

End-to-End Walkthrough

Scenario:

Match: CSK vs MI at Chepauk
Innings: 2
Score: 142/4
Overs: 15.3
Target: 186
Striker: Tilak Varma, 47* off 31
Non-striker: Hardik Pandya, 12* off 8
Pitch: turning
Dew: setting in
Impact Player: available
Strategic Timeout: available

Enter fullscreen mode Exit fullscreen mode

Step 1: Fetch Live Data

The user can either:

  • click "Fetch Current" with a CricAPI key
  • paste a Cricbuzz match ID or URL and click "Free Fetch"

The dashboard renders:

MI 142/4
15.3 overs
Target 186
Required rate around 9.8
Status: Live

Enter fullscreen mode Exit fullscreen mode

Step 2: Ask Captain

The user clicks:

ASK CAPTAIN

Enter fullscreen mode Exit fullscreen mode

The four-agent spinner starts:

Stats Analyst
Strategist
Devil's Advocate
Commentator

Enter fullscreen mode Exit fullscreen mode

Step 3: Stats Analyst Tool Calls

If a live match ID is present:

fetchLiveMatchData(matchId)

Enter fullscreen mode Exit fullscreen mode

Then:

calculateMatchPressure(matchState)

Enter fullscreen mode Exit fullscreen mode

The Stats Analyst returns something like:

• Phase Analysis: Over 15.3 means the match is entering the death phase.
• Required Rate: MI need 44 from 27 balls, about 9.78 RPO.
• Condition Factor: Dew reduces grip, so spin is less reliable.
• Resource View: Pathirana has one high-leverage over available.

Enter fullscreen mode Exit fullscreen mode

Step 4: Strategist Makes the First Call

The Strategist proposes:

Bowl Pathirana for over 16.
Set deep midwicket, long-on, long-off, deep square.
Keep fine leg up and protect the hard-length release shot.
Hold the Impact Player unless a wicket falls.

Enter fullscreen mode Exit fullscreen mode

Step 5: Devil's Advocate Pushes Back

The Devil's Advocate challenges:

If Pathirana leaks 12-14 now, CSK lose their death-over cushion.
Jadeja has completed his quota, so the fallback death plan becomes thin.
The wet ball could turn yorkers into low full tosses.

Enter fullscreen mode Exit fullscreen mode

Step 6: Strategist Revises or Defends

The Strategist responds:

Proceed with Pathirana, but bowl over the wicket to Tilak.
If the first two balls go for boundaries, switch the plan next over:
hard-length pace-off into the pitch, not spin.

Enter fullscreen mode Exit fullscreen mode

Step 7: Commentator Finalizes

The final answer becomes:

CAPTAIN'S CALL:
BOWL PATHIRANA - OVER 16

WHY:
The dew has made spin risky. Pathirana's skiddy angle is the better
weapon against a set left-hander. The field protects Tilak's strongest
release shot and asks him to hit to the bigger, weaker zone.

DISSENT:
The dugout worry is resource burn. If this over goes badly, the 18th
and 20th over plan gets uncomfortable.

CONFIDENCE:
78%

Enter fullscreen mode Exit fullscreen mode

AI Studio Prompt Link

I prototyped prompts locally in code during the build. I have not published a Google AI Studio prompt link yet.

Before publishing the final dev.to post, add it here:

Final Thought

A massive shoutout to ** @gdgcloudpune , especially *@antrixsh_gupta *, *@pratik_kale *, and the entire organizing team. The problem statement was brilliant, the vibe was electric, and events like this push the developer community to level up in incredible ways.

👇 Check out the code and try it yourself:

GitHub Repository: https://github.com/swamiabhishek45/captain-cool
Live Link : https://captain-cool-dusl.vercel.app/