Skip to main content
Agent

Sessions

Create agent sessions, send prompts, stream responses, and replay event history.

  • Create sessions with any supported agent type
  • Stream responses in real time via sessionEvent subscriptions
  • Replay events with sequence numbers for reconnection and history
  • Persist transcripts automatically in SQLite across sleep/wake cycles
  • Universal transcript format using the Agent Communication Protocol (ACP)

Currently only Pi is supported as an agent. Amp, Claude Code, Codex, and OpenCode are coming soon.

Create a session

Use createSession to launch an agent inside the VM. Returns session metadata including capabilities and agent info.

env

Environment variables to pass to the agent process. The VM does not inherit from the host process.env, so API keys must be passed explicitly.

const session = await agent.createSession("pi", {
  env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },
});

cwd

Working directory for the agent session inside the VM. Defaults to /home/user.

const session = await agent.createSession("pi", {
  env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },
  cwd: "/home/user/project",
});

mcpServers

Pass MCP servers to give the agent access to additional tools. MCP servers provide typed tool definitions that the agent’s LLM can discover and call natively.

Local MCP server

Run an MCP server as a child process inside the VM.

const session = await agent.createSession("pi", {
  env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },
  mcpServers: [
    {
      type: "local",
      command: "npx",
      args: ["-y", "@modelcontextprotocol/server-filesystem", "/home/user"],
      env: {},
    },
  ],
});

Remote MCP server

Connect to an MCP server running outside the VM.

const session = await agent.createSession("pi", {
  env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },
  mcpServers: [
    {
      type: "remote",
      url: "https://mcp.example.com/sse",
      headers: {
        Authorization: "Bearer my-token",
      },
    },
  ],
});

additionalInstructions

Append custom instructions to the agent’s system prompt.

const session = await agent.createSession("pi", {
  env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },
  additionalInstructions: "Always write tests before implementation.",
});

skipOsInstructions

Skip the base OS instructions injection. Tool documentation is still included even when this is true.

const session = await agent.createSession("pi", {
  env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },
  skipOsInstructions: true,
});

Send a prompt

Use sendPrompt to send a message to an active session. The response contains the agent’s reply.

Stream responses

Subscribe to sessionEvent to receive real-time streaming output from the agent.

Cancel a prompt

Use cancelPrompt to stop an in-progress prompt.

Resume, close, and destroy sessions

  • resumeSession reconnects to a session that was suspended (e.g. after sleep)
  • closeSession gracefully closes a session
  • destroySession removes the session and all persisted data

Runtime configuration

Change model, mode, and thought level on a live session.

Replay events

Use getSequencedEvents to replay in-memory session events (for live reconnection while the VM is running), or getSessionEvents to replay from persisted storage (for transcript history, including when the VM is not running). See Events for details on the difference.

Persisted session history

Query session history from SQLite. Works even when the VM is not running.

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.

Recommendations

  • Subscribe to sessionEvent before calling sendPrompt to avoid missing early events.
  • Use getSequencedEvents with since for reconnection. Track the last sequence number you processed.
  • Use listPersistedSessions and getSessionEvents to build transcript history UIs without requiring a running VM.
  • Call closeSession when done to release resources. Use destroySession only when you want to permanently delete session data.