Prerequisites: Sign up for a Morph account and create an API key.

Try the Playground

Test Morph’s lightning-fast code merging in seconds

① Generate Code Snippets

Configure your LLM to output abbreviated edit snippets instead of full file rewrites:

Add similar instructions to your LLM system prompt:

Use this tool to propose an edit to an existing file.

This will be read by a less intelligent "apply" 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 changes.
But, each edit should contain 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.
ALWAYS make all edits to a file in a single edit_file instead of multiple edit_file calls to the same file. The apply model can handle many distinct edits at once.

Example Edit Snippet:

// ... existing code ...
function calculateTotal(items) {
  // ... existing code ...
  for item in items:
    total += item.price;
  return total * 1.1;  // Add 10% tax
}
// ... existing code ...

const total, setTotal = useState(0);
useEffect(() => {
  setTotal(calculateTotal(items));
}, [items]);

// ... existing code ...
return <div>Total: {total}</div>;

② Merge with Morph Fast Apply

Pass the abbreviated edit snippet along with the initial code to the OpenAI-compatible Morph Apply endpoint:

API Base URL:

https://api.morphllm.com/v1
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: `<code>${initialCode}</code>\n<update>${editSnippet}</update>`
    }
  ]
});

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

③ Parse Final Code from Response

Collect the merged code from the response:

final_code = response.choices[0].message.content

Why Morph?

Morph’s apply model offers significant advantages over search/replace or full file rewriting:

For access to our longer context models, self-hosting, or business inquiries, please contact us at info@morphllm.com.