How to Add Human-in-the-Loop Approval Before an AI Agent Edits a Contract

Human-in-the-loop AI agent workflow where a reviewer accepts proposed edits to an MSA and the payment term changes from 30 days to 45 days

The Challenge of AI Contract Review

Imagine a legal AI SaaS product. A legal reviewer uploads a contract to its AI chat and asks, “Review this MSA against our vendor playbook.” The agent identifies 14 places that need to be revised.

But the problem comes next. Most workflows end up like this:

  • The agent returns a new DOCX file, but its formatting differs slightly from the original. The reviewer cannot easily tell whether the changes were made correctly, so they have to read the entire document again to see what changed and why.
  • Before-and-after suggestions pile up in the chat. The reviewer then opens Word to copy and paste each change and reapply the formatting by hand.

Even if the agent finds every issue, reviewers still lose time when applying those changes to the original contract is a hassle. The problem is not the model or the prompt. It is the document editing workflow.

AI-suggested changes should go straight into the existing document, where reviewers can check each one and apply only the changes they approve. This article shows how to build that workflow into your app with Thinkfree Document Editor SDK, so your AI agent edits the document in place and a human stays in the loop for every change.

An example legal app built on Document Editor SDK

Why AI Contract Review Workflows Fail

Generating a New Document

For an AI agent, understanding a document and editing it without losing its original formatting are two separate tasks. A common approach is to extract only the text from a document, send it to a model, and build a new file from the model’s rewritten text. Formatting information such as layout and styles is lost along the way. As a result, clause numbering can fall out of order and tables can break. (Related: How to Edit an Existing DOCX with AI Without Breaking Formatting)

The same problem occurs when you ask the AI to change only part of the original document. It still generates a new file instead of editing the existing one. This makes it difficult to see what has changed or to select and apply only the suggestions you need. In the end, the reviewer has to compare the original and the output line by line.

Keeping Edits in the Chat

When the agent suggests edits in the chat, the reviewer has to carry them over into the actual document. Even when the original text is quoted, the suggestion is not linked to the corresponding location in the document. The reviewer ends up switching between the app and Word to find and fix each passage. They can choose which suggestions to keep, but applying them still requires manual work, leaving room for mistakes.

Bringing the Agent into the Document

Both problems have the same cause: the agent works outside the document. The first approach recreates the document instead of editing it, while the second never touches the document at all.

For an agent to work inside a document, three things need to be in place.

  • The agent can read and edit document objects such as paragraphs and ranges.
  • The model can call document editing functions as tools.
  • The document the reviewer sees is the same one the agent edits.

Thinkfree Document Editor SDK meets all three. You can embed an Office editor in your web app and let the agent edit the document open in that editor, directly at the object level.

Giving the Agent Editing Tools

getTools() in Document Editor SDK returns editing functions as tools an LLM can call. You can connect these tools to the model of your choice, including models from OpenAI, Anthropic, and Google.

				
					const tools = await word.getTools({ include: ["insert_text"] });
				
			
  • Each tool includes name, description, inputSchema, and execute(args).
  • You can map them to the tool formats used by OpenAI Chat Completions, Anthropic Messages, and Vercel AI SDK.
  • Use include to pass only the tools your workflow needs.

When the agent needs to change a time period in a contract clause, the LLM selects the appropriate tool to edit the relevant range in the document. The same edit can also be made directly using the object API, as shown below.

				
					const paragraph = await word.getParagraph(paragraphId);
const text = await paragraph.getText();
const from = text.indexOf("30 days");
const range = await paragraph.getRange(from, from + 7);
await range.insertText("45 days", { replace: true });
				
			

Because the edit targets a paragraph and a range within it, the existing formatting stays intact when the text changes. The edit happens in the same document the reviewer is viewing, so there is no separate copy to create or keep in sync.

Applying Only Approved Edits

Reviewers should be able to choose which of the agent’s proposed edits to approve. The simplest approach is to write “Always get the reviewer’s confirmation before editing” in the system prompt. But then approval depends on the model’s judgment. If the model ignores the instruction or is influenced by text inside the document, edits may not be applied as the reviewer intended.

To make sure only approved edits reach the document, keep approval in your app code, not in the model.

Building an Approval Gate

Architecture diagram showing a reviewer’s request passing from an AI agent to an approval gate in the host app, where only approved tool calls reach Document Editor SDK

Your app passes the tools needed for the workflow to the agent using getTools({ include }). The agent interprets the reviewer’s request and decides which tools to use. The host app’s tool loop is what actually runs the calls made by the agent.

So you can add an approval gate to the host app’s tool execution function and decide which calls run. Here is what you can build into it.

  • Check who has access to the document
  • Provide a UI where reviewers can select and approve suggestions
  • Validate and filter the arguments in each model call

Designing the Approval UI

To help reviewers scan AI suggestions and choose which ones to apply, split the process into two steps.

  • Collect suggestions: Instead of running the agent’s edit calls right away, add them to a list of suggestions. Show each one in a separate card explaining what the tool will do, what text will change, and why. This helps reviewers decide which suggestions to approve.
  • Apply selected suggestions: The host app calls tool.execute() only for the suggestions the reviewer approved.

After the edits are applied, the reviewer can refine the wording and finish up right in the editor.

Only the 12 approved edits reach the document

Development Tips

  • Find the target again right before applying an edit. The document may change while the reviewer is selecting suggestions. Instead of storing a fixed offset, read the document again and locate the target when applying the edit. This helps ensure that the approved wording is applied in the right place.
  • Let reviewers know when an edit fails. Display the isError value returned by execute() so reviewers can quickly see which suggestions were not applied. Reviewers can trust the results only when what was applied matches what they approved.
  • Let your app decide which tools the agent can use. Defining a fixed list of tools in your app code sets the limits of what the agent can do. For example, even if a contract contains text that looks like an instruction, the agent can only act through the tools your app allows.

Customizing the Workflow

  • Decide which edits need approval. Separating simple fixes, such as terminology standardization, from edits that require human review can reduce the reviewer’s workload.
  • Tailor the workflow to different roles and tasks. The editor can open the same document in either editing mode or viewing mode (WORD_EDITOR / WORD_VIEWER). Set different permissions for each stage of the process, such as document creation, review requests, review, and final approval.
  • Call editing commands directly from your code. You can call the same APIs used by the agent from JavaScript application logic. This is especially useful for repetitive tasks with clearly defined actions, such as automatically applying contract amounts that have already received internal approval.

A read-only review flow designed in the host app

Implementation Checklist

  • Re-read the paragraph containing an approved suggestion to confirm that the revised wording is correct.
  • Confirm that rejecting a suggestion leaves the original paragraph unchanged.
  • Check the approval gate logs to make sure no tool calls were executed without approval. We recommend that the host app log the actor, document ID, tool name, result, correlation ID, and more.
  • Save the edited document, then reopen it to confirm the changes are still there.

See It in Action

Open the Playground to watch an AI agent edit a document and see how you can bring this workflow into your application.

Connect document tools to your agent.

Explore how legal apps can use the SDK.

Subscribe to the Thinkfree Newsletter

Stay current on Thinkfree product news and the trends shaping enterprise IT. No noise, just the updates that matter.

By submitting, you agree to our Privacy Policy to receive updates and news from Thinkfree Inc.

Like this post? Share with others!