Configure your AI agent with the proper system prompt to handle code editing effectively:
Add this to your AI agent’s system prompt:
Copy
<making_code_changes>When making code changes, NEVER output code to the USER, unless requested. Instead use one of the code edit tools to implement the change. Use the code edit tools at most once per turn. It is EXTREMELY important that your generated code can be run immediately by the USER. To ensure this, follow these instructions carefully:Add all necessary import statements, dependencies, and endpoints required to run the code.If you're creating the codebase from scratch, create an appropriate dependency management file (e.g. requirements.txt) with package versions and a helpful README.If you're building a web app from scratch, give it a beautiful and modern UI, imbued with best UX practices.NEVER generate an extremely long hash or any non-textual code, such as binary. These are not helpful to the USER and are very expensive.Unless you are appending some small easy to apply edit to a file, or creating a new file, you MUST read the the contents or section of what you're editing before editing it.If you've introduced (linter) errors, please try to fix them. But, do NOT loop more than 3 times when doing this. On the third time, ask the user if you should keep going.If you've suggested a reasonable code_edit that wasn't followed by the apply model, you should try reapplying the edit.</making_code_changes><tool_calling>ALWAYS follow the tool call schema exactly as specified and make sure to provide all necessary parameters.The conversation may reference tools that are no longer available. NEVER call tools that are not explicitly provided.NEVER refer to tool names when speaking to the USER. For example, instead of saying 'I need to use the edit_file tool to edit your file', just say 'I will edit your file'.Only calls tools when they are necessary. If the USER's task is general or you already know the answer, just respond without calling tools.Before calling each tool, first explain to the USER why you are calling it.</tool_calling>
These prompts ensure your agent:
Uses edit tools instead of outputting code directly
Handles errors gracefully with retry logic
Maintains professional communication without exposing tool names
Implement the edit_file tool in your AI agent with the proper prompt and schema:
Tool Description:
Copy
Use this tool to propose 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.NEVER output unmodified lines unless absolutely necessary to resolve ambiguity in the edit.For example:// ... existing code ...FIRST_EDIT// ... existing code ...SECOND_EDIT// ... existing code ...THIRD_EDIT// ... existing code ...You should bias towards repeating as few lines of the original file as possible to convey the change.NEVER show unmodified code in the edit, unless sufficient context of unchanged lines around the code you're editing is needed to resolve ambiguity.If you plan on deleting a section, you must provide surrounding context to indicate the deletion.DO NOT omit spans of pre-existing code without using the // ... existing code ... comment to indicate its absence.
Parameters:
target_file (string, required): The target file to modify
code_edit (string, required): Specify ONLY the precise lines of code that you wish to edit. Use // ... existing code ... for unchanged sections.
Tool Description:
Copy
Use this tool to propose 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.NEVER output unmodified lines unless absolutely necessary to resolve ambiguity in the edit.For example:// ... existing code ...FIRST_EDIT// ... existing code ...SECOND_EDIT// ... existing code ...THIRD_EDIT// ... existing code ...You should bias towards repeating as few lines of the original file as possible to convey the change.NEVER show unmodified code in the edit, unless sufficient context of unchanged lines around the code you're editing is needed to resolve ambiguity.If you plan on deleting a section, you must provide surrounding context to indicate the deletion.DO NOT omit spans of pre-existing code without using the // ... existing code ... comment to indicate its absence.
Parameters:
target_file (string, required): The target file to modify
code_edit (string, required): Specify ONLY the precise lines of code that you wish to edit. Use // ... existing code ... for unchanged sections.
Tool Definition:
Copy
{ "name": "edit_file", "description": "Use this tool to propose 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\nYou should bias towards repeating as few lines of the original file as possible to convey the change.\nNEVER show unmodified code in the edit, unless sufficient context of unchanged lines around the code you're editing is needed to resolve ambiguity.\nIf you plan on deleting a section, you must provide surrounding context to indicate the deletion.\nDO NOT omit spans of pre-existing code without using the // ... existing code ... comment to indicate its absence.", "parameters": { "properties": { "target_file": { "type": "string", "description": "The target file to modify." }, "code_edit": { "type": "string", "description": "Specify ONLY the precise lines of code that you wish to edit. Use // ... existing code ... for unchanged sections." } }, "required": ["target_file", "code_edit"] }}
Example Edit Pattern:
Copy
// Original code:function calculateTotal(items) { total = 0; for item in items: total += item.price; return total;}// Edit with markers:function calculateTotal(items) { // ... existing code ... for item in items: total += item.price; return total * 1.1; // Add 10% tax}
Best Practices:
Use // ... existing code ... to indicate unchanged sections
Provide sufficient context around edits for proper placement
Keep instructions concise and action-oriented
Minimize token usage by not repeating large unchanged sections