DEV Community

Pranitha U P
Pranitha U P

Posted on

How I Built an Autonomous SRE Agent That Actually Remembers Past Incidents Using Hindsight and FastAPI

When production goes down, every minute counts. Traditional monitoring tools alert you instantly, but they don't remember why a similar connection pool exhaustion or memory leak happened three months ago. Your SRE team ends up reinventing the wheel under pressure.

I wanted to change that. I set out to build an Autonomous Site Reliability Engineering (SRE) Incident Response Agent that doesn't just process stateless errors, but maintains long-term, persistent operational memory to diagnose and resolve production failures instantly.

Here is how I architected and implemented it using FastAPI, Google Gemini, and Hindsight.


The Architecture: Why Stateless LLMs Fail at Ops

Most AI troubleshooting scripts fail because LLMs are stateless. When a database error hits, a standard LLM chatbot sees the error in isolation. It has no context of your infrastructure's history, past post-mortems, or previous configuration tweaks.

To build a true agentic system, we need a dedicated memory layer. I integrated Hindsight, a specialized memory system developed by Vectorize, which allows AI agents to store, semantic-recall, and evolve their operational knowledge over timespan_3span_3.

The system operates on two core pillars:

  1. Memory Retention (/api/retain): Automatically saving resolved incident runbooks, root causes, and terminal commands into a persistent vector bank.
  2. Contextual Retrieval & Diagnosis (/api/diagnose): Querying historical memory when a new error occurs, injecting relevant past runbooks into the LLM context, and generating a precise SRE resolution planspan_4span_4.

Code Implementation: Building the Backend Agent

Here is the core backend implementation using FastAPI, the modern google-genai SDK, and the Hindsight client.


python
import os
from fastapi import FastAPI
from pydantic import BaseModel
from hindsight_client import Hindsight
from google import genai

app = FastAPI(title="Autonomous AI Incident Response Agent")

# Environment & Clients Setup
HINDSIGHT_API_KEY = "hsk_a0f29fd7808a64542cf1dec59f70aab1_9b3f87fec8505c17"
HINDSIGHT_ENDPOINT = "[https://api.hindsight.vectorize.io](https://api.hindsight.vectorize.io)"
GEMINI_API_KEY = "AQ.Ab8RN6IXMyfNuX1LDejSxSjs5alio0Tk08hG5jkGikqHVC_t2A"

hindsight_client = Hindsight(base_url=HINDSIGHT_ENDPOINT, api_key=HINDSIGHT_API_KEY)
gemini_client = genai.Client(api_key=GEMINI_API_KEY.strip())
BANK_ID = "incident-logs-bank"

class DiagnoseRequest(BaseModel):
    error_log: str

def recall_and_diagnose(new_error_log: str):
    # Step 1: Recall similar past incidents from Hindsight memory
    recall_res = hindsight_client.recall(bank_id=BANK_ID, query=new_error_log)
    memories = [m.text for m in recall_res.results]
    recalled_context = "\n".join(memories) if memories else "No past memories found."

    # Step 2: Formulate SRE prompt with historical context
    prompt = f"""
    You are an Autonomous SRE Incident Response AI Agent.
    Current Server Error: {new_error_log}
    Historical Memory Runbooks: {recalled_context}
    Diagnose the root cause and provide step-by-step resolution.
    """

    # Step 3: Generate diagnostic plan via Gemini
    response = gemini_client.models.generate_content(
        model="gemini-2.5-flash",
        contents=prompt
    )
    return {"diagnosis": response.text, "recalled_memory": recalled_context}

@app.post("/api/diagnose")
def api_diagnose(data: DiagnoseRequest):
    return recall_and_diagnose(data.error_log)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)