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,
*,
unified_exceptions: bool | None = None,
max_retries: int | 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
unified_exceptions
bool | None
None
Convert provider exceptions for this instance when True, or preserve them when False. None (default) uses ANY_LLM_UNIFIED_EXCEPTIONS.
max_retries
int | None
None
How many times the provider's SDK client retries a failed request (0 disables retries). None (default) keeps the SDK's own default. Raises UnsupportedParameterError for providers whose SDK has no retry count to set.
**kwargs
Any
required
Additional provider-specific arguments
Returns: An AnyLLM instance bound to the specified provider.
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