Build with AI
These docs are written to be read by coding agents as well as people. Paste one of the prompts below into Claude Code, Cursor, Codex, Copilot or any other agent, and it will find the pages it needs and wire up the SDK. Every prompt points the agent at Markdown versions of the docs, so it reads the real API instead of guessing.
Add cairnflow to a project
Section titled “Add cairnflow to a project”For an existing TypeScript service or a new one. The agent installs the SDK, writes a first
service with durable steps, exposes it with serve() or fetchHandler, and tells you how to
register and invoke it.
You are adding cairnflow to this TypeScript project. Cairnflow is a durable execution runtime:handlers run inside my own HTTP service, and cairnflow records every step in a journal, so a crashor a deploy resumes the run where it stopped instead of starting over or repeating side effects.
Read these first. They are Markdown pages written for agents:- https://cairnflow.dev/llms.txt (index of all docs)- https://cairnflow.dev/docs/quickstart.md- https://cairnflow.dev/docs/concepts.md- https://cairnflow.dev/docs/develop/durable-steps.md
Then:1. Install the SDK: `npm install @cairnflow/sdk` (Node.js 22.6 or newer; the SDK has no dependencies). Make sure `package.json` has `"type": "module"` if you run `.ts` files directly.2. Create one service module. Use `service(name, handlers)` for stateless handlers, `service(name, handlers, { type: "VIRTUAL_OBJECT" })` when state belongs to a key (a user, an order, a chat), and `workflow(name, { run, ... })` for a process that runs once per ID.3. Wrap every side effect (HTTP calls, database writes, payments, emails) in `ctx.run("<stable step name>", () => ...)`. Step names are part of the journal: do not rename them later and do not put configuration values in them.4. Expose the service: - on a long-running server: `serve({ services, port: Number(process.env.PORT ?? 9080), hostname: "0.0.0.0", identityKeys })` - on Vercel, Cloudflare Workers or AWS Lambda: `fetchHandler({ services, identityKeys })` from `@cairnflow/sdk/fetch` (see https://cairnflow.dev/docs/workflows/serverless.md)5. Read `CAIRN_URL` and `CAIRN_KEY` from the environment. Read `CAIRN_IDENTITY_KEY` too and pass `identityKeys: [process.env.CAIRN_IDENTITY_KEY]` when it is set; leave `identityKeys` out when it is not (a node signs its requests only when it is configured to). Never hard-code keys.6. Finish by showing me the two commands to run: register the deployment with `POST $CAIRN_URL/deployments` and body `{"uri": "<URL of the service>"}`, then invoke one handler with `POST $CAIRN_URL/invoke/<Service>/<handler>`, both with `authorization: Bearer $CAIRN_KEY`. The URL must be reachable from the cairnflow node: with a node on the same machine, `http://127.0.0.1:<port>` works.
Rules for all handler code:- No `Date.now()`, `Math.random()` or generated IDs outside `ctx.run`.- Do not catch and swallow errors around `ctx` commands; rethrow what you do not handle.- Throw `TerminalError` for failures a retry cannot fix (bad input, a refused payment).- Keep results, state and call inputs small JSON values. Put files and images in `ctx.artifact` and return the reference; keep data that later invocations need (such as a chat history) in state, not in an artifact.- Do not invent APIs. If something is not in the docs, ask me.Make a Vercel AI SDK agent durable
Section titled “Make a Vercel AI SDK agent durable”For a chat agent that already uses streamText and useChat. The agent keeps your tools,
prompts and UI, and moves the model and tool calls onto durable steps with approvals and a
reconnecting stream.
This project has a chat agent built with the Vercel AI SDK v7 (`streamText` on the server,`useChat` in the browser). Make it durable with cairnflow without rewriting it: every model calland tool call becomes a journaled step, tools can wait for human approval, and the streamreconnects after a page reload.
Read these first. They are Markdown pages written for agents:- https://cairnflow.dev/docs/agents/vercel-ai-sdk.md- https://cairnflow.dev/docs/agents/approvals.md- https://cairnflow.dev/docs/agents/streaming.md
Server:1. Install the SDK: `npm install @cairnflow/sdk` (Node.js 22.6 or newer; no runtime dependencies).2. Move the `streamText` call into `chatObject("Chat", async (ctx, turn) => streamText({ ... }))`.3. Wrap the model: `wrapLanguageModel({ model, middleware: cairnMiddleware(ctx) })`.4. Wrap the tools: `cairnTools(ctx, tools, { approve: [<names of tools that need a human>] })`.5. Pass `messages: await convertToModelMessages(turn.messages)` and `onError: turn.onError`.6. `chatObject` stops after 5 attempts by default, so a provider that keeps failing does not retry forever. To change it: `chatObject("Chat", run, { options: { retryPolicy: { maxAttempts: 3, onMaxAttempts: "PAUSE" } } })`.7. Serve it: `serve({ services: [chat], port: Number(process.env.PORT ?? 9080), hostname: "0.0.0.0", identityKeys })`, with `identityKeys: [process.env.CAIRN_IDENTITY_KEY]` when that variable is set and left out when it is not.
Browser:8. Create `const transport = cairnChatTransport({ baseUrl: "<same-origin proxy path>", service: "Chat" })` and add the same-origin proxy route described in the guide.9. Load the history when rendering the page with `await transport.messages(chatId)`, then call `useChat({ id: chatId, transport, messages: initial, resume: true })`.10. Approve tools with `transport.approve(chatId, approvalId, { approved })`, not with `addToolApprovalResponse`.
Keep the existing tool definitions, prompts and UI components. Do not call the model or a tooloutside `cairnMiddleware` and `cairnTools`. `maxRetries` has no effect here: cairnflow retries afailed step itself. On Cairnflow Cloud the browser gets each model step's text when the stepfinishes, not token by token, unless the deployment is registered with `"transport": "declared"`(see the streaming guide); do not promise live tokens without it. Do not invent APIs; if somethingis not in the guide, ask me.Keep your agent on the rules
Section titled “Keep your agent on the rules”Add this block to the instructions file your agent reads on every task (AGENTS.md, CLAUDE.md,
.cursor/rules/cairnflow.mdc, .github/copilot-instructions.md). It keeps later changes inside
cairnflow’s rules, such as never renaming a step that running invocations depend on.
## cairnflow
This project uses cairnflow for durable execution. Docs for agents: https://cairnflow.dev/llms.txt(append `.md` to any https://cairnflow.dev/docs/ URL to get the page as Markdown).
- Every side effect inside a handler goes through `ctx.run("<step name>", fn)`, or through `cairnTools` / `cairnMiddleware` for Vercel AI SDK tools and models.- Step names are part of the journal. Renaming a step breaks in-flight invocations; see https://cairnflow.dev/docs/develop/versioning.md before renaming or reordering steps.- No `Date.now()`, `Math.random()` or random IDs outside `ctx.run`.- Never swallow errors around `ctx` commands. Throw `TerminalError` for failures a retry cannot fix.- State (`ctx.get`, `ctx.set`) and durable promises exist only on virtual objects and workflows.- Keep journaled values small; put files and images in `ctx.artifact` (its references are only readable by the invocation that wrote them, so do not keep them in state for later turns).Docs in Markdown
Section titled “Docs in Markdown”| What | Where |
|---|---|
| Any page as Markdown | add .md to its URL, e.g. /docs/quickstart.md |
| Copy a page for your agent | the Copy page button next to every page title |
| Index of all pages | /llms.txt |
| All docs in one file | /llms-full.txt |
| A shorter version without asides | /llms-small.txt |