OpenCode
OpenCode is an open-source coding agent: it keeps sessions, runs a loop of model requests and tool calls, and stores each transcript. OpenCode 2 runs inside a Cloudflare Durable Object, on the object’s own SQLite. Alchemy starts it there, answers its model requests with your models, and registers your tools with it, all in the same isolate. A sandbox is only where tools that need files or a shell do their work. Models, tools and sandboxes are the ones you declare with @alchemy.run/ai.
New to OpenCode? Start with What is OpenCode?.
A quick example
Section titled “A quick example”A model, and where the coding tools find files and a shell:
import * as AI from "@alchemy.run/ai";import * as AICloudflare from "@alchemy.run/ai/Cloudflare";import * as AIBoat from "@alchemy.run/ai/Boat";import * as Boat from "@alchemy.run/boat";import * as Cloudflare from "@alchemy.run/cloudflare";
export class Fast extends AI.Model<Fast>()("fast") {}
export const FastLive = AICloudflare.Model(Fast, { model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",});
export const Sandbox = AIBoat.Sandboxes( Boat.Snapshot("Base", { run: ["npm install --global pnpm"] }),);A Durable Object yields the harness and prompts a session by name:
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 { Fast, FastLive, Sandbox } from "./agent.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, requestId: string) { const reply = yield* harness.session(name).prompt(text, { requestId }); return reply.text; }), }; }); }).pipe(Effect.provide(HarnessLive)),) {}Where to start
Section titled “Where to start”Want to build? Follow Setup, then the tutorial: a bot that answers issues on a GitHub repository.
Looking for something specific? Every section of these docs is listed below. Models, tools, MCP servers, sandboxes and processes are shared by every harness, and are covered in the AI docs.