Skip to main content

LLM API Overview

The @mariozechner/pi-ai package provides a unified API for interacting with Large Language Models from multiple providers. It abstracts away provider-specific differences while maintaining full access to advanced features like tool calling, streaming, and reasoning capabilities.

Key Features

Unified Interface

Single API that works across 15+ providers including OpenAI, Anthropic, Google, xAI, Groq, Cerebras, and more. Switch between models without changing your code.

Type Safety

Full TypeScript support with auto-completion for providers, models, and options. TypeBox schemas ensure runtime validation of tool parameters.

Automatic Model Discovery

Query available models with full metadata including context windows, pricing, capabilities (vision, reasoning), and API type.

Tool Calling (Function Calling)

Define tools with TypeBox schemas for automatic validation. Supports streaming partial tool arguments as they arrive.

Streaming Support

Real-time streaming of text, thinking/reasoning, and tool calls with granular events for building responsive UIs.

Cost Tracking

Automatic token counting and cost calculation per request with breakdown by input, output, and cache usage.

Cross-Provider Handoffs

Seamlessly switch models mid-conversation while preserving context, tool calls, and thinking blocks.

Context Serialization

Contexts are plain JSON objects that can be easily serialized, stored, and transferred between services.

Installation

TypeBox types (Type, Static, TSchema) are re-exported from the package.

Core Concepts

Models

Models are identified by provider and model ID. The library includes metadata for all supported models:

Context

A context contains the conversation history, system prompt, and available tools:

Messages

Three message types:
  • UserMessage - User input (text and/or images)
  • AssistantMessage - Model responses with text, thinking, and tool calls
  • ToolResultMessage - Results from tool execution (text and/or images)

Streaming vs Complete

Two ways to interact with models:

Quick Example

Next Steps

Providers

Explore all 15+ supported providers and their setup

Streaming

Learn about streaming events and real-time responses

Tools

Define and use tool calling for agentic workflows

Thinking

Enable reasoning capabilities across providers