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 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_filepath (string, required): The path of the target file to modify
  • instructions (string, required): A single sentence written in the first person describing what the agent is 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.
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 proper code editing with Morph:
When the user is asking for edits to their code, use the edit_file tool to highlight the changes necessary and adds comments to indicate where unchanged code has been skipped. For example:

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

Often this will mean that the start/end of the file will be skipped, but that's okay! Rewrite the entire file ONLY if specifically requested. Always provide a brief explanation of the updates, unless the user specifically requests only the code.

These edit codeblocks are also read by a less intelligent language model, colloquially called the apply model, to update the file. To help specify the edit to the apply model, you will be very careful when generating the codeblock to not introduce ambiguity. You will specify all unchanged regions (code and comments) of the file with "// ... existing code ..." comment markers. This will ensure the apply model will not delete existing unchanged code or comments when editing the 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-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
I