TL;DR
Researchers at J.P. Morgan AI Research have introduced LAW (Legal Agentic Workflows), a modular framework that uses a code-generation agent to orchestrate specialized tools for analyzing complex legal contracts. By moving away from expensive fine-tuning toward a tool-use approach, LAW achieves massive performance gains—surpassing standard LLM baselines by up to 92.9% on complex reasoning tasks like calculating contract termination dates.
What Problem Does This Paper Address?
Legal contracts in the financial domain (specifically custody and fund services) are a nightmare for standard AI. They are incredibly long (averaging 27,000 tokens), highly unstructured, and packed with dense jargon.
Off-the-shelf Large Language Models (LLMs) like GPT-3.5 or GPT-4 often fail here because their context windows are too small to "read" the whole document at once, and they frequently hallucinate specific details like dates or parties. While some researchers try to solve this by fine-tuning models on legal text, the authors argue this is a "static" solution: it’s expensive, hard to scale, and the model quickly becomes outdated as new regulations or contract types emerge.
Methods
Instead of building a better "legal brain" through fine-tuning, the authors built a better "legal workstation." LAW uses a modular, agentic architecture:
- The Orchestrator: A Code Generation Agent that receives a user query and writes Python code to solve it. This code acts as the "plan," calling various specialized tools in sequence.
- Domain-Specific Tools:
- Extraction Tools: Use RoBERTa and fuzzy matching to pull out specific dates and parties.
- Reasoning Tools: Calculate contract lifecycles (e.g., adding a 3-year duration to an effective date) or identify "Master" vs. "Amendment" agreements.
- Semantic Labeling: A fine-tuned T5 model classifies paragraphs into 20 distinct legal sections (like "indemnification" or "termination") to enable precise retrieval.
- Text Agents: Specialized agents for summarization and comparison. To handle documents that exceed context limits, LAW splits text into chunks, processes them in parallel via "sub-agents," and then concatenates the results.
- Validation Loop: A three-tier system checks the generated code for syntax errors, "hallucinated" tools that don't exist, and runtime failures, providing feedback to the agent to self-correct.
Datasets
The researchers focused on Form 485BPOS, a regulatory filing for investment companies governed by the US Investment Company Act of 1940.
- Source: The SEC’s EDGAR database.
- Scale: 23 years of filings, resulting in a knowledge base of 17,831 legal contracts (approx. 22 GB of data).
- Evaluation: A curated dataset of 720 labeled user queries, ranging from simple retrieval (finding parties) to complex analytical tasks (comparing clauses across time).
Key Insights and Ideas
- Code as the "Glue": Using an LLM to generate Python code to solve a problem is far more reliable than asking the LLM to generate the answer directly. Code provides a deterministic way to handle math (like date calculations) and data manipulation.
- Modularity over Monoliths: By using a suite of reusable tools, the system is "future-proof." If a new type of contract appears, you only need to add a new tool or update a specific agent, rather than retraining the entire model.
- The Context Gap: Standard RAG (Retrieval-Augmented Generation) often fails on legal docs because relevant information is scattered. LAW’s "multi-doc traversing" and parallel sub-agents allow it to maintain a "global" understanding of documents that are hundreds of pages long.
Key Results
The performance gap between LAW and a GPT-3.5-based RAG baseline is stark, particularly as task complexity increases:
- Multi-Hop Reasoning: On the task "Find termination dates" (which requires finding an effective date and a duration, then doing the math), LAW achieved 95.4% accuracy, while the baseline managed only 2.5%.
- Entity Extraction: LAW hit 100% accuracy in identifying parties, compared to the baseline's 16.3%, which often missed entities in dense text.
- Analytical Tasks: For comparing clauses across different contracts, the baseline often failed entirely due to context limits, while LAW maintained high performance (71.9 BERTScore F1).
Limitations and Future Work
While LAW is highly effective, the authors note a few hurdles:
- Tool Reliability: The system is only as good as its tools. Ensuring tools work across every possible weirdly-formatted contract remains a challenge.
- Orchestration Complexity: As more tools are added, the logic required for the agent to choose the right tool becomes more complex.
- Language: The current system is optimized for English-language contracts.
Future work will look into expanding LAW to non-English contracts and exploring how these agentic workflows can be applied to other specialized domains like medicine or engineering.
Summary updated