OpenCode
OpenCode is an open-source coding agent. It keeps sessions, each a transcript in its database. For each answer it asks a model, runs the tools the model calls, and asks again until the model is done. OpenCode 2 runs inside a Cloudflare Durable Object: its database is the object’s SQLite, and it needs no machine of its own.
Take one small app: a Coder Durable Object with a session pr-142. OpenCode runs in the object. Its model requests are answered by the Fast model’s Layer, and its call to your deploy tool runs the tool’s handler, both in the same isolate. The bash tool needs a shell, which a Durable Object does not have, so it runs its command in the session’s sandbox on Boat.
- holds
- files · shell
Install
Section titled “Install”pnpm add https://pkg.alchemy.run/@alchemy.run/opencode/9c58f00 https://pkg.alchemy.run/@alchemy.run/ai/9c58f00bun add https://pkg.alchemy.run/@alchemy.run/opencode/9c58f00 https://pkg.alchemy.run/@alchemy.run/ai/9c58f00npm install https://pkg.alchemy.run/@alchemy.run/opencode/9c58f00 https://pkg.alchemy.run/@alchemy.run/ai/9c58f00yarn add https://pkg.alchemy.run/@alchemy.run/opencode/9c58f00 https://pkg.alchemy.run/@alchemy.run/ai/9c58f00OpenCode needs no account. It is a dependency of @alchemy.run/opencode, pinned to the version it was tested with, and bundled into your Worker. The coding tools need a sandbox platform; their commands use only bash, grep and base64, so nothing of OpenCode is installed in the sandbox.
Run processes on OpenCode
Section titled “Run processes on OpenCode”An AI.Process names no harness. OpenCode.HarnessLive provides AI.Harness and needs OpenCode’s storage. Provide it where the process’s other Layers are:
import * as AI from "@alchemy.run/ai";import * as OpenCode from "@alchemy.run/opencode";import * as OpenCodeCloudflare from "@alchemy.run/opencode/Cloudflare";import * as Layer from "effect/Layer";import { EngineerLive } from "./Engineer.process.ts";import { SmartLlama, Smart } from "./Models.ts";
export const Live = EngineerLive.pipe( Layer.provide( Layer.mergeAll(OpenCode.HarnessLive, OpenCode.CodingTools, AI.DefaultModel(Smart)), ), Layer.provide(Layer.mergeAll(OpenCodeCloudflare.DurableObjectStorage, SmartLlama)),);OpenCode.CodingTools is only needed when a process mentions a coding tool, and then so is an AI.Sandboxes Layer.
A session is the OpenCode session named <process>/<key>. Every message is a prompt that carries the process’s prose as its instructions and the mentioned tools as the only tools allowed, so a deploy that changes a process changes its running sessions from their next message. send stores the prompt and returns, prompt waits for the session to go idle and gives the text of the answer, and messages is OpenCode’s transcript. A session keeps the model it was started on until a message names another.
OpenCode names a tool it did not ship after where it came from. A process that mentions your Deploy reads alchemy_deploy in its prose, and one that mentions AI.Bash reads bash, OpenCode’s own. An MCP server’s tools are <server>_<tool>.
Yield the harness yourself
Section titled “Yield the harness yourself”What OpenCode has beyond processes, such as its agents, per-prompt tools and its own client, is on OpenCode.Harness. Yield it in a Durable Object’s outer Effect and list the models and tools it has:
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 } 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, requestId: string) { const reply = yield* harness.session(name).prompt(text, { requestId }); return reply.text; }), }; }); }).pipe(Effect.provide(HarnessLive)),) {}OpenCodeCloudflare.DurableObjectStorage runs OpenCode in the Durable Object, with its sessions in the object’s SQLite under tables prefixed opencode_, apart from your own. OpenCode starts on a session’s first use, which takes a few tens of milliseconds, and closes after thirty seconds idle so the object can leave memory.
Sessions and prompts
Section titled “Sessions and prompts”harness.session(name) is the session with that name. prompt sends a message and waits for the answer, through every tool call OpenCode makes on the way:
const reply = yield* harness.session("pr-142").prompt("Why does the login test fail?");return reply.text;reply.messages is what the prompt added to the transcript, as OpenCode stores it, and session.messages is the whole transcript, oldest first.
A prompt uses the harness’s first model and every listed tool unless it says otherwise. tools is exact: the model gets these and nothing else, including OpenCode’s own tools. The second argument of session sets these defaults for every prompt of that session:
const reply = yield* session.prompt("Run the tests and fix what fails.", { model: Smart, tools: [AI.Bash, AI.Read, AI.Edit, RunTests], instructions: "Use pnpm, never npm.",});A requestId makes a prompt exactly-once: a second prompt with the same id waits for the first and returns its answer, after the object restarts too. agent picks one of OpenCode’s own agents, such as build or plan. abort stops the prompt that is running.
Prompts take minutes
Section titled “Prompts take minutes”A request that must answer quickly, such as a webhook, can’t wait for a prompt. submit gives the session a prompt and returns once it is stored, and wait returns when the session has nothing left to run:
yield* session.submit("Fix the failing test.");yield* session.wait;While a prompt runs the harness keeps the Durable Object’s alarm armed. OpenCode marks a prompt before it runs it, so an object evicted midway is started again by the alarm and the prompt resumes within about half a minute.
Model requests
Section titled “Model requests”OpenCode holds no API key. It is given one model provider, named after the harness (alchemy by default), whose address does not resolve. The harness is the fetch OpenCode uses, so it takes each request for that address and answers it with the AI.Model Layer of the model the prompt chose, in the same isolate. Logging, caching, rate limits and spend caps on your AI Gateway apply to OpenCode’s requests, because they are your runtime’s requests.
Your tools are registered with OpenCode and run in the Durable Object, where their bindings are. The model sees each as alchemy_<name>, and the harness’s name option changes the prefix. A tool not on the harness fails with NotOnHarness.
OpenCode.CodingTools provides each of the six coding tool names as a tool that works in the session’s sandbox, so the model sees alchemy_bash. Each has its own Layer (OpenCode.Bash, OpenCode.Read, …), and AI.Bash.make(...) in place of OpenCode.Bash replaces one with your own.
OpenCode.WebFetch, OpenCode.WebSearch and OpenCode.Subagent are OpenCode’s own. They are listed as they are and need no Layer:
const harness = yield* OpenCode.Harness({ models: [Fast], tools: [AI.CodingTools, OpenCode.WebFetch],});An MCP server listed in tools is connected from the Durable Object with your client Layer. To have OpenCode connect to a server itself, add it to OpenCode’s own config.
Code mode
Section titled “Code mode”Your tools and MCP servers can go through code mode. OpenCode’s own tools run inside OpenCode and stay direct calls. The harness adds the declarations of the tools a program may call to the prompt’s system prompt, and the model runs programs with alchemy_codemode:
const HarnessLive = Layer.mergeAll( FastLive, AddLive, WithdrawLive, OpenCode.CodingTools, OpenCodeCloudflare.DurableObjectStorage, AI.CodeModeAsync.pipe(Layer.provide(AICloudflare.EvalWorkerLoader())),).pipe(Layer.provideMerge(SandboxLive));
const harness = yield* OpenCode.Harness({ models: [Fast], tools: [AI.CodingTools, AI.CodeMode(Add, Withdraw)],});A program does not resume after a crash, under any evaluator. A prompt cut off while one runs fails.
OpenCode’s own code mode
Section titled “OpenCode’s own code mode”OpenCode.CodeModeNative uses OpenCode’s built-in execute tool in place of a convention and an AI.Eval. OpenCode writes the declarations, so a program calls your tools under the harness’s name:
const { sum } = await tools.alchemy.add({ a: 2, b: 3 });return sum;Programs are async only, and a declared failure rejects with an Error whose message is the failure’s fields as JSON. Every tool of yours a prompt allows goes through programs, the coding tools included, so a prompt that lists one directly beside a group fails.
The session’s sandbox
Section titled “The session’s sandbox”A session’s sandbox opens the first time one of its tools uses AI.Sandbox. A prompt that calls only tools of yours, or none, opens nothing, and a harness whose tools use neither files nor a shell takes no AI.Sandboxes at all. The session’s transcript is never in the sandbox: it is in the object’s SQLite.
The sandbox’s key is made from the Durable Object’s id and the session’s name; session.sandboxKey is that key. cwd on the harness sets the directory tools run in, such as a repository your image already has.
yield* harness.session("pr-142").suspendSandbox;yield* harness.session("pr-142").removeSandbox;suspendSandbox stops the sandbox and keeps its disk, where the platform can, and the next tool call resumes it. removeSandbox deletes it, and the session’s next tool call that needs one starts a new, empty one.
OpenCode’s own API
Section titled “OpenCode’s own API”harness.use hands your function OpenCode’s own typed client, the one @opencode/sdk gives, for diffs, reverts, forks and the rest. The call stays in the isolate: OpenCode opens no port.
const sessionID = yield* harness.session("pr-142").id;const diff = yield* harness.use((client) => client.session.diff({ sessionID }));config is OpenCode’s own configuration, the contents of an opencode.json, merged under what the harness sets:
const harness = yield* OpenCode.Harness({ models: [Fast], tools: [AI.CodingTools], config: { agent: { review: { prompt: "You review diffs. You never edit files." } }, },});
const reply = yield* harness.session("pr-142").prompt("Review the last commit.", { agent: "review" });The harness always sets the model provider, and it turns off OpenCode’s auto-update and session sharing.