Skip to main content
Morph Quickstart

Fast Apply

Fast Apply is a tool you give your agent. Instead of writing the full file or issuing search-and-replace commands, your agent outputs a lazy edit snippet (just the changed lines, with // ... existing code ... markers for everything else). The Morph API merges that snippet into the original file at 10,500 tok/s with 98% accuracy. This is the same approach Cursor uses. If you’ve used Cursor’s apply, you already know the UX.

Setup

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 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

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.
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: "YOUR_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

The merged code is in response.choices[0].message.content. Write it to the target file.
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)

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.
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 catches unexpected changes before they hit disk.

WarpGrep: Code Search Subagent

WarpGrep searches your codebase in its own isolated context window, finds the right code in 3.8 steps, and returns only the precise file/line-range spans your model needs. Your agent’s context stays clean.

Try WarpGrep in the Playground

Test code search with live examples
1

Add a search tool to your agent

import { MorphClient } from '@morphllm/morphsdk';

const morph = new MorphClient({ apiKey: "YOUR_API_KEY" });

const result = await morph.warpGrep.execute({
  query: 'Find authentication middleware',
  repoRoot: '.'
});
2

Use the results

if (result.success) {
  for (const ctx of result.contexts) {
    console.log(`File: ${ctx.file}`);
    console.log(ctx.content);
  }
}
WarpGrep returns ranked code spans with file paths and line numbers, ready to inject into your agent’s context. The intermediate search steps never touch your main model’s context window.
Requires ripgrep (rg) installed locally. No embeddings or index setup needed.

Next Steps