Skip to main content
Prompt for your coding agent
Read https://docs.morphllm.com/api-reference/endpoint/apply and integrate the Morph Apply API into our agent. Search for where our agent's edit tool is defined and route every edit larger than one line through this endpoint (model morph-v3-fast), keeping search-and-replace for single-line changes. The agent sends instructions, the original code, and a lazy edit snippet; Morph returns the merged file. Plan the integration first, then implement and verify with a multi-line edit.

Overview

The Apply API enables lightning-fast code editing at 10,500+ tokens/second with 98% accuracy. This OpenAI-compatible endpoint intelligently merges code changes while preserving structure and formatting.

Models

Choose the model that best fits your use case:

Message Format

The Apply API uses a structured XML format within the message content:
<instruction>Brief description of what you're changing</instruction>
<code>Original code content</code>
<update>Code snippet showing only the changes with // ... existing code ... markers</update>

Format Guidelines

  • <instruction>: Optional but recommended. Use first-person, clear descriptions
  • <code>: The complete original code that needs modification
  • <update>: Show only what changes, using // ... existing code ... for unchanged sections

Usage Examples

import OpenAI from "openai";

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

const instruction = "I will add error handling to prevent division by zero";
const originalCode = "function divide(a, b) {\n  return a / b;\n}";
const codeEdit = "function divide(a, b) {\n  if (b === 0) {\n    throw new Error('Cannot divide by zero');\n  }\n  return a / b;\n}";

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

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

Error Codes

edit_file Tool Guide

Build AI agent tools with Morph Apply

More Examples

See more implementation patterns