// ... 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
- Anthropic
- OpenAI
- Vercel AI SDK
- MorphClient
- Python
- cURL
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 Parameters:
edit_file tool to your agent. Use one of the formats below.- General Prompt
- JSON Tool (Claude)
- Output Parsing (No Tool)
Why do I need the instructions to be generated by the model?
Why do I need the instructions to be generated by the model?
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.
What to add to your System Prompt
What to add to your System Prompt
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:- TypeScript
- Python
- cURL
Code-in/Code-out (Sandbox Support)
UseapplyEdit when you manage your own filesystem or work in sandboxes like E2B, Modal, or Daytona:
- applyEdit Function
- MorphClient Method
- Python
- cURL
applyEdit returns mergedCode instead of writing to disk. Use it in sandbox environments where you control file I/O.Configuration
API
- execute (file-based)
- applyEdit (code-based)
Input (Returns (
EditFileInput):EditFileResult):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
- 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.