Skip to main content

lmux-anthropic

Anthropic provider for lmux. Wraps the anthropic SDK.

Supports chat completions and streaming.

Part of the lmux ecosystem: standardized interface, cost tracking on every response, and registry-based routing across providers.

Auth

Set ANTHROPIC_API_KEY in your environment. The default AnthropicEnvAuthProvider reads it automatically.

from lmux_anthropic import AnthropicProvider

provider = AnthropicProvider()

Usage

Chat

from lmux import UserMessage

response = provider.chat("claude-sonnet-4-20250514", [UserMessage(content="Hello")])
print(response.content)
print(response.cost)

Streaming

for chunk in provider.chat_stream("claude-sonnet-4-20250514", [UserMessage(content="Hello")]):
    if chunk.delta:
        print(chunk.delta, end="")

Async

All methods have async variants: achat, achat_stream.

Registry

Use with the lmux registry to route across multiple providers:

from lmux import Registry

registry = Registry()
registry.register("anthropic", provider)
response = registry.chat("anthropic/claude-sonnet-4-20250514", messages)

Provider Params

from lmux_anthropic import AnthropicParams

response = provider.chat(
    "claude-sonnet-4-20250514",
    messages,
    provider_params=AnthropicParams(inference_geo="us"),
)
Parameter Type Description
thinking dict Extended thinking configuration
metadata dict[str, str] Request metadata
top_k int Top-k sampling
service_tier "auto" | "standard_only" Service tier selection
inference_geo "us" Inference geography (affects cost)
cache_control dict Top-level prompt-cache control — auto-places a breakpoint on the last cacheable block (e.g. {"type": "ephemeral"})
pricing_as_of datetime.date Override the date used for dated pricing (e.g. a model's introductory-rate window); defaults to the current date

Prompt Caching

Two ways to opt in:

  • Top-level (auto-placement): pass cache_control via AnthropicParams (above) to cache the full rendered prefix.
  • Explicit breakpoints: place CachePointContent parts in UserMessage content. A cache point marks the end of the stable prefix; it attaches cache_control to the preceding content block. A cache point with no preceding block in its message applies to whatever came before it: the prior message's last block, or the system text seen so far (system text after the marker stays outside the cached prefix). A marker with nothing cacheable before it is dropped, and when two markers resolve to the same block the first one wins.
from lmux import CachePointContent, TextContent, UserMessage

messages = [
    UserMessage(content=[TextContent(text=big_stable_context), CachePointContent(ttl="1h")]),
    UserMessage(content="What changed since yesterday?"),
]

Cache reads/writes are reported on response.usage (cache_read_tokens, cache_creation_tokens, and the per-TTL cache_creation_tokens_by_ttl breakdown) and priced into response.cost, including the 2x write rate for ttl="1h".

Claude on Vertex AI

Requires the vertex extra, which pulls in google-auth via anthropic[vertex]:

uv add "lmux-anthropic[vertex]"

AnthropicVertexProvider serves Claude through GCP Vertex AI with the same chat/streaming interface:

from lmux_anthropic import AnthropicVertexProvider

provider = AnthropicVertexProvider(project_id="my-project", region="global")
response = provider.chat("claude-sonnet-4-5@20250929", [UserMessage(content="Hello")])
print(response.provider)  # "anthropic-vertex"
print(response.cost)

project_id falls back to the ANTHROPIC_VERTEX_PROJECT_ID environment variable, then to the project resolved by the auth provider (e.g. the gcloud default project under ADC, or the service account key file's project). region falls back to CLOUD_ML_REGION; a request without a region raises at first call. region accepts "global", a multi-region ("us", "eu"), or a specific region ("us-east5", ...). Model IDs use Vertex's @-versioned format (claude-sonnet-4-5@20250929) or plain names for newer models (claude-opus-4-6).

Vertex Auth

Application Default Credentials by default; a service account file is also supported:

from lmux_anthropic import AnthropicVertexServiceAccountAuthProvider

provider = AnthropicVertexProvider(
    project_id="my-project",
    region="global",
    auth=AnthropicVertexServiceAccountAuthProvider(service_account_file="/path/to/key.json"),
)

Any AuthProvider that returns google.auth Credentials works — either bare, or as a (credentials, project_id) tuple so the provider can infer the project.

Vertex Params Caveat

AnthropicParams.service_tier and AnthropicParams.inference_geo are Anthropic-API-only: the Vertex provider drops them from outgoing requests, and the inference_geo US cost multiplier never applies.

Claude in Microsoft Foundry

No extra needed — AnthropicFoundryProvider ships with the base package and serves Claude through a Foundry resource with the same chat/streaming interface:

from lmux_anthropic import AnthropicFoundryProvider

provider = AnthropicFoundryProvider(resource="example-resource")
response = provider.chat("claude-sonnet-4-6", [UserMessage(content="Hello")])
print(response.provider)  # "anthropic-foundry"
print(response.cost)

resource and the mutually exclusive base_url fall back to the ANTHROPIC_FOUNDRY_RESOURCE and ANTHROPIC_FOUNDRY_BASE_URL environment variables. Model IDs are Foundry deployment names, which default to the plain model IDs (claude-sonnet-4-6, ...). Foundry bills Anthropic's standard API pricing through the Microsoft Marketplace, so costs come from the same pricing table with no multiplier.

Foundry Auth

The default AnthropicFoundryEnvAuthProvider reads an API key from ANTHROPIC_FOUNDRY_API_KEY. For Microsoft Entra ID, wrap a bearer-token provider:

from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from lmux_anthropic import AnthropicFoundryProvider, AnthropicFoundryTokenAuthProvider

token_provider = get_bearer_token_provider(
    DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
provider = AnthropicFoundryProvider(
    resource="example-resource",
    auth=AnthropicFoundryTokenAuthProvider(token_provider=token_provider),
)

Any AuthProvider that returns an API key string or a () -> str token-provider callable works.

Foundry Params Caveat

Same as Vertex: service_tier and inference_geo are dropped from outgoing requests, and the inference_geo US cost multiplier never applies.

Constructor Options

AnthropicProvider(
    auth=...,               # AuthProvider[str], default: AnthropicEnvAuthProvider()
    base_url=...,           # Optional base URL override
    timeout=...,            # Request timeout in seconds
    max_retries=...,        # Max retry attempts
    default_max_tokens=..., # Default max tokens (default: 4096)
)

Download files

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

Source Distribution

lmux_anthropic-0.8.0.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

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

lmux_anthropic-0.8.0-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file lmux_anthropic-0.8.0.tar.gz.

File metadata

  • Download URL: lmux_anthropic-0.8.0.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lmux_anthropic-0.8.0.tar.gz
Algorithm Hash digest
SHA256 060fee51cdca40e1d5d81df9289330e3a65785f55ae85e3d969bdd29d32275e9
MD5 a963689fffbf7004ee8dd4a3377f7d37
BLAKE2b-256 d15662cdc30b2d0362a6dac008caf357d8193a02643994aba75940b8d235af59

See more details on using hashes here.

Provenance

The following attestation bundles were made for lmux_anthropic-0.8.0.tar.gz:

Publisher: publish.yml on cluebbehusen/lmux

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

File details

Details for the file lmux_anthropic-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: lmux_anthropic-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 19.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lmux_anthropic-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1bef57bbd9577c03e32579cce0acf1ef6366c2d20b4592ed8eed8ee34c9a3af
MD5 2188777dd953928b65076b463d72254e
BLAKE2b-256 739a786807f123d5774e001ae5488ac6dcc5905767bac57c36c01f0ce9c0d651

See more details on using hashes here.

Provenance

The following attestation bundles were made for lmux_anthropic-0.8.0-py3-none-any.whl:

Publisher: publish.yml on cluebbehusen/lmux

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