Skip to content

AI

@alchemy.run/ai models agentic processes in code. A process is prose that describes a piece of work, and everything it mentions is a real reference: a tool, an LLM, another process, a command of your Domain model.

Processes, tools, your business rules and your infrastructure are one declarative program that the compiler checks and Alchemy deploys. The harness that runs a process, Pi or OpenCode, is one Layer.

New to this? Start with What is Alchemy AI?.

Name a model. Which LLM answers is the Layer behind the name:

src/models.ts
import * as AI from "@alchemy.run/ai";
import * as AICloudflare from "@alchemy.run/ai/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",
});

Declare a tool: what it takes, what it returns, and a handler:

src/Add.tool.ts
import * as AI from "@alchemy.run/ai";
import * as Schema from "effect/Schema";
export class Add extends AI.Tool<Add>()("add", {
description: "Add two integers.",
parameters: Schema.Struct({ a: Schema.Number, b: Schema.Number }),
success: Schema.Struct({ sum: Schema.Number }),
}) {}
export const AddLive = Add.make(({ a, b }) => ({ sum: a + b }));

Describe the work as a process: a name, and a Layer with its prose. Mentioning the tool gives it to the process:

src/Accountant.process.ts
import * as AI from "@alchemy.run/ai";
import { Add } from "./Add.tool.ts";
export class Accountant extends AI.Process<Accountant>()("Accountant", {}) {}
export const AccountantLive = Accountant.toLayer({
description: "Add up the numbers it is given.",
})`
Add the numbers you are given with ${Add}. Reply with only the total.
`;

Host it. The Layers say what a session runs on, here Pi, and where: each session in a Durable Object of its own.

src/Books.ts
import * as AI from "@alchemy.run/ai";
import * as AICloudflare from "@alchemy.run/ai/Cloudflare";
import * as Pi from "@alchemy.run/pi";
import * as PiCloudflare from "@alchemy.run/pi/Cloudflare";
import * as Layer from "effect/Layer";
import { AccountantLive } from "./Accountant.process.ts";
import { AddLive } from "./Add.tool.ts";
import { Fast, FastLive } from "./models.ts";
const Harness = Layer.mergeAll(Pi.HarnessLive, AI.DefaultModel(Fast), AddLive).pipe(
Layer.provide(Layer.mergeAll(PiCloudflare.DurableObjectStorage, FastLive)),
);
export const AccountantSessions = AICloudflare.ProcessSessions(
AccountantLive.pipe(Layer.provide(Harness)),
);

A Worker given AccountantSessions, or a policy of a Domain model, holds a session by its key:

const accountant = yield* Accountant;
const reply = yield* accountant.get("q3").prompt("12, 30 and 7");

Swap Pi.HarnessLive and its storage for OpenCode’s, and the same process runs there.

Want to build? Install the package and follow the tutorial: an issue board whose policies start processes, on any harness.

Terminal window
pnpm add https://pkg.alchemy.run/@alchemy.run/ai/9c58f00

Looking for something specific? Every section of these docs is listed below.