This is my current challenge, I’d appreciate your comment if you have any insights.
1 - Consider I have two agents that work together. The first one is responsible for generating the code for a website/app from a prompt. The second one is responsible for making changes to it.
2 - So far, the agent that does the coding creation works really well. Awesome results with GPT-5 on high reasoning + couple of tools like internet search.
3 - However, on the code editing part I’ve been struggling a lot, hence this cry for help…
4 - For context, at first it worked in the dumbest of ways possible: whenever a user asked for changes to his generated app/website, I’d take the entire code, send it to the LLM along with the changes and ask it to generate it back to me.
5 - I think we’ve all been there: the problem with this approach (like I think all of you know) is that the model is prone to making unsolicited change (as its regenarating everything from scratch), but also it is very slow and expensive. Sometimes what happens is the user just wants to change like a color or a piece of text, and the agent goes on to burn and burn tokens and takes like 2 to 3 minutes.
6 - So I decided I needed a different approach. Right now, here’s what I’ve done: I created a prompt that asks the model to just tell me which lines to delete, replace and/or add. In theory it should work, but for complex changes it doesn’t, at least not reliably.
The prompt is below (see if you can make sense of it):
string exampleHtml = $@"
<html>
<body>
<h1>Hello</h1>
<h2>My name is John Doe</h2>
</body>
</html>
";
var exampleReturnObject = new Patch
{
Modifications = new List<PatchLine>
{
new PatchLine
{
LineIndex = 2,
Content = "<h1>Howdy</h1>"
},
new PatchLine
{
LineIndex = 3,
Content = ""
}
},
Additions = new List<PatchLine>
{
new PatchLine
{
LineIndex = 4,
Content = "<p>"
},
new PatchLine
{
LineIndex = 5,
Content = " My name is John Doe"
},
new PatchLine
{
LineIndex = 6,
Content = "</p>"
}
},
};
var otherExampleReturnObject = new Patch
{
Modifications = new List<PatchLine>
{
new PatchLine
{
LineIndex = 2,
Content = "<h1>Howdy</h1>"
},
new PatchLine
{
LineIndex = 3,
Content = "<p>"
}
},
Additions = new List<PatchLine>
{
new PatchLine
{
LineIndex = 4,
Content = " My name is John Doe"
},
new PatchLine
{
LineIndex = 5,
Content = "</p>"
}
},
};
string exampleReturnJson = JsonConvert.SerializeObject(exampleReturnObject);
string otherExampleReturnJson = JsonConvert.SerializeObject(otherExampleReturnObject);
string instructions = $@"
# ROLE
- You are a coding editor that generates patch files.
# CONTEXT
- The HTML for the current code is broken into lines (by \n).
- Consider each line as a separate line of code, and to be represented by its index: first line = 0, second line = 1, etc.
# RETURN OBJECT
- You must return an object to be desserialized, as defined by the examples below.
<EXAMPLE 1>
## JSON
{exampleReturnJson}
## COMMENTS
- This is the expected return from the following change request: 'change the main header to Howdy and turn the h2 into a paragraph'.;
- Notice the line of the header (h1) is simply replaced.
- Notice the line with the h2 is erased.
- 3 new lines are added after the one that was erased.
- The final HTML is expected to be:
<html>
<body>
<h1>Howdy</h1>
<p>
My name is John Doe
</p>
</body>
</html>
</EXAMPLE 1>
<EXAMPLE 2>
## JSON
{otherExampleReturnJson}
## COMMENTS
- This example is the same as the previous one, only with a slight different approach.
- Instead of erasing line of index 3 and then adding the new lines starting at index 4, it recycled line 3 and replaced it with the first line of the new code.
- The final HTML is expected to be (notice the lack of an empty line at line index 3):
<html>
<body>
<h1>Howdy</h1>
<p>
My name is John Doe
</p>
</body>
</html>
</EXAMPLE 2>
# GUIDELINES
- You can't delete lines, you can only add new lines and/or modify existing lines.
- In the '{nameof(Patch.Modifications)}' property, you define the lines you want to change by their index and new value.
- If you want to erase code, you add the lines to the '{nameof(Patch.Modifications)}' property with an empty string.
# PATCHING ORDER
- With your instructions, our code is going to patch the existing HTML code in the following order: 1) Modifications, 2) Additions.
- This means you must first work the on the modifications, then on the additions.
- Consider the code that exists starting from the index of the first new line to be added will be bumped down.
# INSTRUCTIONS
1. Analyze the current code and the change request.
2. Create the patch object with SIMPLE, SMALL changes.
3. Call the {nameof(ValidateHtmlPatchTool)} to validate it.
4. If validation fails, simplify your approach - use smaller text patterns.
5. Return the patch object (validation errors for minor issues are acceptable).
# OUTPUT
- Return only the raw JSON. No conversation, no fences.
";
7 - So I’m wondering, what’s the go to approach for this? Do you have any tips?