For the complete documentation index, see llms.txt. This page is also available as Markdown.

Quickstart

Install any-llm and make your first API call in 5 minutes

Requirements

  • Python 3.11 or newer

  • API keys for your chosen LLM provider

Installation

pip install any-llm-sdk[all]  # Install with all provider support

Installing Specific Providers

If you want to install a specific provider from our supported providers:

pip install any-llm-sdk[mistral]  # For Mistral provider
pip install any-llm-sdk[ollama]   # For Ollama provider
# install multiple providers
pip install any-llm-sdk[mistral,ollama]

Library Integration

If you're building a library, install just the base package (pip install any-llm-sdk) and let your users install provider dependencies.

API Keys: Set your provider's API key as an environment variable (e.g., export MISTRAL_API_KEY="your-key") or pass it directly using the api_key parameter.

APIs

Using the AnyLLM Class

For applications making multiple requests with the same provider, use the AnyLLM class to avoid repeated provider instantiation:

API Call

When to Choose Which Approach

Use Direct API Functions (completion, acompletion) when:

  • Making simple, one-off requests

  • Prototyping or writing quick scripts

  • You want the simplest possible interface

Use Provider Class (AnyLLM.create) when:

  • Building applications that make multiple requests with the same provider

  • You want to avoid repeated provider instantiation overhead

Finding model names: Check the providers page for provider IDs, or use the list_models API to see available models for your provider.

Streaming

For the providers that support streaming, you can enable it by passing stream=True:

Reasoning

For providers that support reasoning, you can request thinking traces alongside the response using reasoning_effort:

Reasoning also works with streaming — each chunk may include chunk.choices[0].delta.reasoning.

Embeddings

embedding and aembedding allow you to create vector embeddings from text using the same unified interface across providers.

Not all providers support embeddings - check the providers documentation to see which ones do.

Moderation

moderation and amoderation run a content-safety classifier against input (or output) text and return a normalized, OpenAI-compatible result.

Not all providers support moderation; calling an unsupported provider raises NotImplementedError. Today, OpenAI and Mistral implement the API.

Pass include_raw=True to populate ModerationResult.provider_raw with the untouched provider response (useful for debugging or provider-specific fields).

Tools

any-llm supports tool calling for providers that support it. You can pass a list of tools where each tool is either:

  1. Python callable - Functions with proper docstrings and type annotations

  2. OpenAI Format tool dict - Already in OpenAI tool format

any-llm automatically converts your Python functions to OpenAI tools format. Functions must have:

  • A docstring describing what the function does

  • Type annotations for all parameters

  • A return type annotation

Exception Handling

The any-llm package provides a unified exception hierarchy that works consistently across all LLM providers.

Enabling Unified Exceptions

Opt-in Feature: Unified exception handling is currently opt-in. Set the ANY_LLM_UNIFIED_EXCEPTIONS environment variable to enable it:

When enabled, provider-specific exceptions are automatically converted to any-llm exception types. When disabled (default), the original provider exceptions are raised with a deprecation warning.

Basic Usage

Accessing Original Exceptions

All unified exceptions preserve the original provider exception for debugging:

Last updated