Skip to main content

No project description provided

Project description

Project logo

any-llm

Read the Blog Post

Docs

Linting Unit Tests Integration Tests

Python 3.11+ PyPI Discord

Communicate with any LLM provider using a single, unified interface. Switch between OpenAI, Anthropic, Azure / Microsoft Foundry, Mistral, Ollama, and more without changing your code.

Documentation | otari.ai | Try the Demos | Contributing

Quickstart

pip install 'any-llm-sdk[mistral,ollama]'

export MISTRAL_API_KEY="YOUR_KEY_HERE"  # or OPENAI_API_KEY, etc
from any_llm import completion
import os

# Make sure you have the appropriate environment variable set
assert os.environ.get('MISTRAL_API_KEY')

response = completion(
    model="mistral-small-latest",
    provider="mistral",
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)

That's it! Change the provider name and add provider-specific keys to switch between LLM providers.

Coming from LiteLLM? Your API keys and environment variables carry over unchanged. Install the SDK with extras for the providers you need, then update your import and model strings:

pip install 'any-llm-sdk[openai,anthropic]'  # or [all] for everything
# before
from litellm import completion
response = completion(model="openai/gpt-4o", messages=[...])

# after
from any_llm import completion
response = completion(model="openai:gpt-4o", messages=[...])

See Supported Providers to map your existing model strings.

That's the full migration — no proxy, no extra config.

Installation

Requirements

  • Python 3.11 or newer
  • API keys for whichever LLM providers you want to use

Basic Installation

Install support for specific providers:

pip install 'any-llm-sdk[openai]'           # Just OpenAI
pip install 'any-llm-sdk[mistral,ollama]'   # Multiple providers
pip install 'any-llm-sdk[all]'              # All supported providers

See our list of supported providers to choose which ones you need.

Setting Up API Keys

Set environment variables for your chosen providers:

export OPENAI_API_KEY="your-key-here"
export ANTHROPIC_API_KEY="your-key-here"
export MISTRAL_API_KEY="your-key-here"
# ... etc

Alternatively, pass API keys directly in your code (see Usage examples).

Otari Gateway

For budget management, API key management, usage analytics, and multi-tenant support, see mozilla-ai/otari.

Why choose any-llm?

  • Simple, unified interface - Single function for all providers, switch models with just a string change
  • Developer friendly - Full type hints for better IDE support and clear, actionable error messages
  • Leverages official provider SDKs - Ensures maximum compatibility
  • Stays framework-agnostic so it can be used across different projects and use cases
  • Battle-tested - Powers our own production tools (any-agent)

Usage

any-llm offers two main approaches for interacting with LLM providers:

Option 1: Direct API Functions (Recommended for Bootstrapping and Experimentation)

Recommended approach: Use separate provider and model parameters:

from any_llm import completion
import os

# Make sure you have the appropriate environment variable set
assert os.environ.get('MISTRAL_API_KEY')

response = completion(
    model="mistral-small-latest",
    provider="mistral",
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)

Alternative syntax: Use combined provider:model format:

response = completion(
    model="mistral:mistral-small-latest", # <provider_id>:<model_id>
    messages=[{"role": "user", "content": "Hello!"}]
)

Option 2: AnyLLM Class (Recommended for Production)

For applications that need to reuse providers, perform multiple operations, or require more control:

from any_llm import AnyLLM

llm = AnyLLM.create("mistral", api_key="your-mistral-api-key")

response = llm.completion(
    model="mistral-small-latest",
    messages=[{"role": "user", "content": "Hello!"}]
)

When to Use Which Approach

Approach Best For Connection Handling
Direct API Functions (completion) Scripts, notebooks, single requests New client per call (stateless)
AnyLLM Class (AnyLLM.create) Production apps, multiple requests Reuses client (connection pooling)

Both approaches support identical features: streaming, tools, responses API, etc.

Responses API

For providers that implement the OpenAI-style Responses API, use responses or aresponses:

from any_llm import responses

result = responses(
    model="gpt-4o-mini",
    provider="openai",
    input_data=[
        {"role": "user", "content": [
            {"type": "text", "text": "Summarize this in one sentence."}
        ]}
    ],
)

# Non-streaming returns an OpenAI-compatible Responses object alias
print(result.output_text)

Finding the Right Model

The provider_id should match our supported provider names.

The model_id is passed directly to the provider. To find available models:

  • Check the provider's documentation
  • Use our list_models API (if the provider supports it)

Motivation

The landscape of LLM provider interfaces is fragmented. While OpenAI's API has become the de facto standard, providers implement slight variations in parameter names, response formats, and feature sets. This creates a need for light wrappers that gracefully handle these differences while maintaining a consistent interface.

Existing Solutions and Their Limitations:

  • LiteLLM: Popular but reimplements provider interfaces rather than leveraging official SDKs, leading to potential compatibility issues.
  • AISuite: Clean, modular approach but lacks active maintenance, comprehensive testing, and modern Python typing standards.
  • Framework-specific solutions: Some agent frameworks either depend on LiteLLM or implement their own provider integrations, creating fragmentation
  • Proxy Only Solutions: solutions like OpenRouter and Portkey require a hosted proxy between your code and the LLM provider.

any-llm addresses these challenges by leveraging official SDKs when available, maintaining framework-agnostic design, and requiring no proxy servers.

Documentation

Contributing

We welcome contributions from developers of all skill levels! Please see our Contributing Guide or open an issue to discuss changes.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

any_llm_sdk-1.18.0.tar.gz (154.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

any_llm_sdk-1.18.0-py3-none-any.whl (203.4 kB view details)

Uploaded Python 3

File details

Details for the file any_llm_sdk-1.18.0.tar.gz.

File metadata

  • Download URL: any_llm_sdk-1.18.0.tar.gz
  • Upload date:
  • Size: 154.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for any_llm_sdk-1.18.0.tar.gz
Algorithm Hash digest
SHA256 9136b094da01a4fd1a6370610bfc03f7d640a4ba72a1f53299044e55ddd74a00
MD5 b43b9aa2fa564b57fa5ef97aa548e8e8
BLAKE2b-256 079c8a1b3f014f3ae1db1ca193466ae8b54f49580b0500bd4f25a640b893d5fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for any_llm_sdk-1.18.0.tar.gz:

Publisher: release.yaml on mozilla-ai/any-llm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file any_llm_sdk-1.18.0-py3-none-any.whl.

File metadata

  • Download URL: any_llm_sdk-1.18.0-py3-none-any.whl
  • Upload date:
  • Size: 203.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for any_llm_sdk-1.18.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d36d67dada917d31027efb6cc4233e52c5f40b8dfbd8df35f7c4cf9d508b05fb
MD5 f1500ab892c8bac649a0f0b65c0829e0
BLAKE2b-256 4a4e3cb7c7594354105cfa2afedff929a6cc0409a449a9719a5b5c63553201e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for any_llm_sdk-1.18.0-py3-none-any.whl:

Publisher: release.yaml on mozilla-ai/any-llm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page