Setup
You need:
- Node.js 24.11.1+ or Bun
- An Alchemy app on Cloudflare. See Cloudflare setup.
- For the coding tools, an account on a sandbox platform: Boat, E2B, Daytona, Modal or Archil
OpenCode needs no account. It is a dependency of the harness and is bundled into your Worker.
Install
Section titled “Install”Install the harness and the shared AI package, and your sandbox platform’s provider if you use the coding tools:
pnpm add https://pkg.alchemy.run/@alchemy.run/opencode/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/ai/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/boat/pr:3743:56b4873bun add https://pkg.alchemy.run/@alchemy.run/opencode/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/ai/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/boat/pr:3743:56b4873npm install https://pkg.alchemy.run/@alchemy.run/opencode/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/ai/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/boat/pr:3743:56b4873yarn add https://pkg.alchemy.run/@alchemy.run/opencode/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/ai/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/boat/pr:3743:56b4873Add the sandbox provider
Section titled “Add the sandbox provider”The coding tools work in a sandbox, so the Stack needs the platform’s provider next to Cloudflare’s:
import * as Boat from "@alchemy.run/boat";import * as Cloudflare from "@alchemy.run/cloudflare";import * as Alchemy from "alchemy";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";import Api from "./src/Api.ts";
export default Alchemy.Stack( "MyApp", { providers: Layer.mergeAll(Cloudflare.providers(), Boat.providers()), state: Cloudflare.state(), }, Effect.gen(function* () { const api = yield* Api; return { url: api.url }; }),);Sign in to the platform with your profile:
pnpm alchemy profile edit --add Boatbun alchemy profile edit --add Boatnpx alchemy profile edit --add Boatyarn alchemy profile edit --add BoatA model and sandboxes
Section titled “A model and sandboxes”A harness needs a model. Tools that use files or a shell also need an AI.Sandboxes Layer. See Models and Sandboxes.
A harness
Section titled “A harness”Yield the harness in a Durable Object’s outer Effect and provide its Layers once:
import * as AI from "@alchemy.run/ai";import * as Cloudflare from "@alchemy.run/cloudflare";import * as OpenCode from "@alchemy.run/opencode";import * as OpenCodeCloudflare from "@alchemy.run/opencode/Cloudflare";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";import { FastLive, Fast } from "./models.ts";import { Sandbox } from "./Sandbox.ts";
const HarnessLive = Layer.mergeAll( FastLive, OpenCode.CodingTools, OpenCodeCloudflare.DurableObjectStorage,).pipe(Layer.provideMerge(Sandbox));
export default class Coder extends Cloudflare.DurableObject<Coder>()( "Coder", Effect.gen(function* () { const harness = yield* OpenCode.Harness({ models: [Fast], tools: [AI.CodingTools] });
return Effect.gen(function* () { return { prompt: Effect.fn(function* (name: string, text: string) { const reply = yield* harness.session(name).prompt(text); return reply.text; }), }; }); }).pipe(Effect.provide(HarnessLive)),) {}OpenCodeCloudflare.DurableObjectStorage runs OpenCode in the Durable Object, with its sessions in the object’s SQLite. Layer.provideMerge(Sandbox) gives the sandboxes to the coding tools and to the harness.
What the sandbox image needs
Section titled “What the sandbox image needs”Nothing of OpenCode is installed in the sandbox. The coding tools run bash, grep and base64 there, which Boat’s own image and node:22-bookworm have.
Next: the tutorial.