Skip to content

Setup

You need:

Pi itself needs no account. The model is yours, through any Effect AI provider, and so is the sandbox platform, when the harness uses one.

Install the harness and the shared AI package:

Terminal window
pnpm add https://pkg.alchemy.run/@alchemy.run/pi/pr:3743:56b4873 https://pkg.alchemy.run/@alchemy.run/ai/pr:3743:56b4873

A harness needs at least one model. Workers AI needs nothing beyond your Cloudflare account:

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

See Models for other providers.

Yield the harness in a Durable Object’s outer Effect and provide its Layers once:

src/Session.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Pi from "@alchemy.run/pi";
import * as PiCloudflare from "@alchemy.run/pi/Cloudflare";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { Fast, FastLive } from "./models.ts";
const HarnessLive = Layer.mergeAll(FastLive, PiCloudflare.DurableObjectStorage);
export default class Session extends Cloudflare.DurableObject<Session>()(
"Session",
Effect.gen(function* () {
const harness = yield* Pi.Harness({ models: [Fast] });
return Effect.gen(function* () {
return {
prompt: Effect.fn(function* (name: string, text: string) {
const conversation = yield* harness.createConversation({ name });
const submission = yield* conversation.submit({ type: "input", content: text });
return (yield* submission.wait).status;
}),
};
});
}).pipe(Effect.provide(HarnessLive)),
) {}

Bind the Durable Object to a Worker by yielding Session in the Worker’s own outer Effect. See Durable Objects.

A harness that uses the coding tools needs a sandbox platform: E2B, Daytona, Modal, Boat or Archil. Add its provider to the Stack and sign in to it with your profile. See Sandboxes.

Next: the tutorial.