How DocuLegis Rebuilt Its Legal AI System on Mastra

DocuLegis gives European legal teams regulatory answers they can trace back to source — and keeps customer data on infrastructure they control. Getting there meant replacing a hard-to-maintain custom agent system with Mastra.

Lëtzebuerg.ai started as a broader bet on AI for traditional businesses. They picked legal as the place to start after seeing how many smaller law firms and legal departments in Luxembourg and neighboring jurisdictions needed something more tailored than the one-size-fits-all options on the market. They began narrow, solving regulatory search in the financial domain, before expanding into other areas.

Now, DocuLegis is a legal AI "workstation" for small and mid-size European law firms and legal departments. It gives them contract review, regulatory research, and drafting capabilities.

In a legal use case like this one, accuracy and traceability are essential. Generated responses can't be outdated, wrong, or hallucinated, and users have to be able to verify them — the output should include the exact document, paragraph, or sentence supporting it.

Since the company mostly targets European customers, the team also received a lot of questions about GDPR compliance, banking secrecy, and where client data would be stored.

The custom architecture and its limitations

Before adopting Mastra, the DocuLegis team built custom agents with direct model API calls and experimented with OpenAI APIs, Ollama, LangChain, and other frameworks.

That implementation created a lot of engineering problems. Tool invocation was unreliable — they had to create and call tools manually, memory was often overloaded or completely lost, and it was hard to change models. The team also faced availability issues. There were rate limits with the different APIs or sometimes OpenAI would just be down, and the system couldn't switch models easily without something like OpenRouter. Large models were handling small tasks, and citations didn't support the answers at all. This last issue would be especially damaging for the product's reputation. After all, legal matters are all about traceability.

At some point, patching the system further stopped making sense. The team decided to rebuild it with Mastra instead.

"We completely abandoned the previous product and started from zero again with Mastra. We managed to recreate previous capabilities and even make them better in about 3 months." — Manoj Elvitigala, Head Developer

Why Mastra

The engineering team at DocuLegis came from a software development background. Mastra's TypeScript-first approach aligned well with their experience. The documentation, the book Sam wrote, and the workshops also helped them adopt the framework. They appreciated how Mastra's team contributed to the community.

"Mastra is a set of building boxes for agents — these building blocks are very easy to implement, update, scale up or add new features to in the future. We're web developers with a lot of experience, but we're not AI developers and Mastra helped us get agents immediately inside our web development." — Manoj Elvitigala, Head Developer

"Funny thing: I explained Mastra to my wife and told her that Mastra is like Lego for software engineers." — Jan Lohmann, Co-founder

DocuLegis now uses Mastra workflows and multiple agents to divide responsibilities such as contract analysis, risk analysis, search, tool use, and drafting.

The contract review is a workflow with an agent that has a lot of tools wired in. It lets a user upload a contract and select its type, governing law, and legal role. The system provides per-clause risk analysis, suggested wording, and comments, followed by an in-app chat that uses Mastra memory. Teams can define reusable legal roles and contract configurations with prompts, background documents, templates, precedents, and internal rules.

The regulatory search and knowledge management modules use the Mastra RAG environment with Mastra memory built in, on top of an agentic RAG setup. Knowledge management holds documents each client uploads directly. On the regulatory side, that setup currently draws on a roughly 40,000-document database from the European Publication Office — one source among several regulators, such as Luxembourg's CSSF or Germany's BaFin, the team plans to connect.

The DocuLegis team also built in ways to handle hallucination and bad citations. The citation pipeline uses different models for reranking and answer generation. Before the output is shown to the user, a separate validator checks citations against the answer and removes any that don't hold up. Mastra compacts the memory and only keeps what's related to the question.

The team also pairs Mastra's eval framework with Langfuse for tracing, to see what the agent is actually doing in the background — something their earlier custom-built evals often got wrong. As Manoj put it, without that visibility, "I don't know what the agents are doing in the background." It's made the system less of a black box.

Self-hosting Mastra

As Marco Mišolić, co-founder of DocuLegis, says: "it's very important that users feel their data is being processed somewhere safe and controlled." The team's initial instinct was: instead of a company paying tens of thousands for an OpenAI subscription, why not just buy the hardware and host on-premise? But models keep evolving, so that means continuously reinvesting. They wanted an option in between and that's when they found LuxProvide, a partly state-owned company promoted by the Luxembourg state, at the heart of the AI program pushed by the European Commission. That made it very reassuring for clients.

DocuLegis now self-hosts Mastra and Mastra Studio in a private-cloud environment operated through LuxProvide's AI infrastructure in Luxembourg. No customer data is sent to external models or tool services. The system uses a locally deployed GPT OSS model and the team is testing Kimi K2 as a potential addition.

What's next for DocuLegis

DocuLegis is currently in the kickstart phase. The team is in conversations with legal departments of companies interested in implementing these workflows. The goal is to grow adoption among SMEs in Europe. The focus is not only law firms, they want to go broader and target recruitment or real-estate companies. As Jan Lohmann says: "A lot of companies haven't even considered that their legal department can be upgraded."

Product-wise, the team's next step is to implement customer workflows and collect feedback while they continue improving citation mitigation, eval visibility, integrations, and drafting capabilities.

On the Mastra front, they want to enable non-devs to build agents as well. Today, non-developers can only describe what an agent should do and wait for the dev team to build it in code. Mastra's Agent Builder could let them build and test agents on their own, with the dev team cloning the working version into the product.

Product Marketer & Content Engineer
Articles by Patrycja
Share:

Start building today

Quickstart