Catalog / Cloudflare Developer Platform
Agents, Workers - Run the Pi Durable harness on Cloudflare with the Agents SDK
The Agents SDK now provides first-class support for building agents using the Pi harness. You can build long-running agents using the combination of Pi 1.0 ↗︎, Pi Durable ↗︎, and the new PiHarness class that the Cloudflare Agents SDK provides, ensuring your agent's work is durably persisted, even if interrupted mid-turn.
Built with Earendil ↗︎, this integration is our first step toward first-class support for third-party agent harnesses on Cloudflare.
Copy promptPrompt copied!Beta
PiHarness is in beta. Pi Durable ↗︎ is a new, experimental package, and the PiHarness API will likely change as Pi Durable matures.
PiHarness is a new "Lifecycle capability" provided by the Cloudflare Agents SDK. Pi Durable provides the agent harness and the Lifecycle is responsible for keeping the agent running in the Durable Object. The Lifecycle is a core concept in the Agents SDK ensuring that long-running work can run in a Durable Object, surviving restarts, crashes, and network issues. We will share more on Lifecycle capabilities in the near future.
Install
npmyarnpnpmbunnpm i agents@latest @earendil-works/pi-durable @earendil-works/pi-aiyarn add agents@latest @earendil-works/pi-durable @earendil-works/pi-aipnpm add agents@latest @earendil-works/pi-durable @earendil-works/pi-aibun add agents@latest @earendil-works/pi-durable @earendil-works/pi-ai
Both Pi packages are optional peer dependencies of agents, so you only install them if you use the harness.
Use it in an Agent
Creating a Pi agent requires configuring the Pi Harness with a model, skills, and tools, then registering the PiHarness with the Agent class.
import { Agent } from "agents";
import { createModels } from "@earendil-works/pi-ai/models";
import { createRegistry, Harness } from "@earendil-works/pi-durable";
import { PiHarness } from "agents/harness/pi";
import { createAI } from "agents/models/pi-ai";
export class Assistant extends Agent {
ai = createAI({ binding: this.env.AI });
registry = createRegistry();
harness = new PiHarness({
harness: ({ storage, context }) => {
const models = createModels();
models.setProvider(this.ai.provider);
return Harness.open(
storage,
{ models, registry: this.registry },
context,
);
},
defaults: { model: this.ai("@cf/moonshotai/kimi-k2.7-code") },
});
constructor(ctx, env) {
super(ctx, env);
this.lifecycle.use(this.harness);
}
async ask(prompt) {
const { text } = await this.harness.prompt(prompt);
return text;
}
}import { Agent } from "agents";
import { createModels } from "@earendil-works/pi-ai/models";
import { createRegistry, Harness } from "@earendil-works/pi-durable";
import { PiHarness } from "agents/harness/pi";
import { createAI } from "agents/models/pi-ai";
export class Assistant extends Agent<Env> {
ai = createAI({ binding: this.env.AI });
registry = createRegistry();
harness = new PiHarness({
harness: ({ storage, context }) => {
const models = createModels();
models.setProvider(this.ai.provider);
return Harness.open(
storage,
{ models, registry: this.registry },
context,
);
},
defaults: { model: this.ai("@cf/moonshotai/kimi-k2.7-code") },
});
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
this.lifecycle.use(this.harness);
}
async ask(prompt: string) {
const { text } = await this.harness.prompt(prompt);
return text;
}
}
The agents/models/pi-ai entry point supports AI Gateway and Workers AI models, so you can get started with Cloudflare models right away or use your existing Pi AI provider.
Add tools with extensions
Both tools and system prompt sections are provided to the Pi Harness via extensions.
import { Type } from "@earendil-works/pi-ai";
import { skills } from "agents/harness/pi";
const WordCount = Type.Object({ text: Type.String() });
const wordCount = {
name: "word_count",
description: "Count the words in a text.",
parameters: WordCount,
replay: "safe",
async execute({ text }) {
const words = text.split(/\s+/).filter(Boolean).length;
return { content: [{ type: "text", text: String(words) }] };
},
};
// In the harness factory, before Harness.open():
registry.install({
name: "editor",
sections: [
{ key: "preamble", render: () => "You are an editor.", tag: false },
],
tools: [wordCount],
});
registry.install(await skills(sources));import { Type } from "@earendil-works/pi-ai";
import type { ToolRegistration } from "@earendil-works/pi-durable";
import { skills } from "agents/harness/pi";
const WordCount = Type.Object({ text: Type.String() });
const wordCount: ToolRegistration<typeof WordCount> = {
name: "word_count",
description: "Count the words in a text.",
parameters: WordCount,
replay: "safe",
async execute({ text }) {
const words = text.split(/\s+/).filter(Boolean).length;
return { content: [{ type: "text", text: String(words) }] };
},
};
// In the harness factory, before Harness.open():
registry.install({
name: "editor",
sections: [
{ key: "preamble", render: () => "You are an editor.", tag: false },
],
tools: [wordCount],
});
registry.install(await skills(sources));
For more information on creating and configuring extensions, refer to Extensions.
Learn more
- Pi harness documentation
- Pi harness extensions
- pi-ai model provider
- Pi harness example ↗︎, with WebSockets, a browser UI, and a
@cloudflare/computerWorkspace for the model's tools - Lifecycle ↗︎
- Pi Durable announcement ↗︎ from Earendil