Skip to main content
Coding agents rewrite entire files to change a few lines. A 500-line file costs ~$0.12 in tokens and takes 8 seconds to regenerate. Fast Apply merges just the changed lines at 10,500 tok/s, 98% accuracy. The agent outputs only the edited lines using // ... existing code ... markers. Morph merges them server-side and returns the complete file. Token usage drops 40% compared to full-file rewrites.

Installation

Quick Start

The instructions parameter provides context for ambiguous edits, helping the apply model make correct decisions and reach 98% accuracy. Have the parent model generate the instructions.

How It Works

1

Add an edit_file tool to your agent

Add the edit_file tool to your agent. Use one of the formats below.
Parameters:
The instructions param should be generated by the model, not hardcoded. Example: “I am adding error handling to the user auth and removing the old auth functions”
The instructions parameter provides crucial context for ambiguous edits, helping the apply model make correct decisions and achieve near 100% accuracy even in edge cases.
2

Merge with Morph Fast Apply

When the agent calls your tool, send the original file + the edit snippet to Morph’s API. It returns the merged file. Write it to disk.
3

Handle the Response

The merged code is in response.choices[0].message.content. Write it to the target file.
4

Verifying Edits (Optional but Recommended)

Pass the diff back to the agent so it can verify the changes match its intent. To save tokens, you can limit this to cases where the linter reports errors.
This catches unexpected changes before they hit disk.

Direct Usage

Use without an agent:

Code-in/Code-out (Sandbox Support)

Use applyEdit when you manage your own filesystem or work in sandboxes like E2B, Modal, or Daytona:
applyEdit returns mergedCode instead of writing to disk. Use it in sandbox environments where you control file I/O.

Configuration

API

Input (EditFileInput):
Returns (EditFileResult):
All types are exported from the SDK root:

Edge / Cloudflare Workers

Use @morphllm/morphsdk/edge for edge environments (Cloudflare Workers, Vercel Edge, Deno):
The edge entry point has zero Node.js dependencies. It exports applyEdit, generateUdiff, countChanges, and callMorphAPI.

When to Use

Use Fast Apply when:
  • Your agent edits existing files (the primary use case)
  • Batching multiple edits to the same file in one call
  • Running in CI pipelines where token cost and latency matter
  • Working in sandboxed environments (E2B, Modal, Daytona) via applyEdit
Don’t use Fast Apply when:
  • Creating new files. Write them directly, there’s nothing to merge.
  • The entire file needs rewriting (rare). Full generation is simpler.
  • Editing non-code files like images or binaries.

Error Handling