mech.app

The mech.app newsletter

Agentic AI, minus the noise.

Get practical field notes on AI agents, automation, developer tools and security delivered to your inbox.

No spam. Unsubscribe anytime.

Dev Tools

Open SWE's Sandbox Architecture: How LangChain Built a Production-Ready Coding Agent

Cloud sandboxes, Slack invocation, subagent orchestration, and automatic PR creation. The plumbing behind a coding agent safe enough for production.

Source: github.com
Open SWE's Sandbox Architecture: How LangChain Built a Production-Ready Coding Agent

LangChain just open-sourced the architecture that Stripe, Ramp, and Coinbase built internally: a coding agent that accepts work from Slack and Linear, executes code in isolated cloud sandboxes, and opens pull requests without human handholding. Open SWE (10,498 stars, trending #12 on GitHub Python) is not a toy demo. It is a blueprint for production agent infrastructure with explicit security boundaries, multi-channel invocation, and subagent orchestration patterns.

This is the plumbing that makes a coding agent safe and useful in a real organization. The architecture choices matter more than the LLM calls.

Why Cloud Sandboxes Are Not Optional

Open SWE runs agent-generated code in ephemeral cloud environments, not on the orchestrator host. This is the first decision that separates production agents from prototypes.

Security boundaries:

  • The orchestrator (LangGraph runtime) never executes untrusted code directly.
  • Each task spawns a fresh sandbox with a cloned repository and limited network access.
  • The sandbox runtime cannot read orchestrator secrets or access internal APIs without explicit credential injection.
  • If the agent hallucinates a rm -rf /, it destroys the sandbox, not your CI server.

Isolation shape:

  • Open SWE uses Modal or E2B as the sandbox provider. Both offer API-driven container lifecycle management.
  • The orchestrator sends a task payload (repository URL, branch, instructions) to the sandbox API.
  • The sandbox clones the repo, runs the agent’s code edits, executes tests, and returns a diff.
  • The orchestrator receives the diff and decides whether to open a PR or request human review.

This is a unidirectional data flow. The sandbox cannot call back into the orchestrator or access its state. The orchestrator polls for completion or listens to a webhook.

Failure modes:

  • Sandbox timeout: the orchestrator marks the task as failed and logs the partial output.
  • Sandbox OOM: the provider kills the container, the orchestrator retries with a smaller context window.
  • Network partition: the orchestrator waits for the sandbox health check to fail, then spawns a replacement.

The key insight is that the orchestrator treats the sandbox as a black box. It does not trust the sandbox to enforce policy. It trusts the sandbox provider to enforce resource limits and network isolation.

Slack and Linear Invocation Without Exposing Credentials

Open SWE accepts work from Slack slash commands and Linear issue webhooks. This is harder than it sounds. You cannot give the agent your Slack bot token and hope for the best.

Invocation layer architecture:

  1. A Slack user types /openswe fix the login bug.
  2. Slack sends a POST to your webhook endpoint with a signed payload.
  3. The webhook handler verifies the signature, extracts the user ID and channel ID, and checks an allowlist.
  4. If the user is authorized, the handler creates a LangGraph thread with the task description and user context.
  5. The orchestrator spawns a sandbox, runs the agent, and posts progress updates to the original Slack thread.
  6. When the agent finishes, it opens a PR and posts the link to Slack.

Credential scoping:

  • The Slack bot token lives in the orchestrator’s environment, not in the sandbox.
  • The agent cannot read the token or call Slack APIs directly.
  • The orchestrator exposes a tool interface: post_message(channel, text) and upload_file(channel, path).
  • The tool implementation checks that the channel ID matches the original invocation context.

This is a capability-based security model. The agent gets a function that posts to a specific channel, not a token that posts to any channel.

Linear integration:

  • Linear sends a webhook when an issue is labeled openswe.
  • The webhook handler extracts the issue description, repository link, and assignee.
  • The orchestrator creates a LangGraph thread with the issue context and spawns a sandbox.
  • When the agent finishes, it posts a comment on the Linear issue with the PR link.

The Linear API token is also scoped. The agent cannot read arbitrary issues or update unrelated projects. The orchestrator enforces this by checking the issue ID against the original webhook payload.

Subagent Orchestration in LangGraph

Open SWE uses a main agent that delegates to specialized subagents: a code editor, a test runner, a documentation writer. This is not a simple function call. It is a state machine with handoffs and backpressure.

Orchestration flow:

  1. The main agent receives a task: “Add OAuth support to the login endpoint.”
  2. It spawns a code editor subagent with the task description and current file tree.
  3. The code editor returns a diff.
  4. The main agent spawns a test runner subagent with the diff and test suite path.
  5. The test runner returns pass/fail and error logs.
  6. If tests fail, the main agent sends the error logs back to the code editor and requests a fix.
  7. If tests pass, the main agent spawns a documentation writer subagent to update the README.
  8. The documentation writer returns a new README diff.
  9. The main agent combines all diffs and opens a PR.

State passing:

  • Each subagent is a LangGraph node with its own state schema.
  • The main agent serializes the current state (file tree, diffs, test results) and passes it to the subagent as input.
  • The subagent returns a new state object, which the main agent merges into the global state.
  • LangGraph handles state persistence and checkpointing. If the orchestrator crashes, it resumes from the last checkpoint.

Preventing runaway delegation:

  • The main agent has a delegation budget: maximum 5 subagent calls per task.
  • Each subagent call increments a counter in the global state.
  • If the counter exceeds the budget, the main agent stops delegating and returns a partial result.
  • The orchestrator logs the delegation trace for debugging.

This is not a recursive agent that can spawn infinite children. It is a fixed-depth tree with explicit backpressure.

Tool call visibility:

  • Each subagent exposes a set of tools: read_file, write_file, run_command, search_codebase.
  • The main agent does not call these tools directly. It delegates to a subagent that has the tool in its toolkit.
  • The orchestrator logs every tool call with the subagent ID, tool name, and arguments.
  • This gives you a complete audit trail of what the agent did and which subagent did it.

Automatic PR Creation and Review Gates

Open SWE opens pull requests automatically, but it does not merge them automatically. This is the right default for production.

PR creation flow:

  1. The agent finishes editing code and running tests.
  2. The orchestrator calls the GitHub API to create a branch with the agent’s commits.
  3. The orchestrator opens a PR with a generated title and description.
  4. The PR description includes the original task, the agent’s reasoning, and links to the Slack thread or Linear issue.
  5. The orchestrator posts the PR link to Slack and Linear.

Review gates:

  • The PR is marked as a draft by default.
  • A human must review the code and mark it ready for review.
  • The orchestrator does not request reviewers automatically. You configure a CODEOWNERS file or a review policy in GitHub.
  • The PR cannot merge until it passes CI and gets at least one approval.

This is a trust-but-verify model. The agent does the work, but a human makes the final decision.

Failure recovery:

  • If the agent’s code does not compile, the orchestrator posts the error logs to Slack and asks the user to clarify the task.
  • If the agent’s tests fail, the orchestrator retries with a smaller scope (e.g., fix one function instead of the entire module).
  • If the agent times out, the orchestrator saves the partial diff and posts it to Slack for manual completion.

The orchestrator never silently fails. It always posts a status update to the original invocation channel.

Deployment Shape and Observability

Open SWE is designed to run on a single server with a persistent LangGraph state store. This is not a distributed system. It is a monolith with external sandboxes.

Deployment components:

ComponentRoleFailure Mode
OrchestratorRuns LangGraph, handles webhooks, manages stateSingle point of failure. Deploy with a process manager (systemd, Docker Compose).
State StorePostgres or SQLite for LangGraph checkpointsData loss if not backed up. Use managed Postgres for production.
Sandbox ProviderModal or E2B for code executionRate limits or quota exhaustion. Configure fallback provider.
Slack/LinearInvocation channelsWebhook delivery failures. Implement retry with exponential backoff.
GitHubPR creation and code storageAPI rate limits. Cache repository metadata locally.

Observability hooks:

  • Every LangGraph node emits a structured log with the node name, input state, and output state.
  • The orchestrator sends these logs to a centralized logging service (e.g., Datadog, Loki).
  • You can query logs by task ID, user ID, or subagent ID.
  • The orchestrator exposes a /metrics endpoint with Prometheus metrics: task count, success rate, sandbox duration, LLM token usage.

Cost tracking:

  • Each task logs the total LLM tokens consumed (input and output).
  • Each sandbox call logs the runtime duration and memory usage.
  • You can aggregate these metrics to estimate cost per task or cost per user.

The orchestrator does not implement distributed tracing. If you need cross-service correlation, you must inject trace IDs manually.

When to Use Open SWE and When to Avoid It

Use Open SWE when:

  • You want a coding agent that accepts work from Slack or Linear, not just a CLI.
  • You need isolated code execution with explicit security boundaries.
  • You are comfortable running a stateful orchestrator on a single server.
  • You want to customize the agent’s tools and subagent logic without rewriting the entire framework.

Avoid Open SWE when:

  • You need a distributed agent system with horizontal scaling. Open SWE is a monolith.
  • You want the agent to merge PRs automatically without human review. Open SWE enforces a review gate.
  • You need sub-second response times. Sandbox startup adds 5-10 seconds per task.
  • You want a no-code agent builder. Open SWE requires Python and LangGraph knowledge.

The architecture is opinionated. It assumes you want safety over speed, and human oversight over full automation. If you want a different trade-off, you will need to fork the codebase.

Technical Verdict

Open SWE is the first open-source coding agent that shows you the production plumbing: sandboxes, invocation layers, subagent orchestration, and PR gates. It is not a research prototype. It is a deployable system with real security boundaries.

The architecture choices are sound. Cloud sandboxes prevent the agent from destroying your infrastructure. Scoped credentials prevent it from leaking secrets. Subagent budgets prevent runaway delegation. Review gates prevent it from merging bad code.

The deployment shape is simple: one server, one state store, external sandboxes. This is the right default for most teams. If you need distributed orchestration, you will outgrow Open SWE quickly, but you will have learned the patterns.

The code is readable. The LangGraph state machines are explicit. The tool interfaces are well-defined. You can fork this and customize it without reverse-engineering a black box.

If you are building an internal coding agent, start here. You will save months of architecture decisions.