Caveman Agent SDK· Open sourceAgent SDK & Harness v4
Sessions, Caveman Gates, Sensors, and Cheap-First Escalation
Stream multi-turn agent sessions, enforce fail-closed Caveman Gate approvals on high-risk tools, park at zero compute with Sensors, and escalate models only when checks fail.
Every agent run in @caveman-ai/agent is backed by a continuous Session with one append-only event log. You can stream typed events, intercept sensitive tool calls with fail-closed Caveman Gates, park between triggers with Sensors, and escalate from a fast model to a frontier model only when a test command fails.
Fail-closed Caveman Gates#
Attach { gate: { risk: "high" } } to any sensitive tool. Execution pauses fail-closed (including nested QuickJS cell and MCP calls) until your gateProvider approves. Without a provider, gated actions are denied by default.
import { createMemoryGateProvider, defineAgent, runInProcess, tool } from "@caveman-ai/agent";
const issueRefund = tool(
"issue_refund",
"Issue a refund for an invoice.",
{
type: "object",
properties: { id: { type: "string" }, amountUsd: { type: "number" } },
required: ["id", "amountUsd"],
},
async ({ id, amountUsd }: { id: string; amountUsd: number }) => ({ refunded: true, id, amountUsd }),
{ gate: { risk: "high", summary: (input) => `Refund ${JSON.stringify(input)}` } },
);
const billing = defineAgent({
name: "billing",
instructions: "Use issue_refund when a customer was double-charged.",
computer: "none",
tools: [issueRefund],
});
const result = await runInProcess({
agent: billing,
task: "Refund $42 on invoice 4411",
model: "anthropic/claude-sonnet-4-6",
gateProvider: createMemoryGateProvider(async (req) => ({
gateId: req.gateId,
status: "approved", // return "denied" to block execution fail-closed
decidedBy: "ops-reviewer",
})),
});Multi-turn streaming sessions#
Use cave.sessions.create() for live event streaming, follow-up turns, and interactive requires_action handling:
import { Caveman } from "@caveman-ai/agent";
const cave = new Caveman();
const session = await cave.sessions.create({
agent: "builder",
task: "Fix the flaky retry test",
cwd: ".",
});
for await (const event of session.events()) {
console.log(event.seq, event.type, event.data);
}
const first = await session.next();
const followUp = await session.send("Also add a regression test for 429 responses.");
await session.close();When a turn emits requires_action carrying an Ask:
kind: "tool": answered automatically by the handler passed totool().kind: "approval": answered bycanUseTool(denied by default if omitted).kind: "question": answered byonQuestion(when omitted, the agent decides and states its assumption).
Cheap-first escalation with checks#
Try a fast model first and escalate only when a deterministic check command (or verifier function) fails, bounded by a hard USD budget:
const result = await cave.run({
agent: "builder",
task: "Fix the failing parser test",
model: "fast",
check: "pnpm test",
escalate: ["base", "frontier"],
budget: { usd: 2 },
});If check exits non-zero, the SDK hands the failure output to the next model in escalate and halts immediately if budget.usd is reached.
Sensors and durable parking#
Declare defineSensor(...) triggers when an agent should sleep until an external condition fires. Headless runs exit with code 5 (parked on approval, tool, or timer) or 6 (idle between turns), letting your host or Caveman Cloud reclaim compute and resume cleanly from the event log.