DEV Community

Franklin Alejandro Bonalde
Franklin Alejandro Bonalde

Posted on

Building a Root Cause Analysis Agent with RAG — and the first thing that broke

I'm Frank Bonalde, an AI Engineer based in São Paulo. I build agentic systems and RAG applications. This is a write-up of one of my projects — what it does, how I measured it, and the first thing that broke. Portfolio: https://technobonal.github.io

The problem

In logistics operations, when something fails — a delayed shipment, a sync error, a stuck order — an operator usually has to dig through past incident reports and runbooks by hand to figure out the cause. It's slow, and the knowledge lives in people's heads.

I wanted an assistant where the operator describes the symptom in plain language and gets back something they can actually act on: the likely root cause, a recommended fix, a confidence level, and the evidence behind it.

The stack

I kept it deliberately simple and production-friendly:

  • Streamlit — front end, fast to iterate.
  • LangChain — agent orchestration.
  • Groq running openai/gpt-oss-120b — fast inference.
  • Cohere multilingual embeddings — operators write in Spanish and Portuguese, not just English.
  • Pinecone — vector store built from the incident documentation.

The core idea: never let the model answer from thin air. Every answer is anchored to retrieved chunks, and each piece of evidence is shown with its source and relevance score, so the user can verify instead of trusting blindly.

Structured output, on purpose

Instead of free-form text, I forced the agent to return a structured object:

# The agent must return this shape, not prose.
# Structured output makes every answer checkable the same way.
{
    "root_cause": "…",      # the most likely cause, one statement
    "solution": "…",        # recommended fix
    "confidence": 0.0,      # how sure the agent is (0–1)
    "evidence": [           # retrieved chunks that support the answer
        {"source": "runbook_42.md", "relevance": 0.81, "text": "…"}
    ]
}
Enter fullscreen mode Exit fullscreen mode

This one decision paid off everywhere: it made the UI predictable, and it made the output measurable.

How I measured quality

With structured output, evaluation becomes concrete. I ran known incidents through the agent and checked two things:

  1. Retrieval — did the right documents come back for the symptom?
  2. Root cause match — did the reported cause match the real one?

It's not a formal benchmark yet, but it's enough to catch regressions and, more importantly, to see where the system is weak instead of guessing.

The first thing that broke: confidence

Here's the interesting part. The first thing that broke wasn't retrieval or the model's reasoning — it was the confidence score.

The model happily reported high confidence even when the retrieved evidence was weak. A barely-relevant chunk, and it would still say "0.9". That's the worst kind of failure in a tool meant to be trusted: confidently wrong.

The root of it: I was letting the model self-report a single confidence number that silently mixed two very different things — how sure the model feels and how good the evidence actually is.

The fix

I separated the two signals and showed both:

# Model confidence and retrieval relevance are NOT the same thing.
# Keeping them separate stops a confident model from hiding weak evidence.
model_confidence = answer["confidence"]          # what the model claims
evidence_strength = max(c["relevance"] for c in chunks)  # what retrieval found

# If the evidence is weak, the user sees it immediately —
# regardless of how sure the model sounds.
if evidence_strength < THRESHOLD:
    show_warning("Low-evidence answer — verify before acting.")
Enter fullscreen mode Exit fullscreen mode

Now, when the evidence is weak, the user sees it right away. The model's self-assurance can no longer paper over a thin retrieval.

Lessons

  • Structure first. Forcing a structured answer made everything downstream — UI, evaluation, debugging — easier.
  • Separate your signals. A single "confidence" number that blends model certainty and evidence quality will lie to you.
  • Show the evidence. Letting users see sources and relevance turns a black box into a tool people trust.

What's next

Formal retrieval metrics (Recall@k, MRR) and citations quoted word-for-word from the source documents, so a user can jump straight to the sentence that backs each claim.


If you're working on RAG or agentic systems, I'd love to compare notes.

Top comments (0)