← All work
Case study · Flagship

Kuwaiti Law RAG Agent

An agentic RAG system over Kuwait's legislation that answers with the exact article and line it relied on.

Role
AI Engineer
Company
6 Sigma Code
When
2026 — ongoing
Stack
gRPC · LangChain · Postgres + pgvector
77laws embedded
7,399articles, continuously ingested
9levels in the knowledge tree
The system · two lanes
Ingestion
  1. Laws inContinuous ingestion
  2. Structure-aware chunksLaw, article, line
  3. Recursive clustering9 levels, RAPTOR-style
  4. Topic nodes + vectorsDenormalized pgvector
Answering
  1. Question inover gRPC
  2. Pre-filternarrow the corpus
  3. BM25 + vectortwo retrievers
  4. RRF fusionone ranked list
  5. Cited answer2nd LLM call, verbatim

The problem

A legal answer is only useful if someone can check it. A lawyer needs the exact article and line behind a claim, not a confident paraphrase.

It is used by lawyers at an office, who previously searched the statutes by hand.

What I built

A knowledge tree, not a pile of chunks

Structure-aware chunking follows how laws are written. Recursive clustering then builds a RAPTOR-style tree across 9 levels, so broad questions land on topic nodes and narrow ones on single articles.

Hybrid retrieval

Pre-filtering narrows the corpus, then BM25 and vector search run side by side and Reciprocal Rank Fusion merges them. Exact legal terms and loose phrasing both find the right article.

Citations you can verify

Every answer points to the line and article it came from, served over gRPC to the products that use it.

The calls

Call 1 · Framework

LangChain over LangGraph.

One decision point per turn. A graph would add state machinery the problem doesn’t need. Simpler was correct, not trendy.

Call 2 · Data model

Denormalized pgvector beside normalized Postgres.

The source of truth stays clean. Retrieval reads a flat copy, so there are no joins on the hot path.

Call 3 · Citations

A second LLM call just for citations.

A dedicated pass extracts verbatim quotes. It closes the citation gap I found in my own ProofRAG benchmark.