The Code Apply API provides a direct interface for applying code edits using the Morph model. This endpoint intelligently merges code changes at 10,500+ tokens/second with 99.2% accuracy, designed specifically for AI agents and development tools.Unlike the chat-based API, this endpoint accepts structured parameters directly, making it easier to integrate into automated workflows and development environments.
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_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.
Tool Definition
{ "name": "edit_file", "description": "Use this tool to make an edit to an existing file.\n\nThis 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.\nWhen writing the edit, you should specify each edit in sequence, with the special comment // ... existing code ... to represent unchanged code in between edited lines.\n\nFor example:\n\n// ... existing code ...\nFIRST_EDIT\n// ... existing code ...\nSECOND_EDIT\n// ... existing code ...\nTHIRD_EDIT\n// ... existing code ...\n\nYou should still bias towards repeating as few lines of the original file as possible to convey the change.\nBut, each edit should contain minimally sufficient context of unchanged lines around the code you're editing to resolve ambiguity.\nDO 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.\nIf 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 ...```.\nMake sure it is clear what the edit should be, and where it should be applied.\nMake 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.", "input_schema": { "type": "object", "properties": { "target_file": { "type": "string", "description": "Name or path of target file to modify." }, "instructions": { "type": "string", "description": "A single sentence instruction describing what you are going to do for the sketched edit. This is used to assist the less intelligent model in applying the edit. Use the first person to describe what you are going to do. Use it to disambiguate uncertainty in the edit." }, "code_edit": { "type": "string", "description": "Specify ONLY the precise lines of code that you wish to edit. NEVER specify or write out unchanged code. Instead, represent all unchanged code using the comment of the language you're editing in - example: // ... existing code ..." } }, "required": ["target_file", "instructions", "code_edit"] }}
Instead of using tool calls, you can have the agent output code edits in markdown format that you can parse:
Agent Instruction
Use this approach to make edits to existing files by outputting code edits in a specific markdown format.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 response instead of multiple responses to the same file. The apply model can handle many distinct edits at once.When you want to edit a file, output your code edits using this markdown format:```filepath=path/to/file.js instruction=A single sentence describing what you're changing// ... existing code ...YOUR_CODE_EDIT_HERE// ... existing code ...```The instruction should be written in the first person describing what you're changing. Used to help disambiguate uncertainty in the edit.
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”
2
2. Call the API
Send the original code and edit snippet to the Code Apply endpoint: