AnyLLM
The AnyLLM class - provider interface with metadata access and reusability
The AnyLLM class is the provider interface at the core of any-llm. Use it when you need to make multiple requests against the same provider without re-instantiating on every call.
Creating an Instance
AnyLLM.create()
Factory method that returns a configured AnyLLM instance for the given provider.
def create(
provider: str | LLMProvider,
api_key: str | None = None,
api_base: str | None = None,
**kwargs: Any,
) -> AnyLLMprovider
str | LLMProvider
required
The provider name (e.g., 'openai', 'anthropic')
api_key
str | None
None
API key for the provider
api_base
str | None
None
Base URL for the provider API
**kwargs
Any
required
Additional provider-specific arguments
Returns: An AnyLLM instance bound to the specified provider.
from any_llm import AnyLLM
llm = AnyLLM.create("openai", api_key="sk-...")
response = llm.completion(
model="gpt-4.1-mini",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)Static Methods
AnyLLM.split_model_provider()
Parses a combined "provider:model" string into its components.
model
str
Combined identifier in "provider:model" format (e.g., "openai:gpt-4.1-mini"). The legacy "provider/model" format is also accepted but deprecated.
Returns: A (provider, model_name) tuple. provider is an LLMProvider member when the provider has one, and the bare name for a config-only registry gateway.
Raises: ValueError if the string does not contain a : or / delimiter, or UnsupportedProviderError if the provider is not resolvable.
AnyLLM.get_all_provider_metadata()
Returns metadata for every supported provider, sorted alphabetically by name.
Returns: A list of ProviderMetadata objects.
AnyLLM.get_supported_providers()
Returns a list of all supported provider key strings.
Returns: list[str] of provider keys (e.g., ["anthropic", "openai", ...]).
Instance Methods
All instance methods below are called on an AnyLLM object returned by AnyLLM.create().
completion() / acompletion()
Create a chat completion. See the Completion reference for the full parameter list.
responses() / aresponses()
Create a response using the OpenResponses API. See the Responses reference.
messages() / amessages()
Create a message using the Anthropic Messages API format. All providers support this through automatic conversion.
list_models() / alist_models()
List available models for this provider. See the List Models reference.
create_batch() / acreate_batch()
Create a batch job. See the Batch reference.
get_provider_metadata()
Returns metadata for this provider instance's class.
Returns: A ProviderMetadata object describing the provider's capabilities.
Last updated