Skip to content

pi-ai unified LLM API

The pi-ai unified LLM API is a TypeScript library (@mariozechner/pi-ai) that provides a standardized abstraction layer over 20+ distinct LLM providers^[001-TODO__Pi_Monorepo_-AI_Agent_开发工具包.md]. It is part of the Pi Monorepo toolkit, designed to solve the fragmentation of LLM APIs by normalizing request/response formats, authentication, and streaming events across different services^[001-TODO__Pi_Monorepo-_AI_Agent_开发工具包.md].

This API allows developers to switch between providers (e.g., OpenAI, Anthropic, local models) within the same conversation without changing the application code, while providing built-in utilities for token tracking, schema validation, and context serialization^[001-TODO__Pi_Monorepo_-_AI_Agent_开发工具包.md].

Key Features

Unified Streaming Events

The library transforms disparate provider protocols into a consistent stream of events^[001-TODO__Pi_Monorepo_-_AI_Agent_开发工具包.md]. This simplifies client-side logic by handling standard events such as: * Text: text_start, text_delta, text_end * Thinking: thinking_start, thinking_delta, thinking_end * Tool Calls: toolcall_start, toolcall_delta, toolcall_end * Lifecycle: done, error

Provider Interoperability

The API supports seamless handoffs between providers during a session^[001-TODO__Pi_Monorepo_-AI_Agent_开发工具包.md]. For example, a conversation can start on Claude, switch to GPT-4, and then to a local model. Special handling is included for provider-specific features; for instance, Anthropic's extended thinking blocks are automatically converted to standard <thinking> tags for compatibility^[001-TODO__Pi_Monorepo-_AI_Agent_开发工具包.md].

Context Management

  • Serialization: Context state is serialized into standard JSON, enabling persistence and transmission across different services or environments^[001-TODO__Pi_Monorepo_-_AI_Agent_开发工具包.md].
  • Validation: It utilizes TypeBox for schema definitions and AJV (Another JSON Schema Validator) to automatically validate tool parameters^[001-TODO__Pi_Monorepo_-_AI_Agent_开发工具包.md].
  • Tracking: Every API call automatically tracks token usage and estimated costs^[001-TODO__Pi_Monorepo_-_AI_Agent_开发工具包.md].

Environment Support

  • Browser Compatible: The API is designed to run in browser environments (with the exception of Amazon Bedrock)^[001-TODO__Pi_Monorepo_-_AI_Agent_开发工具包.md].
  • OAuth Integration: Built-in support for OAuth flows and token refresh for providers like OpenAI Codex, GitHub Copilot, and Google Gemini CLI^[001-TODO__Pi_Monorepo_-_AI_Agent_开发工具包.md].

Supported Providers

The pi-ai library aggregates support for over 20 providers, categorized into the following groups^[001-TODO__Pi_Monorepo_-_AI_Agent_开发工具包.md]:

Category Providers
Direct OpenAI, Anthropic, Google, Mistral, Groq, Cerebras, xAI, OpenRouter, MiniMax, Vercel AI Gateway
Cloud Platforms Azure OpenAI, Google Vertex AI, Amazon Bedrock
OAuth OpenAI Codex, GitHub Copilot, Google Gemini CLI, Antigravity
Compatible APIs Ollama, vLLM, LM Studio, DeepSeek, zAI, OpenCode
Special Kimi For Coding (Moonshot AI)

Architecture & Implementation

The library includes specific implementations for various proprietary and open APIs^[001-TODO__Pi_Monorepo_-_AI_Agent_开发工具包.md]. Examples include: * anthropic-messages * openai-responses / openai-completions * google-generative-ai / google-vertex * bedrock-converse-stream * openai-codex-responses (WebSocket/SSE)

A Faux Provider is also included as an in-memory mock provider for testing purposes^[001-TODO__Pi_Monorepo_-_AI_Agent_开发工具包.md].

  • [[pi-coding-agent]]: The primary CLI application that utilizes pi-ai for its model interactions.
  • [[pi-agent-core]]: The runtime layer that works alongside pi-ai to manage agent state and tool execution.

Sources

  • 001-TODO__Pi_Monorepo_-_AI_Agent_开发工具包.md