lmux-anthropic
Anthropic provider for lmux. Talks to the Anthropic Messages API directly over httpx.
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="")
Tool continuations
Extended thinking and redacted thinking blocks must be returned unmodified when a tool result continues the assistant turn. lmux-anthropic preserves the native ordered blocks in response.continuation; use to_assistant_message() to keep them with the normalized response:
from lmux import ToolMessage
response = provider.chat(model, messages, tools=tools, reasoning_effort="high")
messages.append(response.to_assistant_message())
messages.append(ToolMessage(content=tool_result, tool_call_id=response.tool_calls[0].id))
Continuations are scoped to the Anthropic API surface that produced them, including Vertex AI, Microsoft Foundry, and Amazon Bedrock. A matching continuation is replayed exactly; other providers ignore it and use the normalized content and tool calls.
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 |
For manual thinking, an integer budget_tokens raises the default max_tokens when needed. An explicit max_tokens is preserved instead, along with the provider-specific thinking configuration. Ensure those explicit values are compatible with the deployed model; manual thinking normally requires budget_tokens < max_tokens, except when interleaved thinking applies.
Prompt Caching
Two ways to opt in:
- Top-level (auto-placement): pass
cache_controlviaAnthropicParams(above) to cache the full rendered prefix. - Explicit breakpoints: place
CachePointContentparts inUserMessagecontent. A cache point marks the end of the stable prefix; it attachescache_controlto 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:
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.
reasoning_effort can select the correct thinking mode only when the deployment name contains a recognizable Claude model ID. For an opaque custom deployment name, configure the thinking mode explicitly for the deployed model:
from lmux_anthropic import AnthropicParams
response = provider.chat(
"claude-prod",
[UserMessage(content="Hello")],
provider_params=AnthropicParams(thinking={"type": "enabled", "budget_tokens": 8192}),
)
Use {"type": "adaptive"} instead when the deployment targets a model that requires adaptive thinking.
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.
Claude on Amazon Bedrock
Requires the bedrock extra, which pulls in boto3 (for the AWS credential chain) and the shared lmux-bedrock-shared internals:
uv add "lmux-anthropic[bedrock]"
AnthropicBedrockProvider serves Claude through Bedrock's native Anthropic Messages API (InvokeModel / InvokeModelWithResponseStream) with the same chat/streaming interface — distinct from lmux-aws-bedrock, which speaks Bedrock's normalized Converse API across all vendors. Use this one when you want Claude on Bedrock with the exact first-party Messages semantics (thinking config, cache_control, output_config all pass through unchanged):
from lmux_anthropic import AnthropicBedrockProvider
provider = AnthropicBedrockProvider(region="us-east-1")
response = provider.chat("anthropic.claude-opus-4-8", [UserMessage(content="Hello")])
print(response.provider) # "anthropic-bedrock"
print(response.cost)
Model IDs are the Bedrock forms — a bare model ID (anthropic.claude-opus-4-8) or a cross-region inference-profile ID (us.anthropic.claude-opus-4-8, eu.anthropic.…). region falls back to the resolved AWS session's region, then us-east-1. endpoint_url overrides the endpoint; use_fips=True selects the FIPS 140-3 endpoint.
Pricing comes from the generated Bedrock table (shared with lmux-aws-bedrock via lmux-bedrock-shared), keyed by the request's Bedrock ID — so a us.-profile request is billed at its regional rate, no multiplier involved.
Bedrock Auth
Two modes, resolved once on first use:
- Bearer token — set
AWS_BEARER_TOKEN_BEDROCKand the request carriesAuthorization: Bearer <token>; nothing is signed. - SigV4 — otherwise AWS credentials are resolved through boto3 (env vars, profile, SSO, instance metadata) and every request is signed. The default
AnthropicBedrockEnvAuthProvideruses boto3's default credential chain;AnthropicBedrockSessionAuthProvidertakes explicitregion_name/profile_name/keys:
from lmux_anthropic import AnthropicBedrockProvider, AnthropicBedrockSessionAuthProvider
provider = AnthropicBedrockProvider(
auth=AnthropicBedrockSessionAuthProvider(profile_name="prod", region_name="us-east-1"),
)
Bedrock Params Caveat
Same as Vertex/Foundry: 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)
default_headers=..., # Optional headers included with every request
transport=..., # Optional httpx.BaseTransport for the sync client (proxies, testing)
async_transport=..., # Optional httpx.AsyncBaseTransport for the async client
)
default_headers is also accepted by AnthropicVertexProvider, AnthropicFoundryProvider, and
AnthropicBedrockProvider. It is useful for gateway authentication, tracing, and routing. Provider-managed
authentication, API-version, and content-type headers take precedence over caller values, case-insensitively. Bedrock
custom headers are included in the SigV4 signature.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lmux_anthropic-0.15.0.tar.gz.
File metadata
- Download URL: lmux_anthropic-0.15.0.tar.gz
- Upload date:
- Size: 28.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1464a4f66399a1a903969f501c18d9280ed688c37ab044ca7fa52a4f2ef4c4d
|
|
| MD5 |
cf75d36a6f79bbc6af5cb931d9650833
|
|
| BLAKE2b-256 |
ef3b6624ef4987781d122c87b5c20103ad09e5101e0df7c8e3fcdde33de30d76
|
Provenance
The following attestation bundles were made for lmux_anthropic-0.15.0.tar.gz:
Publisher:
publish.yml on cluebbehusen/lmux
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lmux_anthropic-0.15.0.tar.gz -
Subject digest:
e1464a4f66399a1a903969f501c18d9280ed688c37ab044ca7fa52a4f2ef4c4d - Sigstore transparency entry: 2306939106
- Sigstore integration time:
-
Permalink:
cluebbehusen/lmux@c330796afb2e901072be42c1b674ec37e9713307 -
Branch / Tag:
refs/tags/lmux-anthropic-v0.15.0 - Owner: https://github.com/cluebbehusen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c330796afb2e901072be42c1b674ec37e9713307 -
Trigger Event:
push
-
Statement type:
File details
Details for the file lmux_anthropic-0.15.0-py3-none-any.whl.
File metadata
- Download URL: lmux_anthropic-0.15.0-py3-none-any.whl
- Upload date:
- Size: 32.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41f2e46a5bfbb57d070319fd059a18055fc439b335ff133dc33b334d54806f45
|
|
| MD5 |
dd5a7fd6af6f86776b2c40452e10d68a
|
|
| BLAKE2b-256 |
8415c3f42e13eca46adec3e75291d920f12fcfcdb1e6551ce00fc46b88887cc3
|
Provenance
The following attestation bundles were made for lmux_anthropic-0.15.0-py3-none-any.whl:
Publisher:
publish.yml on cluebbehusen/lmux
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lmux_anthropic-0.15.0-py3-none-any.whl -
Subject digest:
41f2e46a5bfbb57d070319fd059a18055fc439b335ff133dc33b334d54806f45 - Sigstore transparency entry: 2306939116
- Sigstore integration time:
-
Permalink:
cluebbehusen/lmux@c330796afb2e901072be42c1b674ec37e9713307 -
Branch / Tag:
refs/tags/lmux-anthropic-v0.15.0 - Owner: https://github.com/cluebbehusen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c330796afb2e901072be42c1b674ec37e9713307 -
Trigger Event:
push
-
Statement type: