Skip to content
netologistPublic

About

Autonomous SRE AI Multi-Agent Incident Investigation

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

9 Commits

Folders and files

Repository files navigation

πŸ‘ Shepherd

Autonomous SRE AI Multi-Agent Incident Investigation & Root Cause Analysis Platform

CI - Build & Test E2E - Kubernetes Kind Python 3.11+ Architecture License: MIT

Inspired by and built upon the production architecture documented in Trendyol Tech: SRE AI Agents in Trendyol.


πŸ“– Table of Contents

  1. Executive Summary & Background
  2. Key Production Lessons from Trendyol
  3. Architecture Overview
  4. Core Architectural Pillars
  5. Project Directory Structure
  6. Getting Started & Installation
  7. Usage Guide
  8. End-to-End Testing with Local Kind Cluster
  9. CI/CD Automation (GitHub Actions)
  10. Architecture Decision Records (ADRs)

🎯 Executive Summary & Background

In complex, distributed microservice architectures (+20,000 services, +2,000 databases, millions of requests/second), an incident triggers an avalanche of downstream symptoms: database pool starvations, HTTP 504 gateway timeouts, circuit breaker trips, and container memory ballooning.

Asking a single monolithic AI agent to "find the root cause" typically backfiresβ€”it floods responders with false positives and identifies symptom services rather than the true culprit.

Shepherd implements a collaborative multi-agent investigation pipeline that:

  • Runs automatically when an incident or P1/P2 alert is declared.
  • Fans out domain-isolated specialists (Metrics, Traces, Kubernetes, Troubleshooting) in parallel.
  • Enforces an evidence-only cross-validation gate to filter noise.
  • Produces a structured FinalReport with an actionable evidence chain, timeline, and remediation tasks.
  • Transitions into a stateful, tool-enabled assistant allowing SRE on-call engineers to interrogate findings interactively.

πŸ’‘ Key Production Lessons from Trendyol

Shepherd codifies the real-world operational insights published in the Trendyol Tech case study:

  1. Avoid Monolithic Toolsets: Exposing ~200 tools in a single MCP server causes agents to make off-target calls (e.g. Kubernetes agent attempting Jaeger PromQL queries) and burns millions of tokens. Shepherd uses domain-scoped MCP servers (~50-60 scoped tools per specialist).
  2. Composite MCP Tools: Merging 3-4 consecutive tool calls (e.g., describe pod, get events, get usage) into single composite operations like diagnose_pod and application_perf_overview reduces tool turns and token usage by ~40%.
  3. Tool-Free Correlator: Giving the correlation node tool access tempts it to hallucinate new investigations. Keeping it tool-free forces it to act strictly as an evidence synthesizer over specialist findings.
  4. Deterministic Control Flow: LLMs should never decide graph routing loops. Control flow transitions (Deep Dive vs. Synthesis) are governed by deterministic Python rules.
  5. Phase-Specific Model Profiles: Using expensive models (e.g. Claude Opus) everywhere is slow (~9 mins) and costly. Tiered model allocation (Fast Flash models for Gather/Prefetch, Balanced Pro models for Specialists, Deep-Reasoning models for Correlation/Synthesis) lowers end-to-end run time to ~4 minutes.
  6. Multi-Model & Deterministic Fallback: An investigation must never fail with an uncaught exception. If all LLMs fail, a deterministic template report is assembled from raw specialist findings.

πŸ› Architecture Overview

                                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                 β”‚ Entry Router β”‚
                                 β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                                        β”‚
                                 β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
                                 β”‚ Gather Agent β”‚ (Regex Extraction + Parallel Prefetch)
                                 β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                                        β”‚ (LangGraph Send Fan-Out)
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β–Ό                     β–Ό                     β–Ό
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚ Metrics Spec  β”‚     β”‚  Traces Spec  β”‚     β”‚   K8s Spec    β”‚
          β”‚ (PromQL/Perf) β”‚     β”‚ (Jaeger/OTel) β”‚     β”‚ (Read-Only)   β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚                     β”‚                     β”‚
                  β”‚ (Typed Pydantic Findings Extraction)      β”‚
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                        β–Ό
                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                              β”‚  Correlate Agent  β”‚ (Zero Tool Access / Evidence Matching)
                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                        β”‚
                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                              β”‚  Evaluation Gate  β”‚ (Deterministic Python Rule)
                              β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
                 (Unvalidated)     β”‚         β”‚ (Cross-Validated / Max Rounds = 2)
                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         └─────────────────┐
                 β–Ό                                             β–Ό
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚   Deep Dive   β”‚ ────────(Loop Back)──────>β”‚ Synthesize Agent  β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                                β–Ό
                                                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                                      β”‚    FinalReport    β”‚
                                                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                                β”‚
                                                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                                      β”‚  Post-Inv Chat    β”‚ (Stateful + Live MCPs)
                                                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

βš™οΈ Core Architectural Pillars

1. Domain-Scoped MCP & Composite Tools

Specialists interact only with domain-isolated MCP servers:

  • metrics-mcp: PromQL queries, get_service_golden_signals, application_perf_overview.
  • traces-mcp: Jaeger span waterfalls, search_jaeger_traces, jaeger_search_service_traces.
  • k8s-mcp: Read-only cluster inspection, list_pod_status, get_pod_events, diagnose_pod, get_workload_overview.
  • troubleshoot-mcp: Pre-checks, investigate_incident, get_intelligent_troubleshooting, run_static_infra_checks.

All MCP tools format responses into concise, structured Markdown tables, eliminating verbose JSON overhead.

2. Two-Phase Specialist Execution

Every specialist executes in two sequential phases:

  • Phase 1 (Tool-Use Loop): Iterative research against domain MCP tools bounded by iteration caps, per-call size limits (50KB), and duplicate suppression.
  • Phase 2 (Structured Extraction): Tool-free LLM extraction converting accumulated telemetry notes into strongly-typed Pydantic models (MetricsFindings, TraceFindings, KubernetesFindings).

3. Tool-Free Correlation & Cross-Validation Rule

The Correlate Agent has zero tool access. It receives typed findings from all specialists and tests for convergence: $$\text{Primary Root Cause} \iff \text{Corroborated by } \ge 2 \text{ independent specialists}$$ Single-source anomalies remain marked as contributing factors rather than root causes.

4. Deterministic Evaluation Gate & Deep Dive Loop

Routing after correlation is executed by a pure Python rule:

def route_after_evaluation(correlation, deep_dive_count):
    if deep_dive_count >= 2:
        return "synthesize"
    if correlation and correlation.confidence == "high" and correlation.cross_validated:
        return "synthesize"
    return "deep_dive"

When evidence is incomplete, the evaluator produces targeted questions dispatched directly to named specialists. A hard cap of 2 deep dive rounds prevents unbounded execution.

5. Model Profiles & Multi-Tier Fallback Chain

Models are allocated per pipeline phase:

  • Gather / Prefetch: gemini-2.5-flash / gpt-4o-mini / claude-3-5-haiku (Fast, token-efficient).
  • Specialists: gemini-2.5-pro / claude-3-7-sonnet (Balanced reasoning & tool-use).
  • Correlate / Evaluate / Synthesize: claude-3-7-sonnet / claude-opus (Deep cross-domain reasoning).

6. Production Guardrails & Safe Coercion

  • Duplicate Tool Detector: SHA-256 hash tracking prevents repeated tool calls with identical arguments.
  • Character Budget Caps: 50,000 chars per tool result; 800,000 chars cumulative per specialist.
  • Safe Coercion: Custom Pydantic validators automatically coerce strings like "<UNKNOWN>", "-", "N/A" into safe zeros/nulls, preventing schema validation crashes.

7. Stateful Post-Investigation Chat

Once the FinalReport is assembled and stored in the LangGraph PostgreSQL checkpointer, SRE engineers can chat on the same thread. The Chat Agent retains full incident findings and has active MCP tool access to run live verifications under human direction.

8. Continuous 1-5 Star Feedback Loop

Every investigation report and chat turn can be rated (1 to 5 stars + comments) via the feedback API to monitor RCA accuracy over time.


πŸ“‚ Project Directory Structure

shepherd/
β”œβ”€β”€ CONTEXT.md                                   # Domain Ubiquitous Language & Glossary
β”œβ”€β”€ Dockerfile                                   # Production container image with embedded kubectl
β”œβ”€β”€ pyproject.toml                               # Package dependencies & tool configs
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ adr/                                     # Architecture Decision Records (0001 - 0005)
β”‚   └── architecture/
β”‚       β”œβ”€β”€ high-level-architecture.md           # System context & data flow diagrams
β”‚       └── low-level-architecture.md            # LangGraph state machine & sequence specs
β”œβ”€β”€ manifests/
β”‚   └── scenario-oom-incident.yaml               # Realistic OOMKilled & CrashLoop incident scenario
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ setup-kind-e2e.sh                        # Automated Kind cluster & scenario deployment
β”‚   β”œβ”€β”€ run-e2e-incident.py                      # Multi-agent investigation runner against live cluster
β”‚   └── cleanup-kind.sh                          # Kind cluster teardown script
β”œβ”€β”€ src/
β”‚   └── shepherd/
β”‚       β”œβ”€β”€ config/                              # Model profiles, limits & environment settings
β”‚       β”œβ”€β”€ domain/                              # Pydantic schemas, findings & safe coercion
β”‚       β”œβ”€β”€ mcp/                                 # Domain-scoped MCP clients & composite tools
β”‚       β”œβ”€β”€ agents/                              # Specialist registry, guardrails & pipeline agents
β”‚       β”œβ”€β”€ graph/                               # LangGraph state, Send() fan-out & Entry Router
β”‚       └── server/                              # FastAPI REST & feedback endpoints
└── tests/                                       # End-to-end integration & unit test suite

πŸš€ Getting Started & Installation

Prerequisites

  • Python 3.11+
  • (Optional) Docker / Colima / OrbStack & Kind for live Kubernetes E2E testing.
  • (Optional) API Keys for Anthropic, OpenAI, or Google Gemini (ANTHROPIC_API_KEY, OPENAI_API_KEY, GEMINI_API_KEY). If omitted, Shepherd operates in built-in offline simulation mode.

Installation

# Clone repository
git clone https://github.com/netologist/shepherd.git
cd shepherd

# Install in editable mode with development dependencies
pip install -e ".[dev]"

Running Unit & Integration Tests

PYTHONPATH=src pytest tests/ -v

πŸ’» Usage Guide

Python API Usage

import asyncio
from shepherd.graph.router import ShepherdRouter
from shepherd.domain.schemas import InvestigationType

async def run_investigation():
    router = ShepherdRouter()

    # 1. Trigger autonomous investigation
    prompt = "INC-1042: High latency and 5xx errors on checkout-service in namespace prod"
    state = await router.start_investigation(
        raw_input=prompt,
        investigation_type=InvestigationType.INCIDENT_REVIEW,
        investigation_id="inv-demo-1042",
    )

    report = state["final_report"]
    print(f"Primary Root Cause: {report['primary_root_cause']}")
    print(f"Confidence: {report['confidence']} (Cross-Validated: {report['cross_validated']})")

    # 2. Interactive post-investigation chat
    reply = await router.send_chat_message(
        investigation_id="inv-demo-1042",
        message="Which database queries were causing the connection pool starvation?",
    )
    print(f"\nAgent Chat Reply:\n{reply}")

if __name__ == "__main__":
    asyncio.run(run_investigation())

FastAPI REST Endpoints

Start the API server:

uvicorn shepherd.server.app:app --host 0.0.0.0 --port 8000 --reload

1. Start an Investigation (POST /api/v1/investigations)

curl -X POST http://localhost:8000/api/v1/investigations \
  -H "Content-Type: application/json" \
  -d '{
    "raw_input": "INC-4021: order-api pods failing with CrashLoopBackOff in ecommerce-demo",
    "investigation_type": "incident-review",
    "investigation_id": "inv-4021"
  }'

2. Get Investigation Report (GET /api/v1/investigations/{id})

curl -X GET http://localhost:8000/api/v1/investigations/inv-4021

3. Post-Investigation Chat (POST /api/v1/investigations/{id}/chat)

curl -X POST http://localhost:8000/api/v1/investigations/inv-4021/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "What is the memory limit configured on the crashing pod?"}'

4. Submit Feedback Rating (POST /api/v1/feedback)

curl -X POST http://localhost:8000/api/v1/feedback \
  -H "Content-Type: application/json" \
  -d '{
    "investigation_id": "inv-4021",
    "rating": 5,
    "comment": "Accurately diagnosed OOMKilled exit code 137.",
    "reviewer": "oncall-sre@company.com"
  }'

Interactive Terminal RCA Runner

Run the rich visual investigation terminal script:

python scripts/run-e2e-incident.py

🚒 End-to-End Testing with Local Kind Cluster

Test Shepherd against a real local Kubernetes cluster simulating an OOMKilled microservice failure:

Step 1: Provision Cluster & Deploy Faulty Microservice

./scripts/setup-kind-e2e.sh

Creates a shepherd-e2e Kind cluster and deploys order-api with a 32Mi limit in namespace ecommerce-demo.

Step 2: Run Live Multi-Agent Investigation

python scripts/run-e2e-incident.py

Shepherd connects to the live cluster via kubectl, diagnoses exit code 137 (OOMKilled), extracts span lock timeouts, synthesizes the cross-validated report, and handles interactive chat.

Step 3: Teardown Cluster

./scripts/cleanup-kind.sh

πŸ”„ CI/CD Automation (GitHub Actions)

  • .github/workflows/ci.yml: Python 3.11/3.12 test matrix + Docker build verification.
  • .github/workflows/e2e.yml: Spawns an ephemeral Kind cluster, deploys the OOM incident scenario, and executes the multi-agent investigation test.

πŸ“š Architecture Decision Records (ADRs)

Detailed decision records are cataloged in docs/adr/:


πŸ“œ References

  • Altunsoy, GΓΌven. "SRE AI Agents in Trendyol". Trendyol Tech, Medium (2026). Read Article

Shepherd β€’ Built with ❀️ for SRE & On-Call Engineering Teams

About

Autonomous SRE AI Multi-Agent Incident Investigation

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages