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.
Tool Description
Use this tool to make an edit to an existing file.

This will be read by a less intelligent model, which will quickly apply the edit. You should make it clear what the edit is, while also minimizing the unchanged code you write.
When writing the edit, you should specify each edit in sequence, with the special comment // ... existing code ... to represent unchanged code in between edited lines.

For example:

// ... existing code ...
FIRST_EDIT
// ... existing code ...
SECOND_EDIT
// ... existing code ...
THIRD_EDIT
// ... existing code ...

You should still bias towards repeating as few lines of the original file as possible to convey the change.
But, each edit should contain minimally sufficient context of unchanged lines around the code you're editing to resolve ambiguity.
DO NOT omit spans of pre-existing code (or comments) without using the // ... existing code ... comment to indicate its absence. If you omit the existing code comment, the model may inadvertently delete these lines.
If you plan on deleting a section, you must provide context before and after to delete it. If the initial code is ```code \n Block 1 \n Block 2 \n Block 3 \n code```, and you want to remove Block 2, you would output ```// ... existing code ... \n Block 1 \n  Block 3 \n // ... existing code ...```.
Make sure it is clear what the edit should be, and where it should be applied.
Make edits to a file in a single edit_file call instead of multiple edit_file calls to the same file. The apply model can handle many distinct edits at once.
Parameters:
  • target_file (string, required): The target file to modify
  • instructions (string, required): A single sentence written in the first person describing what you’re changing. Used to help disambiguate uncertainty in the edit.
  • code_edit (string, required): Specify ONLY the precise lines of code that you wish to edit. Use // ... existing code ... for unchanged sections.
The instructions field should be generated by your AI model, not user input. Follow the tool description above nearly verbatim - terminology like “use it to disambiguate uncertainty in the edit” should be used. Example: “I am adding error handling to the user authentication function”
2

Merge with Morph Fast Apply

Your tool’s execution should use Morph’s API to merge the code, as well as write the code to a file.
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-large",
  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);

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