Can I deploy OpenCode to Vercel with Sandbox?

devv1.1.18
c87939arelease: v1.1.18
This post might have stale content, as dev is 2006 commits ahead.

can i deploy opencode to vercel? if i use the opencode SDK alongside vercel sandbox? https://vercel.com/docs/vercel-sandbox

or what would be the recommended setup?

Avatar of anthropic/claude-opus-4.5
Claude Opus 4.5
commented

Now I have a complete picture. Let me summarize what I've found:


##Summary: Running OpenCode with Vercel Sandbox

Short answer: It's possible but requires significant setup work. Here's why:

###The Challenge

OpenCode is a full coding agent that requires:

  1. Bun runtime - OpenCode's server uses Bun.serve() (packages/opencode/src/server/server.ts:2873)
  2. File system access - Full read/write access for code editing, snapshots, LSP, etc.
  3. Shell/bash execution - The core bash tool spawns shell commands (packages/opencode/src/tool/bash.ts)
  4. LSP servers - For intelligent code features
  5. Persistent storage - Sessions, configuration, worktrees

###Vercel Sandbox Limitations

From the Vercel Sandbox docs:

  • Runtimes available: node22, node24, python3.13 - No Bun runtime
  • Base system: Amazon Linux 2023
  • You CAN: Install packages via dnf, run arbitrary commands with sudo, have file system access in /vercel/sandbox
  • Timeout: Max 5 hours (Pro/Enterprise) or 45 minutes (Hobby)

####Option 1: Run OpenCode in Vercel Sandbox (Complex)

You could install Bun inside the sandbox and run OpenCode:

import { Sandbox } from '@vercel/sandbox';
import ms from 'ms';

const sandbox = await Sandbox.create({
  runtime: 'node22',  // Use node as base
  resources: { vcpus: 4 },
  timeout: ms('1h'),
  ports: [4096],
});

// Install Bun
await sandbox.runCommand({
  cmd: 'curl',
  args: ['-fsSL', 'https://bun.sh/install', '|', 'bash'],
  sudo: true,
});

// Install OpenCode
await sandbox.runCommand({
  cmd: 'npm',
  args: ['i', '-g', 'opencode-ai'],
});

// Start OpenCode server
await sandbox.runCommand({
  cmd: 'opencode',
  args: ['serve', '--hostname=0.0.0.0', '--port=4096'],
  detached: true,
  env: {
    ANTHROPIC_API_KEY: '...',
    OPENCODE_SERVER_PASSWORD: '...',
  }
});

// Connect via SDK
const client = createOpencodeClient({
  baseUrl: sandbox.domain(4096),
});

####Option 2: Use the OpenCode SDK Differently (Simpler)

The OpenCode SDK (packages/sdk/js/src/server.ts) spawns the opencode binary locally. For a cloud setup, you'd:

  1. Deploy OpenCode on a VPS/container (not Vercel) - Use the Docker image
  2. Use the SDK client from Vercel Functions to connect to it:
import { createOpencodeClient } from '@opencode-ai/sdk';

// From Vercel Function or API route
const client = createOpencodeClient({
  baseUrl: 'https://your-opencode-server.example.com',
});

####Option 3: Self-hosted OpenCode + Vercel Frontend

OpenCode has a client/server architecture (mentioned in the README):

"A client/server architecture. This for example can allow OpenCode to run on your computer, while you can drive it remotely from a mobile app."

You could:

  1. Run OpenCode server on a dedicated VM (AWS EC2, DigitalOcean, Railway, Fly.io, etc.)
  2. Build a web frontend on Vercel that uses the OpenCode SDK client to communicate with your server

###What Would Actually Work Best

Given OpenCode's requirements, the recommended setup would be:

ComponentPlatform
OpenCode ServerRailway, Fly.io, or AWS/GCP/Azure container (supports Bun natively)
Web UI / API GatewayVercel (Next.js app using OpenCode SDK client)
Code sandboxThe OpenCode server handles this via its own tools

The Vercel Sandbox is designed for running untrusted code snippets, not long-running development servers. OpenCode already is a sandbox for AI-generated code with its own permission system and tools.

Would you like me to dig deeper into any of these options, or explore how to set up a specific deployment architecture?


END OF POST

Can I deploy OpenCode to Vercel with Sandbox? — anomalyco/opencode