Skip to main content
Morph Fast Apply Quickstart

Overview

What is Morph for? Morph Fast Apply looks like a new edit_file tool you give your agent access to. That’s it. Claude will output lazily into this tool when it wants to make an edit. In the tools execution, the Morph API will merge the lazy edit output by Claude/Gemini/etc. into the file. If you like using Cursor - you already like the Fast Apply UX. Fast Apply is a concept used in Cursor.

How to use Morph Fast Apply

Try the API Playground

Test the Apply Model with live examples in our interactive playground
1

1. Add an edit_file tool to your agent

Add the edit_file tool to your agent. Use one of the formats below.
  • General Prompt
  • JSON Tool (Claude)
  • Output Parsing (No Tool)
Tool Description
Use this tool to edit existing files by showing only the changed lines.

Use "// ... existing code ..." to represent unchanged code blocks. Include just enough surrounding context to locate each edit precisely.

Example format:
// ... existing code ...
FIRST_EDIT
// ... existing code ...
SECOND_EDIT
// ... existing code ...

Rules:
- ALWAYS use "// ... existing code ..." for unchanged sections (omitting this marker will cause deletions)
- Include minimal context ONLY when needed around edits for disambiguation
- Preserve exact indentation
- For deletions: show context before and after, omit the deleted lines
- Batch multiple edits to the same file in one call
Parameters:
  • target_filepath (string, required): Path of the file to modify
  • instructions (string, required): Brief first-person description of what you’re changing (helps disambiguate uncertainty in the edit)
  • code_edit (string, required): Only the changed lines with // ... existing code ... markers for unchanged sections
IMPORTANT: 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”
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

Your tool’s execution should use Morph’s API to merge the code. Then you should write the code to a file.
Add this to your system prompt to enable efficient code editing:
When editing code, use the edit_file tool to show only changed lines. Use "// ... existing code ..." markers for unchanged sections.

Example:
// ... existing code ...
{{ edit_1 }}
// ... existing code ...
{{ edit_2 }}
// ... existing code ...

Key points:
- Only rewrite entire files if explicitly requested
- ALWAYS use "// ... existing code ..." markers (omitting them causes deletions)
- Include minimal context for precise edit location
- Provide brief explanations unless user requests code only
import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: process.env.MORPH_API_KEY,
  baseURL: "https://api.morphllm.com/v1",
});

const response = await openai.chat.completions.create({
  model="morph-v3-fast",
  messages: [
    {
      role: "user",
      content: `<instruction>${instructions}</instruction>\n<code>${initialCode}</code>\n<update>${codeEdit}</update>`,
    },
  ],
});

const mergedCode = response.choices[0].message.content;
3

Handle the Response

Extract the merged code from the API response. Use your filesystem to write the code to a file.Response Format:
final_code = response.choices[0].message.content
Extract the Final Code:
const finalCode = response.choices[0].message.content;
// Write to file or return to your application
await fs.writeFile(targetFile, finalCode);
4

Verifying Edits (Optional but Recommended)

We recommend passing the code changes back to the agent in UDiff format. This allows the agent to verify that the changes match its intent and make any necessary corrections. To save on tokens, another option is to check for linting errors and only pass the calculated udiff back when there are linting errors.
import { createTwoFilesPatch } from 'diff';

// Generate UDiff between original and modified code
const udiff = createTwoFilesPatch(
  targetFile, 
  targetFile,
  initialCode,
  mergedCode,
  '', 
  ''
);

// Send back to agent for verification
console.log("Changes applied:", udiff);
This verification step helps catch any unexpected changes and ensures the applied edits match the agent’s intentions.

Next Steps

Ready to start building with Morph? Here’s what to do next:

Explore the Apply API

Learn about the Apply API endpoints for production use

Build Agentic Tools

Create edit_file tools for AI agents and development environments