Skip to main content

Overview

The @mariozechner/pi-ai package provides a unified interface to multiple LLM providers with automatic model discovery, token counting, cost tracking, and cross-provider context handoffs.

Unified API

Single interface for OpenAI, Anthropic, Google, and 12+ other providers

Type Safety

Full TypeScript support with auto-complete for providers and models

Tool Calling

TypeBox schemas with automatic validation and partial JSON streaming

Cross-Provider Handoff

Switch models mid-conversation with automatic context transformation

Installation

Quick Start

Key Features

Supported Providers

All providers support tool calling (function calling) for agentic workflows: API Key Providers:
  • OpenAI (GPT-4o, GPT-5, o1, o3)
  • Anthropic (Claude Sonnet, Opus, Haiku)
  • Google Gemini
  • Amazon Bedrock
  • Mistral AI
  • Groq
  • Cerebras
  • xAI (Grok)
  • OpenRouter
  • Vercel AI Gateway
  • zAI
  • MiniMax
  • Kimi For Coding
  • Hugging Face
OAuth Providers:
  • OpenAI Codex (ChatGPT Plus/Pro, GPT-5.x Codex models)
  • GitHub Copilot
  • Google Gemini CLI (Cloud Code Assist)
  • Google Antigravity (free Gemini, Claude, GPT-OSS)
Cloud Providers:
  • Azure OpenAI (Responses API)
  • Google Vertex AI (with ADC)
Custom:
  • Any OpenAI-compatible API (Ollama, vLLM, LM Studio, etc.)

Tool Calling with TypeBox

Define tools with TypeBox schemas for type-safe validation:

Streaming with Events

Stream responses with granular event types:
Event Types:
  • start - Stream begins
  • text_start, text_delta, text_end - Text generation
  • thinking_start, thinking_delta, thinking_end - Reasoning/thinking
  • toolcall_start, toolcall_delta, toolcall_end - Tool calls
  • done - Completion
  • error - Error or abort

Partial Tool Arguments

During streaming, tool arguments are progressively parsed:

Reasoning/Thinking Models

Many models support extended thinking capabilities:

Cross-Provider Handoffs

Switch models mid-conversation - the library handles context transformation automatically:

Image Support

Vision-capable models can process images:

Token Counting and Costs

Every response includes detailed usage information:

Custom Models

Define custom models for local servers or proxies:

API Reference

Core Functions

Types

For complete type definitions, see Stream and Complete API Reference.

OAuth Authentication

For providers requiring OAuth (Codex, Copilot, Gemini CLI, Antigravity):

Environment Variables

Provider API keys can be set via environment variables:
See Installation Guide for the complete list.