Sessions
Sessions launch an agent inside the VM, stream its responses in real time over sessionEvent, and persist a replayable ACP transcript that survives sleep/wake.
Create a session
Section titled “Create a session”Use createSession to launch an agent inside the VM. Returns session metadata including capabilities and agent info. The agent starts in /home/user by default; override it with the cwd option below.
import { createClient } from "@rivet-dev/agentos/client";import type { registry } from "./server";
const client = createClient<typeof registry>({ endpoint: "http://localhost:6420" });const agent = client.vm.getOrCreate("my-agent");
const session = await agent.createSession("pi", { env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },});console.log(session.sessionId);console.log(session.capabilities);console.log(session.agentInfo);createSession options
Section titled “createSession options”The second argument to createSession accepts:
env: environment variables for the agent process (e.g. API keys). Not inherited from the host.cwd: working directory inside the VM. Defaults to/home/user.mcpServers: MCP servers (local child processes or remote URLs) exposing extra tools.additionalInstructions: text appended to the agent’s system prompt.skipOsInstructions: skip the base OS instructions injection. Tool documentation is still included.
Send a prompt
Section titled “Send a prompt”Use sendPrompt to send a message to an active session. The response contains the agent’s reply.
import { createClient } from "@rivet-dev/agentos/client";import type { registry } from "./server";
const client = createClient<typeof registry>({ endpoint: "http://localhost:6420" });const agent = client.vm.getOrCreate("my-agent");
const session = await agent.createSession("pi", { env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },});const response = await agent.sendPrompt( session.sessionId, "Create a TypeScript function that checks if a number is prime",);console.log(response.text);Stream responses
Section titled “Stream responses”Subscribe to sessionEvent to receive real-time streaming output from the agent.
import { createClient } from "@rivet-dev/agentos/client";import type { registry } from "./server";
const client = createClient<typeof registry>({ endpoint: "http://localhost:6420" });const agent = client.vm.getOrCreate("my-agent");const conn = agent.connect();
// Subscribe to session events before sending the promptconn.on("sessionEvent", (data) => { console.log(`[${data.sessionId}]`, data.event.method, data.event.params);});
const session = await agent.createSession("pi", { env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },});await agent.sendPrompt(session.sessionId, "Explain how async/await works");Cancel a prompt
Section titled “Cancel a prompt”Use cancelPrompt to stop an in-progress prompt.
import { createClient } from "@rivet-dev/agentos/client";import type { registry } from "./server";
const client = createClient<typeof registry>({ endpoint: "http://localhost:6420" });const agent = client.vm.getOrCreate("my-agent");
const session = await agent.createSession("pi", { env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },});
// Start a long-running promptconst promptPromise = agent.sendPrompt( session.sessionId, "Refactor the entire codebase to use TypeScript strict mode",);
// Cancel after 10 secondssetTimeout(async () => { await agent.cancelPrompt(session.sessionId);}, 10_000);
const response = await promptPromise;console.log(response.text);Close and destroy sessions
Section titled “Close and destroy sessions”closeSessiongracefully closes a session without removing persisted datadestroySessionremoves the session and all persisted data- To reconnect to a previously created session and replay its history, see Replay events and Resuming a suspended session
import { createClient } from "@rivet-dev/agentos/client";import type { registry } from "./server";
const client = createClient<typeof registry>({ endpoint: "http://localhost:6420" });const agent = client.vm.getOrCreate("my-agent");
const session = await agent.createSession("pi", { env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },});
// Close without destroying persisted dataawait agent.closeSession(session.sessionId);
// Destroy session and all persisted eventsawait agent.destroySession(session.sessionId);Runtime configuration
Section titled “Runtime configuration”Change model, mode, and thought level on a live session.
import { createClient } from "@rivet-dev/agentos/client";import type { registry } from "./server";
const client = createClient<typeof registry>({ endpoint: "http://localhost:6420" });const agent = client.vm.getOrCreate("my-agent");
const session = await agent.createSession("pi", { env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },});
// Change modelawait agent.setModel(session.sessionId, "claude-sonnet-4-6");
// Change mode (e.g. "plan", "auto")await agent.setMode(session.sessionId, "plan");
// Change thought levelawait agent.setThoughtLevel(session.sessionId, "high");
// Query available optionsconst modes = await agent.getModes(session.sessionId);console.log(modes);
const options = await agent.getConfigOptions(session.sessionId);console.log(options);Replay events
Section titled “Replay events”Use getSessionEvents to replay a session’s persisted events, including for VMs that are not currently running. Pair it with listPersistedSessions to find earlier sessions.
import { createClient } from "@rivet-dev/agentos/client";import type { registry } from "./server";
const client = createClient<typeof registry>({ endpoint: "http://localhost:6420" });const agent = client.vm.getOrCreate("my-agent");
const session = await agent.createSession("pi", { env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },});await agent.sendPrompt(session.sessionId, "Hello");
// Replay persisted eventsconst events = await agent.getSessionEvents(session.sessionId);console.log(events);Persisted session history
Section titled “Persisted session history”Query session history from SQLite. Works even when the VM is not running.
import { createClient } from "@rivet-dev/agentos/client";import type { registry } from "./server";
const client = createClient<typeof registry>({ endpoint: "http://localhost:6420" });const agent = client.vm.getOrCreate("my-agent");
// List all persisted sessionsconst sessions = await agent.listPersistedSessions();for (const s of sessions) { console.log(s.sessionId, s.agentType, s.createdAt);}
// Get full event history for a sessionconst events = await agent.getSessionEvents(sessions[0].sessionId);for (const e of events) { console.log(e.seq, e.event.method, e.createdAt);}Multiple sessions
Section titled “Multiple sessions”A single VM can run multiple sessions simultaneously. Each session has its own agent process but shares the same filesystem. Use different session IDs to manage them independently.
import { createClient } from "@rivet-dev/agentos/client";import type { registry } from "./server";
const client = createClient<typeof registry>({ endpoint: "http://localhost:6420" });const agent = client.vm.getOrCreate("my-agent");
// Create two sessions in the same VMconst coder = await agent.createSession("pi", { env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },});const reviewer = await agent.createSession("pi", { env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },});
// Coder writes codeawait agent.sendPrompt(coder.sessionId, "Write a REST API at /home/user/api.ts");
// Reviewer reads and reviews the same fileawait agent.sendPrompt(reviewer.sessionId, "Review /home/user/api.ts for issues");
// Close each session independentlyawait agent.closeSession(coder.sessionId);await agent.closeSession(reviewer.sessionId);