any-llm-ts
Getting started

Configuration

Configure providers, base URLs, SDK clients, and provider-specific request fields.

Configuration can happen at three levels: provider discovery, SDK client construction, and an individual request.

Provider configuration

AnyLLM.create() accepts a provider name and ProviderOptions:

import { AnyLLM } from "any-llm-ts";

const llm = AnyLLM.create("openai", {
  apiKey: process.env.OPENAI_API_KEY,
  apiBase: "https://api.openai.com/v1",
  maxRetries: 0,
  clientOptions: {
    timeout: 30_000,
  },
});
OptionPurpose
apiKeyOverrides the provider's conventional API-key environment variable.
apiBaseOverrides the configured API base URL or endpoint.
maxRetriesSets the SDK retry count (0 disables retries). undefined keeps the SDK default.
clientOptionsPasses additional constructor options to the underlying provider SDK.

maxRetries counts retries after the first request. OpenAI-compatible and Anthropic-based providers pass it through as maxRetries. Gemini and Vertex AI set httpOptions.retryOptions.attempts to maxRetries + 1 because the GenAI SDK counts the first request. Providers whose SDK has no client-level retry count (azure, bedrock, liner, otari, sagemaker, watsonx) reject the option; configure those through clientOptions instead. For Gemini and Vertex, retryOptions already present in clientOptions.httpOptions win.

The values in clientOptions are intentionally typed as Record<string, unknown> because the accepted keys differ between SDKs and versions.

Request-specific fields

Use providerOptions for request properties not represented by the common interface:

await llm.completion({
  model: "provider-model-name",
  messages: [{ role: "user", content: "Hello" }],
  providerOptions: {
    service_tier: "priority",
  },
});

providerOptions is merged into the outgoing payload after normalized fields, so it can override wire-level values. That is useful as an escape hatch, but it also means you should validate or hard-code these options rather than accepting them directly from untrusted callers.

Azure OpenAI

Azure OpenAI uses the v1 API at {endpoint}/openai/v1/. Dated apiVersion values and azureDeployment client options are rejected; pass the deployment name as model on each request. Image, transcription, and speech calls add api-version=preview unless the caller already set a query version.

export AZURE_OPENAI_ENDPOINT="https://example.openai.azure.com"
export AZURE_OPENAI_API_KEY="..."
# or
export AZURE_OPENAI_AD_TOKEN="..."
const azure = AnyLLM.create("azureopenai");

You can pass OpenAI SDK constructor fields through clientOptions, including azureADTokenProvider for Entra ID. apiKey, azureADToken, and azureADTokenProvider are mutually exclusive.

Hugging Face billing

Team and Enterprise organizations can pay for Inference Providers usage instead of the token's own account. Pass the organization name (or resource-group id) as clientOptions.billTo. Completions send it to @huggingface/inference as billTo. Responses send X-HF-Bill-To on the OpenResponses client, merged with any existing clientOptions.openAI.defaultHeaders.

const huggingface = AnyLLM.create("huggingface", {
  clientOptions: { billTo: "my-org" },
});

Omit billTo to keep billing on the token's own account. Hugging Face only honors the header for an organization the token's user belongs to and which is allowed to be billed for Inference Providers.

Inspect effective metadata

const metadata = AnyLLM.getProviderMetadata("openai");

console.log(metadata.envApiKey);
console.log(metadata.apiBase);
console.log(metadata.capabilities.streaming);
console.log(metadata.capabilities.pdfInput);
console.log(metadata.promptCacheKeySupport);
console.log(metadata.supportsCompletionN);
console.log(metadata.supportsCompletionResponseFormat);
console.log(metadata.tier);

Metadata is returned as a clone, so changing the returned object does not modify the registry. tier mirrors the Python project's credential-backed verification matrix. It describes provider integration coverage, not whether every model and operation is available in your account.

When promptCacheKeySupport is unsupported, passing promptCacheKey to completions, Responses, or Messages fails before a network request. supported means the provider handles the key directly; passthrough means a router or custom compatible endpoint receives it unchanged.

n=1 is dropped for every provider. Other values of n are forwarded only when supportsCompletionN is true; otherwise they raise UnsupportedParameterError.

supportsCompletionResponseFormat is true when a chat completion may carry responseFormat. Inception, Llamafile, MiniMax, SageMaker, and Z.AI clear it for every model. Bedrock answers per model in AnyLLM.supportsCompletionResponseFormat(provider, model) (Claude only).

On this page