The client was building a service that automatically drafted the description of an invention and its embodiments for patent specifications. The existing AI module did not produce text that could be delivered directly to users, so the lead patent attorney had to revise it manually. Under the firm's internal patent-attorney evaluation criteria, its quality was only about 20–30% of the target. As the sole contractor responsible for the AI module, I handled the LangGraph pipeline design, prompts, exception handling, and containerization.
Process
Changing a single model or prompt could not consistently reproduce the attorney's revision criteria or the style, word choice, and sentence structure that legal documents require. I worked with the lead patent attorney to understand the drafting sequence, category-specific judgment criteria, and principles for correcting failure cases. I then formalized this tacit domain knowledge as generation rules and validation conditions, dividing the implementation between prompt engineering and business logic.
I improved the existing code by separating it into LangChain components so models and prompts could be swapped, and redesigned the generation, validation, and revision sequence as a LangGraph workflow. Input documents were parsed by section so that each step received only the context it needed. I also added a stage within the pipeline for processing patent drawings so that non-textual information would not be omitted from generation.
Failure handling and retry logic
A service that drafts legal documents cannot pass a truncated response or formatting error directly to a user. I therefore spent more effort specifying where and how the workflow should recover than on handling successful responses.
- Continue generation when a response is cut off by the token limit
- Block prompt-injection inputs
- Automatically review generated documents and regenerate them when errors are found
Generation tasks ran in parallel. I packaged the completed module as a Docker container so the client could integrate it into the existing service environment. Production deployment and integration with the other service components were outside my scope.
Adopted into the service
Quality under the firm's internal patent-attorney evaluation criteria increased from about 20–30% to 80–90%. Parallel processing implemented inside the pipeline reduced total processing time by about 20%. The containerized module I delivered was subsequently integrated into the live service. For operational stability, the pipeline design included explicit paths and retry conditions for interrupted output, invalid input, and failed review.