How to Use the apply_patch Tool in Openclaw

Image
Table of contents: [Show]

The apply_patch tool in Openclaw lets you apply unified diffs or before/after text transformations directly to files in your workspace. It is ideal for automated code updates, configuration tweaks, and AI-assisted refactoring.

Quick Start

To enable the tool, add this to your ~/.openclaw/openclaw.json:

{
  "tools": {
    "exec": {
      "applyPatch": {
        "enabled": true,
        "workspaceOnly": true,
        "allowModels": ["gpt-4o", "gpt-5.2"]
      }
    }
  }
}

Your overall tool policy must still allow it. Including allow: ["exec"] or allow: ["group:fs"] will implicitly allow apply_patch.

Usage Modes

Option A: Before and After Text

Required fields are before (original text) and after (updated text).

{
  "before": "# Hello\n\nOne",
  "after": "# Hello\n\nTwo",
  "path": "docs/example.md",
  "mode": "view"
}

Option B: Unified Patch

Required field is patch (the unified diff text).

{
  "patch": "diff --git a/src/example.ts b/src/example.ts\n--- a/src/example.ts\n+++ b/src/example.ts\n@@ -1 +1 @@\n-const x = 1;\n+const x = 2;\n",
  "mode": "both"
}

Best Practices and Troubleshooting

  • Always verify file paths are within your workspace when workspaceOnly is enabled.
  • Use allowModels to restrict which AI models can invoke the tool.
  • For complex patches, test with mode: view first to preview changes.
  • Keep backups of critical files before applying patches.

For more automation options, check out our guide on Elevated Mode Openclaw. If you need to run shell commands, see Exec Tool Openclaw.

Openclaw apply_patch tool