# Building a Legal Document Analyzer in typescript with NodeJS

> Source: <https://dev.to/nisa_fatima_bcd75fa085b76/building-a-legal-document-analyzer-in-typescript-with-nodejs-392p>
> Published: 2026-07-29 17:30:41+00:00

Legal documents are notoriously complex, filled with specialized terminology, hidden risks, and critical obligations. Lawyers and legal professionals spend countless hours reviewing contracts, agreements, and other legal documents to identify key terms, assess risks, and summarize obligations. In this post, we'll build a Legal Document Analyzer using [HazelJS](https://hazeljs.ai/) that automates document parsing, term extraction, risk analysis, and obligation summarization.

Legal document analysis presents several challenges:

Our agent will address these challenges using HazelJS's multi-agent architecture, RAG-powered document search, and intelligent risk analysis.

The Legal Document Analyzer uses a multi-agent architecture where each agent specializes in a specific aspect of legal document analysis:

This separation allows each agent to focus on its specialty while the supervisor ensures smooth coordination between them.

One of the key features of our agent is the ability to search through a database of legal documents using Retrieval-Augmented Generation (RAG). We maintain a knowledge base of legal documents with metadata including:

When a legal professional asks about a document, the DocumentParserAgent uses semantic search to find relevant documents based on their query. For example, a query like "Find indemnification clauses in contracts" would return documents containing indemnification provisions.

The RAG implementation uses HazelJS's `RAGPipeline`

with a `MemoryVectorStore`

for efficient semantic search:

```
@Service()
export class LegalKnowledgeBaseService {
  private readonly embeddings = new LocalLegalEmbeddingProvider();
  private readonly vectorStore = new MemoryVectorStore(this.embeddings);
  private readonly rag = new RAGPipeline({
    vectorStore: this.vectorStore,
    embeddingProvider: this.embeddings,
    topK: 3,
  });

  async answer(query: string, topK = 3) {
    const sources = await this.search(query, topK);
    return {
      answer: sources.map((source) => source.content).join('\n\n'),
      sources: sources.map((source) => ({
        id: source.id,
        score: Number(source.score.toFixed(3)),
        category: source.metadata?.category,
        riskType: source.metadata?.riskType,
        severity: source.metadata?.severity,
      })),
    };
  }
}
```

The DocumentParserAgent extracts structure and metadata from legal documents. It identifies:

This structured extraction provides a foundation for deeper analysis by other agents in the system.

The TermExtractorAgent identifies legal terms within documents and provides:

This helps legal professionals quickly understand specialized language and ensures consistent interpretation across documents.

The RiskAnalyzerAgent analyzes documents for potential risks and provides:

The agent calculates overall risk levels and provides comprehensive risk summaries, helping legal professionals prioritize their review efforts.

The ObligationSummarizerAgent extracts and organizes obligations from legal documents:

This systematic approach ensures that no obligations are overlooked and provides a clear roadmap for compliance.

The LegalManagerAgent uses HazelJS's supervisor routing to coordinate between the specialist agents. When a legal professional makes a request, the supervisor analyzes the request and routes it to the appropriate specialist:

The supervisor continues delegating until it has gathered enough information to provide a comprehensive legal analysis, then synthesizes the results into a cohesive report.

Despite being a demo, the agent includes production-ready features:

The agent can be run with:

```
npm install
npm run build
npm run dev
```

The app runs on `http://localhost:3000`

with the HazelJS Inspector available at `/__hazel`

for real-time monitoring and debugging.

You can test the agent with a curl request:

```
curl -s -X POST http://localhost:3000/legal/supervisor \
  -H 'content-type: application/json' \
  -d '{"message":"Analyze this contract: Indemnification Clause: Party A agrees to indemnify and hold harmless Party B from any claims arising from negligence. Extract terms, analyze risks, and summarize obligations.","userId":"legal-analyst-1"}'
```

The agent will analyze your request, parse the document, extract legal terms, identify risks, summarize obligations, and synthesize everything into a comprehensive legal analysis—all coordinated through the supervisor routing system.

Complete Project: [Legal Document Analyzer](https://github.com/nisafatimaa/legal-document-analyzer)

HazelJS Documentation: [Docs](https://hazeljs.ai/docs)

The Legal Document Analyzer demonstrates several key [HazelJS](https://hazeljs.ai/) capabilities:

This agent shows how HazelJS can be used to build professional applications that solve complex domain-specific challenges while maintaining production-grade quality and reliability. The multi-agent approach makes it easy to extend the system with additional specialists (like compliance checkers or jurisdiction analyzers) as needed.
