# How Developer Prompts Handle New File Creation vs Existing File Modifications in SWE-Agent

> Discover how SWE-Agent developer prompts differentiate between creating new files and modifying existing ones using distinct templates for efficient code generation and modification.

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

---

**The SWE-Agent developer agent uses `os.path.exists()` to branch between two distinct prompt templates—[`implement_new_file.md`](https://github.com/langtalks/swe-agent/blob/main/implement_new_file.md) for generating full source code when the file does not exist, and [`implement_diff.md`](https://github.com/langtalks/swe-agent/blob/main/implement_diff.md) for producing JSON-encoded diffs when modifying existing files.**

The `langtalks/swe-agent` repository implements a software engineering agent that automates code changes through specialized developer prompts. Understanding how these prompts distinguish between creating new files and modifying existing ones is crucial for extending the agent's capabilities or debugging its behavior. The system employs a clear branching strategy within the workflow graph to handle these two scenarios with different LLM prompts and file operations.

## The Decision Node: Detecting File Existence

The branching logic resides in the `creating_diffs_for_task` function within [`agent/developer/graph.py`](https://github.com/langtalks/swe-agent/blob/main/agent/developer/graph.py). This node evaluates whether the target file already exists on disk before determining which prompt strategy to invoke.

The decision relies on a simple filesystem check:

- If `os.path.exists(file_path)` returns `False`, the agent follows the **new file creation** path
- If `os.path.exists(file_path)` returns `True`, the agent follows the **existing file modification** path

This binary branch determines whether the agent will generate a complete file from scratch or calculate a precise diff against existing content.

## Creating New Files: The implement_new_file Prompt

When the target file does not exist, the agent invokes the `create_new_file_runnable` using the prompt template defined in [`agent/developer/prompts/implement_new_file.md`](https://github.com/langtalks/swe-agent/blob/main/agent/developer/prompts/implement_new_file.md).

The prompt template receives these input variables:

- `task`: The atomic implementation task
- `additional_context`: Supplementary context for the task
- `research`: The research scratch-pad from previous steps
- `file_path`: The target path for the new file

The LLM returns the complete source code as a string, which the agent writes directly to disk:

```python
new_file_content = create_new_file_runnable.invoke({
    "task": current_atomic_task.atomic_task,
    "additional_context": current_atomic_task.additional_context,
    "research": convert_tools_messages_to_ai_and_human(state.atomic_implementation_research),
    "file_path": file_path
})
with open(file_path, "w") as file:
    file.write(new_file_content)

```

## Modifying Existing Files: The implement_diff Prompt

For existing files, the agent uses the `edit_according_to_diff_runnable` with the [`agent/developer/prompts/implement_diff.md`](https://github.com/langtalks/swe-agent/blob/main/agent/developer/prompts/implement_diff.md) template. This approach requires reading the current file content and presenting it with line numbers to the LLM.

The process involves:

1. **Reading and numbering lines**: The `prepare_for_implementation` function in [`agent/developer/graph.py`](https://github.com/langtalks/swe-agent/blob/main/agent/developer/graph.py) reads the existing file and returns the content with line numbers prepended to each line.

2. **Invoking the diff prompt**: The agent calls the LLM with the numbered file content, the atomic task, research context, and output format instructions for a JSON-encoded diff.

3. **Parsing the response**: The LLM returns a `<code_change_request>` block containing JSON with the original snippet and the edited snippet.

4. **Applying the diff**: The agent extracts the line range from the original snippet, splices the new lines into the file content, and rewrites the file.

```python

# Existing file branch

diffs_tasks = extract_diff_runnable.invoke({
    "task": current_atomic_task.atomic_task,
    "additional_context": current_atomic_task.additional_context,
    "research": convert_tools_messages_to_ai_and_human(state.atomic_implementation_research),
    "file_path": file_path,
    "file_content": file_content,  # Line-numbered content

    "output_format": JsonOutputParser(pydantic_object=Diffs).get_format_instructions()
})

# Parse <code_change_request> and apply changes...

```

## Key Functions and State Management

Several core functions support the new file versus existing file distinction:

**`prepare_for_implementation`** (`agent/developer/graph.py:90-99`): This function checks file existence and returns either the actual file content (for existing files) or the placeholder string `"This is a new file"` (for new files). This prepares the context for downstream prompt selection.

**`SoftwareDeveloperState`** ([`agent/developer/state.py`](https://github.com/langtalks/swe-agent/blob/main/agent/developer/state.py)): The state object tracks the current task, file content, and research scratch-pad (`atomic_implementation_research`). This state persists across the workflow nodes, ensuring the correct context reaches the appropriate prompt template.

**`markdown_to_prompt_template`** ([`helpers/prompts.py`](https://github.com/langtalks/swe-agent/blob/main/helpers/prompts.py)): This utility loads the markdown prompt files ([`implement_new_file.md`](https://github.com/langtalks/swe-agent/blob/main/implement_new_file.md) and [`implement_diff.md`](https://github.com/langtalks/swe-agent/blob/main/implement_diff.md)) into LangChain templates, making them available to the runnables.

## Summary

- The SWE-Agent developer workflow uses `os.path.exists()` in `creating_diffs_for_task` to branch between new file creation and existing file modification.
- New files trigger the [`implement_new_file.md`](https://github.com/langtalks/swe-agent/blob/main/implement_new_file.md) prompt via `create_new_file_runnable`, generating complete source code written directly to disk.
- Existing files trigger the [`implement_diff.md`](https://github.com/langtalks/swe-agent/blob/main/implement_diff.md) prompt via `edit_according_to_diff_runnable`, requiring line-numbered content and returning JSON-encoded diffs that are spliced into the original file.
- The `prepare_for_implementation` function and `SoftwareDeveloperState` manage context and file content across these distinct paths.

## Frequently Asked Questions

### How does SWE-Agent determine whether to create a new file or edit an existing one?

The agent checks `os.path.exists(file_path)` in the `creating_diffs_for_task` node within [`agent/developer/graph.py`](https://github.com/langtalks/swe-agent/blob/main/agent/developer/graph.py). If the file does not exist, it follows the new file creation path; otherwise, it proceeds with the existing file modification workflow.

### What prompt template is used when creating a new file in SWE-Agent?

The agent uses the [`implement_new_file.md`](https://github.com/langtalks/swe-agent/blob/main/implement_new_file.md) prompt template located at [`agent/developer/prompts/implement_new_file.md`](https://github.com/langtalks/swe-agent/blob/main/agent/developer/prompts/implement_new_file.md). This template is invoked through the `create_new_file_runnable` and instructs the LLM to generate the complete source code for the specified `file_path`.

### How are changes applied to existing files in the SWE-Agent developer workflow?

For existing files, the agent reads the file content with line numbers prepended, invokes the [`implement_diff.md`](https://github.com/langtalks/swe-agent/blob/main/implement_diff.md) prompt via `edit_according_to_diff_runnable`, and receives a JSON-encoded diff wrapped in `<code_change_request>` tags. The agent then extracts the original and edited snippets, computes the line range, splices the new lines into the file content, and rewrites the file to disk.

### What role does the `prepare_for_implementation` function play in file operations?

The `prepare_for_implementation` function in [`agent/developer/graph.py`](https://github.com/langtalks/swe-agent/blob/main/agent/developer/graph.py) (lines 90-99) checks whether the target file exists. It returns the actual file content for existing files or the placeholder string `"This is a new file"` for new files, thereby preparing the appropriate context for the subsequent prompt selection and invocation.