# How Developer Prompts Generate Code Changes in SWE-Agent: The Complete Guide to create_diff_prompt and implement_diff

> Learn how SWE-Agent's developer prompts create_diff_prompt and implement_diff generate accurate code changes using a two step LLM extraction and application workflow.

- Repository: [LangTalks/swe-agent](https://github.com/langtalks/swe-agent)
- Tags: how-to-guide
- Published: 2026-03-05

---

**Developer prompts in SWE-Agent generate code changes through a structured two-step workflow where [`create_diff_prompt.md`](https://github.com/langtalks/swe-agent/blob/main/create_diff_prompt.md) extracts precise diff requests from the LLM, and [`implement_diff.md`](https://github.com/langtalks/swe-agent/blob/main/implement_diff.md) applies those edits to source files with line-level accuracy.**

The `langtalks/swe-agent` repository automates software engineering tasks by converting natural language instructions into concrete code modifications. Understanding how developer prompts generate code changes reveals the mechanism behind the agent's ability to read, plan, and edit files autonomously.

## The Two-Step Workflow for Code Generation

The developer agent orchestrates code modifications through a pipeline defined in [`agent/developer/graph.py`](https://github.com/langtalks/swe-agent/blob/main/agent/developer/graph.py). This workflow separates the planning phase from the execution phase, ensuring the LLM first identifies what to change before applying edits.

### Step 1: Extracting Diff Requests with create_diff_prompt

The [`create_diff_prompt.md`](https://github.com/langtalks/swe-agent/blob/main/create_diff_prompt.md) template (loaded as `extract_diffs_tasks_prompt` on line 18 of [`graph.py`](https://github.com/langtalks/swe-agent/blob/main/graph.py)) receives the current file content with prefixed line numbers, the atomic task description, and research context. It instructs the LLM to output one or more `<code_change_request>` blocks containing:

- **original_code_snippet**: The existing code lines with their line numbers
- **edit_code_snippet**: The replacement code to insert

This structured output ensures the agent can parse exactly which lines to replace without ambiguity.

### Step 2: Implementing Edits with implement_diff

The [`implement_diff.md`](https://github.com/langtalks/swe-agent/blob/main/implement_diff.md) prompt (loaded as `implement_diffs_prompt` on line 19) wraps the `edit_according_to_diff_runnable` (line 24). While the current implementation primarily uses the output from step 1 directly, this second prompt provides an extension point for complex transformations that require additional LLM reasoning before finalizing the edit.

## How create_diff_prompt.md Structures Code Changes

The prompt template in [`agent/developer/prompts/create_diff_prompt.md`](https://github.com/langtalks/swe-agent/blob/main/agent/developer/prompts/create_diff_prompt.md) enforces strict XML formatting to ensure machine-parseable output. When the agent invokes `extract_diff_runnable.invoke()` (as seen in the `creating_diffs_for_task` function, lines 45-62), it passes:

- `file_path`: Target file location
- `file_content`: Lines prefixed with `│` and line numbers
- `task`: The atomic development task
- `additional_context`: Supporting information
- `research`: Prior investigation results

The LLM responds with blocks like:

```xml
<code_change_request>
original_code_snippet:
1│ def old_function(x):
2│     return x * 2

edit_code_snippet:
1│ def new_function(x):
2│     return x * 2
3│     # Enhanced documentation

</code_change_request>

```

This format allows the parser in [`graph.py`](https://github.com/langtalks/swe-agent/blob/main/graph.py) (lines 62-74) to extract the line range and replacement text using regular expressions.

## The Implementation Pipeline in graph.py

The [`agent/developer/graph.py`](https://github.com/langtalks/swe-agent/blob/main/agent/developer/graph.py) file orchestrates the entire code generation process through a state machine workflow. Understanding this pipeline clarifies how developer prompts translate into file system modifications.

### Preparation Phase

The `prepare_for_implementation` node reads the target file content or creates a placeholder for new files. It stores the raw content in the agent state for subsequent processing.

### Diff Generation and Application

The `creating_diffs_for_task` function (lines 45-62) executes the core logic:

1. **Invocation**: Calls `extract_diff_runnable.invoke()` with the prepared file content and task context
2. **Parsing**: Uses regex to find all `<code_change_request>` blocks (lines 62-66)
3. **Extraction**: Captures `original_code_snippet` and `edit_code_snippet` from each block (lines 70-74)
4. **Line Calculation**: Parses the first and last line numbers from the original snippet
5. **Replacement**: Splits the file into lines, replaces the slice `[first_line-1:last_line]` with the edited code lines (lines 84-88)
6. **Persistence**: Writes the modified content back to disk (lines 89-91)

The `edit_according_to_diff_runnable` (line 24) remains available for future extensions where the raw diff requires additional LLM processing before application.

## Practical Code Examples

### Invoking the Diff Extraction Prompt Directly

You can interact with the developer prompts programmatically using the helper utilities:

```python
from helpers.prompts import markdown_to_prompt_template
from langchain_anthropic import ChatAnthropic
from langchain_core.output_parsers import StrOutputParser

# Load the prompt template

create_diff_prompt = markdown_to_prompt_template(
    "agent/developer/prompts/create_diff_prompt.md"
)

# Build the runnable

extract_diff = create_diff_prompt | ChatAnthropic(model="claude-sonnet-4-20250514") | StrOutputParser()

# Example inputs

result = extract_diff.invoke({
    "task": "Rename function `old_name` to `new_name`",
    "additional_context": "",
    "research": [],
    "file_path": "my_module.py",
    "file_content": "1| def old_name(x):\n2|     return x*2\n3| \n",
})
print(result)   # → contains <code_change_request> blocks

```

### Applying a Generated Diff

The internal logic used by `creating_diffs_for_task` demonstrates how to parse and apply the structured output:

```python
import re
import os

def apply_diff(file_path: str, diff_output: str):
    # Extract blocks

    blocks = re.findall(r"<code_change_request>(.*?)</code_change_request>", diff_output, re.DOTALL)

    for block in blocks:
        m = re.search(r"original_code_snippet:\s*(.*?)\s*edit_code_snippet:\s*(.*)", block, re.DOTALL)
        if not m:
            continue
        original = m.group(1).strip()
        edited   = m.group(2).strip()

        # Determine line range

        orig_lines = original.splitlines()
        first = int(orig_lines[0].split("|")[0].strip())
        last  = int(orig_lines[-1].split("|")[0].strip())

        # Load current file

        with open(file_path, "r") as f:
            content = f.read().splitlines()

        # Replace slice

        new_content = (
            content[: first - 1] + edited.splitlines() + content[last :]
        )

        # Write back

        with open(file_path, "w") as f:
            f.write("\n".join(new_content))

```

## Summary

- **Developer prompts generate code changes** in SWE-Agent through a structured two-step workflow defined in [`agent/developer/graph.py`](https://github.com/langtalks/swe-agent/blob/main/agent/developer/graph.py).
- **[`create_diff_prompt.md`](https://github.com/langtalks/swe-agent/blob/main/create_diff_prompt.md)** extracts precise modification requests by asking the LLM to output `<code_change_request>` blocks containing original and edited code snippets with line numbers.
- **[`implement_diff.md`](https://github.com/langtalks/swe-agent/blob/main/implement_diff.md)** provides a secondary processing layer for complex transformations, though the current implementation applies simple diffs directly after extraction.
- **The execution pipeline** parses LLM output using regular expressions, calculates line ranges from the numbered snippets, and performs slice-based replacement before writing changes back to disk.

## Frequently Asked Questions

### What is the difference between create_diff_prompt and implement_diff in SWE-Agent?

The `create_diff_prompt` focuses on **extraction**—it receives file content and task descriptions, then asks the LLM to identify specific changes by outputting structured `<code_change_request>` blocks with original and edited snippets. The `implement_diff` prompt focuses on **execution**—it takes an existing change request and can perform additional transformations or refinements before applying the edit, though the current workflow primarily uses the output from step one directly.

### How does SWE-Agent parse the LLM output to apply code changes?

SWE-Agent uses regular expressions in [`agent/developer/graph.py`](https://github.com/langtalks/swe-agent/blob/main/agent/developer/graph.py) to parse the LLM's structured response. It first extracts all `<code_change_request>` blocks, then captures the `original_code_snippet` and `edit_code_snippet` sections within each block. The system parses the line numbers from the first and last lines of the original snippet, calculates the slice indices, replaces those lines with the edited content, and writes the modified file back to disk.

### Where are the developer prompt templates stored in the SWE-Agent repository?

The developer prompt templates are located in the `agent/developer/prompts/` directory. Specifically, [`create_diff_prompt.md`](https://github.com/langtalks/swe-agent/blob/main/create_diff_prompt.md) handles the extraction of code change requests, while [`implement_diff.md`](https://github.com/langtalks/swe-agent/blob/main/implement_diff.md) manages the implementation phase. These markdown files are loaded into LangChain prompt templates via the `markdown_to_prompt_template` helper defined in [`helpers/prompts.py`](https://github.com/langtalks/swe-agent/blob/main/helpers/prompts.py).

### Can I customize the developer prompts to change how code changes are generated?

Yes, you can customize the developer prompts by editing the markdown files in `agent/developer/prompts/`. The [`create_diff_prompt.md`](https://github.com/langtalks/swe-agent/blob/main/create_diff_prompt.md) file controls how the LLM structures its output, so modifying the instructions or XML schema within this file changes the format of the `<code_change_request>` blocks. Similarly, adjusting [`implement_diff.md`](https://github.com/langtalks/swe-agent/blob/main/implement_diff.md) allows you to alter how the system processes complex transformations before applying edits to the codebase.