# Understanding the Role of AI in Open-SWE: Architecture and Implementation

> Discover how AI powers Open-SWE. Learn about its architecture and implementation, using large language models to automate the software engineering lifecycle for efficient development.

- Repository: [LangChain/open-swe](https://github.com/langchain-ai/open-swe)
- Tags: architecture
- Published: 2026-03-19

---

**Open-SWE leverages large language models as its core orchestration engine to automate the entire software engineering lifecycle, from context gathering and sandboxed code editing to automated pull request creation and team notifications.**

Open-SWE, developed by `langchain-ai/open-swe`, represents a paradigm shift in how AI integrates into software development workflows. Rather than functioning as a simple code completion tool, the AI serves as the central decision-making brain that interprets high-level tickets, coordinates sandboxed execution, and delivers production-ready pull requests.

## Core Architecture: AI as the Orchestration Engine

The AI component in Open-SWE functions as the primary orchestration layer, transforming natural language requirements into deterministic software engineering actions.

### LLM Model Selection and Initialization

The AI engine begins with model instantiation through `make_model()` in [`agent/utils/model.py`](https://github.com/langchain-ai/open-swe/blob/main/agent/utils/model.py). This helper function initializes a language model—typically Claude-Opus—and injects provider-specific options before passing it to the Deep Agents Pregel graph. The model produces the next execution step, decides which tool to invoke, and builds the system prompt that guides its behavior throughout the task lifecycle.

### Agent Construction with Deep Agents

The agent itself is constructed using the Deep Agents framework, which underpins LangGraph. In [`agent/server.py`](https://github.com/langchain-ai/open-swe/blob/main/agent/server.py), the `get_agent()` function calls `create_deep_agent()` to wire together the model, system prompt, curated toolset, backend sandbox, and middleware layers. This integration ensures the AI has secure access to file systems and execution environments while maintaining deterministic safety controls.

## Prompt-Driven Behavior and System Context

### System Prompt Construction

The AI's behavior is governed by `construct_system_prompt()` in [`agent/prompt.py`](https://github.com/langchain-ai/open-swe/blob/main/agent/prompt.py). This function assembles the system prompt that informs the model about its execution environment, available tools, and operational policies. The prompt effectively serves as the "brain" that makes the LLM behave like a disciplined coding assistant, constraining it to sandboxed operations and specific workflow patterns.

## Sub-Agent Orchestration and Parallel Execution

### The Task Tool for Sub-Agents

Open-SWE implements sophisticated sub-agent orchestration through the `task` tool, enabling the AI to spawn child agents for parallel work on independent subtasks. For example, the primary agent can delegate documentation generation to one sub-agent while simultaneously running tests through another. This pattern, described in the repository's architecture documentation, allows the AI to parallelize complex software engineering workflows efficiently.

## Curated Toolset and Safety Middleware

### AI-Controlled Tool Selection

The AI operates through a carefully curated set of tools exposed via the Deep Agents framework. These include shell execution, URL fetching, GitHub PR creation, and integrations with Linear and Slack. The AI autonomously decides **when** and **how** to invoke each tool, transforming natural language instructions into concrete actions while maintaining strict operational boundaries.

### Deterministic Safety Middleware

Safety mechanisms operate through deterministic middleware layers defined in [`agent/server.py`](https://github.com/langchain-ai/open-swe/blob/main/agent/server.py). The `ToolErrorMiddleware` handles execution failures gracefully, while `open_pr_if_needed` guarantees that a pull request is created even if the AI model forgets to invoke the PR tool explicitly. These middleware layers ensure that AI operations remain safe, deterministic, and aligned with software engineering best practices.

## Implementation Examples

### Creating an Agent Instance

To instantiate the AI agent for processing a Linear issue or Slack thread:

```python
from langchain.prompts import ChatPromptTemplate
from langchain.schema.runnable import RunnableConfig
from agent.server import get_agent

# Configuration from workflow trigger

config: RunnableConfig = {
    "configurable": {
        "thread_id": "abc123",
        "repo": {"owner": "langchain-ai", "name": "open-swe"},
        "linear_issue": {"linear_project_id": "proj_123", "linear_issue_number": "42"},
    },
    "metadata": {},
}

# Initialize fully-wired Pregel agent

agent = await get_agent(config)

# Execute task

result = await agent.ainvoke(
    {"messages": [{"role": "user", "content": "Add a utility to list all .py files"}]}
)

```

### Tool Execution Flow

When the AI decides to explore the codebase, it generates tool calls such as:

```json
{
  "tool": "execute",
  "args": {
    "command": "ls **/*.py",
    "timeout": 30
  }
}

```

The `execute` tool runs inside the sandbox environment ([`agent/utils/sandbox.py`](https://github.com/langchain-ai/open-swe/blob/main/agent/utils/sandbox.py)) and returns results that inform the AI's next decision.

### Automated PR Creation

Upon completing edits, the AI invokes the PR tool:

```python
{
  "tool": "commit_and_open_pr",
  "args": {
    "branch": "feature/list-py-files",
    "commit_message": "Add utility to list .py files",
    "pr_title": "Add .py listing utility",
    "pr_body": "This PR introduces a small CLI helper that lists all Python files in the repo."
  }
}

```

The `open_pr_if_needed` middleware ensures this step executes even if the AI omits the tool call.

## Summary

- **AI serves as the core orchestration engine** in Open-SWE, transforming natural language tickets into complete software engineering workflows.
- **Model initialization** occurs through `make_model()` in [`agent/utils/model.py`](https://github.com/langchain-ai/open-swe/blob/main/agent/utils/model.py), feeding into the Deep Agents Pregel graph.
- **Agent construction** happens in [`agent/server.py`](https://github.com/langchain-ai/open-swe/blob/main/agent/server.py) via `get_agent()` and `create_deep_agent()`, integrating the LLM, prompts, tools, and sandbox.
- **Prompt engineering** in [`agent/prompt.py`](https://github.com/langchain-ai/open-swe/blob/main/agent/prompt.py) constrains the AI to safe, deterministic behaviors appropriate for production codebases.
- **Sub-agent orchestration** enables parallel execution of independent tasks through the `task` tool.
- **Safety middleware** such as `ToolErrorMiddleware` and `open_pr_if_needed` ensures reliable, sandboxed execution.

## Frequently Asked Questions

### What is Open-SWE?

Open-SWE is an open-source AI-driven software engineering agent developed by LangChain. It automates the complete lifecycle of coding tasks—from interpreting Linear or Slack tickets to editing files in an isolated sandbox and opening pull requests on GitHub.

### How does Open-SWE use AI to write code?

The AI functions as the decision-making brain through the Deep Agents framework. It uses an LLM (typically Claude-Opus) instantiated via `make_model()` to interpret requirements, select appropriate tools from a curated set, and generate code edits. The system prompt in [`agent/prompt.py`](https://github.com/langchain-ai/open-swe/blob/main/agent/prompt.py) guides the model to behave like a disciplined engineer, ensuring it follows sandbox policies and workflow conventions.

### What safety mechanisms prevent the AI from making harmful changes?

Open-SWE implements multiple safety layers: **sandboxed execution** ensures all shell commands run in isolated environments; **deterministic middleware** like `ToolErrorMiddleware` handles failures gracefully; and **guarantee mechanisms** such as `open_pr_if_needed` ensure critical actions (like creating PRs) occur even if the AI omits them. These controls prevent unauthorized access and ensure production safety.

### Can Open-SWE work with multiple repositories or teams?

Yes, Open-SWE supports multi-repository and multi-team workflows through configuration mappings. The [`agent/utils/linear_team_repo_map.py`](https://github.com/langchain-ai/open-swe/blob/main/agent/utils/linear_team_repo_map.py) file maps Linear teams and projects to specific GitHub repositories, while the `get_agent()` function in [`agent/server.py`](https://github.com/langchain-ai/open-swe/blob/main/agent/server.py) accepts configurable thread IDs, repository metadata, and Linear issue details. This allows the AI agent to context-switch between different codebases and team workflows seamlessly.