Skip to content

SDK / Programmatic Usage

✓ Fresh · 2026-02-24

Pi can be used programmatically as a library, enabling you to build applications that leverage Pi's agent capabilities without the interactive CLI.

Installation

bash
npm install @mariozechner/pi-coding-agent

Basic Usage

typescript
import {
  AuthStorage,
  createAgentSession,
  ModelRegistry,
  SessionManager
} from "@mariozechner/pi-coding-agent";

// Create auth storage for API keys
const authStorage = new AuthStorage();

// Create model registry
const modelRegistry = new ModelRegistry(authStorage);

// Create an agent session
const { session } = await createAgentSession({
  sessionManager: SessionManager.inMemory(),
  authStorage,
  modelRegistry,
});

// Send a prompt and get a response
await session.prompt("What files are in the current directory?");

Architecture

Key Components

AuthStorage

Manages authentication credentials (API keys and OAuth tokens):

typescript
const authStorage = new AuthStorage();
// API keys are read from environment variables automatically

ModelRegistry

Tracks available models and their configurations:

typescript
const modelRegistry = new ModelRegistry(authStorage);
// Discovers models based on available API keys

SessionManager

Controls how sessions are stored:

typescript
// In-memory (no persistence)
SessionManager.inMemory()

// File-based (persists to disk)
SessionManager.fileBased("/path/to/sessions/")

AgentSession

The main interface for interacting with the agent:

typescript
const { session } = await createAgentSession({ ... });

// Send a prompt
const result = await session.prompt("Your request here");

// Access session info
console.log(session.tokenCount);
console.log(session.cost);

RPC Mode

For non-Node.js applications, Pi supports an RPC protocol over stdin/stdout:

bash
pi --mode rpc

RPC Communication

The RPC mode uses JSON messages over stdin/stdout:

This enables integration with:

  • Python applications
  • Go services
  • Rust tools
  • Any language that can spawn a process and communicate via stdin/stdout

Monorepo Packages

The Pi monorepo provides multiple packages at different abstraction levels:

PackagePurposeUse When
@mariozechner/pi-coding-agentFull interactive CLI + SDKBuilding apps with full agent capabilities
@mariozechner/pi-agent-coreAgent runtime without CLICustom agent UIs, embedded agents
@mariozechner/pi-aiMulti-provider LLM API onlyJust need LLM access without agent features
@mariozechner/pi-tuiTerminal UI libraryBuilding custom terminal interfaces
@mariozechner/pi-web-uiWeb components for chatBuilding web-based chat UIs
@mariozechner/pi-momSlack botDelegating to Pi from Slack
@mariozechner/pi-podsvLLM deployment CLIManaging GPU pods for self-hosted models

Use Cases

Automated Code Review Bot

typescript
const { session } = await createAgentSession({
  sessionManager: SessionManager.inMemory(),
  authStorage: new AuthStorage(),
  modelRegistry: new ModelRegistry(authStorage),
});

const diff = await getDiffFromPR(prNumber);
const review = await session.prompt(
  `Review this code diff for bugs, security issues, and style:\n\n${diff}`
);
postReviewComment(prNumber, review);

Batch File Processing

typescript
const files = await glob("src/**/*.ts");
for (const file of files) {
  const { session } = await createAgentSession({
    sessionManager: SessionManager.inMemory(),
    authStorage: new AuthStorage(),
    modelRegistry: new ModelRegistry(authStorage),
  });
  await session.prompt(`Analyze ${file} for potential improvements.`);
}

CI/CD Integration

typescript
const { session } = await createAgentSession({ ... });
await session.prompt(
  "Run the test suite and fix any failing tests. " +
  "Commit the fixes with descriptive messages."
);

Pi Coding Agent SOP Documentation