Skip to main content

llmshim

One interface, every LLM provider. The proxy server starts automatically — no setup needed.

Install

pip install llmshim

Configure

import llmshim

# Set API keys (writes to ~/.llmshim/config.toml — only needed once)
llmshim.configure(
    anthropic="sk-ant-...",
    openai="sk-...",
    gemini="AIza...",
    xai="xai-...",
    openrouter="sk-or-...",
)

Or from the CLI: llmshim configure

Self-hosted servers (vLLM / SGLang)

vLLM and SGLang are configured via environment variables (not config.toml) — the auto-spawned proxy inherits them from your Python process. Set them before your first call:

import os

os.environ["VLLM_BASE_URL"] = "http://localhost:8000/v1"
os.environ["VLLM_API_KEY"] = "..."       # optional
os.environ["SGLANG_BASE_URL"] = "http://localhost:30000/v1"
os.environ["SGLANG_API_KEY"] = "..."     # optional

Then address them via the model string — vllm/<served-model> or sglang/<served-model> (see the model table below).

Chat

import llmshim

resp = llmshim.chat("claude-sonnet-5", "What is Rust?")
print(resp["message"]["content"])

With options (all map to the API's provider-agnostic config):

resp = llmshim.chat(
    "openai/gpt-5.6-sol",
    "Explain quicksort",
    max_tokens=500,
    temperature=0.7,
    top_p=0.9,
    top_k=40,
    stop=["\n\n"],
    reasoning_effort="high",
)

With message history:

resp = llmshim.chat("claude-sonnet-5", [
    {"role": "system", "content": "You are a pirate."},
    {"role": "user", "content": "Hello!"},
], max_tokens=500)

Streaming

for event in llmshim.stream("claude-sonnet-5", "Write a poem"):
    if event["type"] == "content":
        print(event["text"], end="", flush=True)
    elif event["type"] == "reasoning":
        pass  # thinking tokens
    elif event["type"] == "usage":
        print(f"\n[↑{event['input_tokens']} ↓{event['output_tokens']}]")

Multi-Model Conversations

Switch models mid-conversation. History carries over.

messages = [{"role": "user", "content": "What is a closure?"}]

r1 = llmshim.chat("claude-sonnet-5", messages, max_tokens=500)
print(f"Claude: {r1['message']['content']}")

messages.append({"role": "assistant", "content": r1["message"]["content"]})
messages.append({"role": "user", "content": "Now explain differently."})

r2 = llmshim.chat("gpt-5.6-sol", messages, max_tokens=500)
print(f"GPT: {r2['message']['content']}")

Reasoning / Thinking

Two provider-agnostic knobs control reasoning; both are clamped to the nearest tier the target model supports:

  • reasoning_effort — "none", "low", "medium", "high", "xhigh", or "max"
  • reasoning_mode — "standard" (default) or "pro" (requests substantially more model work; native on OpenAI gpt-5.6/-pro, emulated as an effort bump elsewhere)
resp = llmshim.chat(
    "claude-sonnet-5",
    "Solve: x^2 - 5x + 6 = 0",
    max_tokens=4000,
    reasoning_effort="high",
    reasoning_mode="pro",
)
print(resp["reasoning"])        # thinking content
print(resp["message"]["content"])  # answer

For full native control, bypass the unified mapping with a namespaced provider_config (see below), e.g. provider_config={"x-anthropic": {"thinking": {"type": "adaptive"}, "output_config": {"effort": "high"}}}.

Provider-Specific Controls (provider_config)

provider_config merges into the request root and carries anything the unified config doesn't cover. Native provider controls MUST be namespaced per provider (x-anthropic, x-openai, x-gemini, x-openrouter, x-vllm, x-sglang); it also carries the top-level tools, response_format, and reasoning_summary keys.

resp = llmshim.chat(
    "anthropic/claude-sonnet-5",
    "Solve this step by step: 17 * 23",
    max_tokens=4000,
    provider_config={
        # native Anthropic adaptive-thinking control
        "x-anthropic": {"thinking": {"type": "adaptive"}, "output_config": {"effort": "high"}},
    },
)

OpenRouter routing preferences use the x-openrouter namespace:

resp = llmshim.chat(
    "openrouter/anthropic/claude-sonnet-5",
    "Hello",
    max_tokens=200,
    provider_config={"x-openrouter": {"provider": {"sort": "throughput"}}},
)

Tool Use / Function Calling

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get current weather",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

resp = llmshim.chat("claude-sonnet-5", "Weather in Tokyo?", max_tokens=500, tools=tools)
for tc in resp["message"].get("tool_calls", []):
    print(f"{tc['function']['name']}({tc['function']['arguments']})")

Tools are accepted in OpenAI Chat Completions format and auto-translated to each provider's native format.

Fallback Chains

resp = llmshim.chat(
    "anthropic/claude-sonnet-5",
    "Hello",
    max_tokens=100,
    fallback=["openai/gpt-5.6-sol", "gemini/gemini-3.8-flash"],
)

Error Handling

Non-streaming errors (bad model, unknown provider, provider failures) raise LlmShimError, which carries the API's structured error fields:

try:
    llmshim.chat("unknown/model", "hi")
except llmshim.LlmShimError as e:
    print(e.status_code)  # 400
    print(e.code)         # "bad_request"
    print(e.message)      # human-readable message

Streaming errors that occur mid-stream instead arrive as an error event (event["type"] == "error"); an HTTP error before the stream starts still raises LlmShimError.

Types

Spec-faithful TypedDict definitions are available in llmshim.types (and the common ones are re-exported at the top level) for static type-checking:

from llmshim.types import ChatResponse, StreamEvent, Message, Config

resp: ChatResponse = llmshim.chat("claude-sonnet-5", "hi")

Available: ChatRequest, ChatResponse, Config, Message, ToolCall, Usage, ResponseMessage, ModelEntry, ModelsResponse, HealthResponse, ErrorResponse, and the StreamEvent union (ContentEvent, ReasoningEvent, ToolCallEvent, UsageEvent, DoneEvent, ErrorEvent).

Other

llmshim.models()   # list available models
llmshim.health()   # {"status": "ok", "providers": [...]}

How It Works

On first call, the package:

  1. Finds the llmshim binary (bundled, on PATH, or in repo)
  2. Lets the child bind a random localhost port and reads its private startup record
  3. Pins the child's one-time TLS certificate, authenticates every request, and keeps a parent-liveness pipe open
  4. Server stops automatically when Python exits

No Docker, no background services, no manual server management.

Development

The unit tests under tests/ are fully mocked — they run a local HTTP server returning canned JSON and SSE, so they need no API keys and make no real provider calls:

pip install httpx pytest
pytest tests/

test_e2e.py is a separate LIVE suite that spawns the real binary and makes billed provider calls; run it only when you deliberately want to hit real APIs.

Advertised Models

Provider Models
OpenAI gpt-6-astra, gpt-5.6-sol, gpt-5.6-terra, gpt-5.6-luna
Anthropic claude-fable-5-1, claude-opus-5, claude-sonnet-5, claude-haiku-4-5-20251001
Gemini gemini-3.8-flash, gemini-3.5-flash-lite
xAI grok-4.6

Call llmshim.models() for the live list filtered to your configured providers.

OpenRouter & self-hosted (vLLM / SGLang)

These providers are addressed by the model string plus environment variables — any model the upstream serves is reachable, so they aren't in the table above.

Provider Address as Env vars Native controls
OpenRouter openrouter/<vendor>/<model> (e.g. openrouter/anthropic/claude-sonnet-5) OPENROUTER_API_KEY (or llmshim.configure(openrouter=...)) provider_config={"x-openrouter": {...}} (provider, models, transforms)
vLLM vllm/<served-model> VLLM_BASE_URL (+ optional VLLM_API_KEY) provider_config={"x-vllm": {...}}
SGLang sglang/<served-model> SGLANG_BASE_URL (+ optional SGLANG_API_KEY) provider_config={"x-sglang": {...}}

Metadata

Release files for llmshim 0.12.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llmshim 0.12.1
File Size Uploaded
llmshim-0.12.1.tar.gz 2.0 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for llmshim 0.12.1
File
llmshim-0.12.1-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details
llmshim-0.12.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl Python 3 none Linux glibc 2.17+ x86-64 Details
llmshim-0.12.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64 Details
llmshim-0.12.1-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details
llmshim-0.12.1-py3-none-macosx_10_12_x86_64.whl Python 3 none macOS 10.12+ x86-64 Details

Total release size: 36.0 MB

Release files / llmshim-0.12.1.tar.gz

Download URL llmshim-0.12.1.tar.gz
Size 2.0 MB
Tags Source
SHA-256 checksum
How to use checksums
bc520c8a2361894ccb8eb2101375cd894bf73a421748f68d83b8c67bf63e7dea
BLAKE2b-256 checksum
How to use checksums
e3653600d7c667e5d9864f2d4b86016ae3d08ca9f9dd00a3af956fc7e5d746c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 23, 2026.

Transparency log

Release files / llmshim-0.12.1-py3-none-win_amd64.whl

Download URL llmshim-0.12.1-py3-none-win_amd64.whl
Size 7.0 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
8c46a8b619ee84d2f1f391c0be878392dea568e6a0f6bcbd1125b3eb314b43af
BLAKE2b-256 checksum
How to use checksums
175f9f51627702c8134745c8d5f2569bd07b84500d2f5eeb701c9c32ede85e73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 23, 2026.

Transparency log

Release files / llmshim-0.12.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL llmshim-0.12.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 7.0 MB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
a882a9758b92862f37118bcc93dd0ba3a68873401c4563632ba71a13ad3be584
BLAKE2b-256 checksum
How to use checksums
facabe4428244e1a99dda3e01280e6a357398c01995d332eca9ae4ac02aa4bda
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 23, 2026.

Transparency log

Release files / llmshim-0.12.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL llmshim-0.12.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 6.6 MB
Tags Linux glibc 2.17+ ARM64 Python 3
SHA-256 checksum
How to use checksums
807956723da8a828dc56426509108628b924ae77fc29911b378aeef526a4791c
BLAKE2b-256 checksum
How to use checksums
37e42029d1e73b76e8149ccda76b19848787c90e98deafc52a7608c1a0c352f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 23, 2026.

Transparency log

Release files / llmshim-0.12.1-py3-none-macosx_11_0_arm64.whl

Download URL llmshim-0.12.1-py3-none-macosx_11_0_arm64.whl
Size 6.5 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
86e7d0707a44728e4f37498f633537d6521ef1616fa2909d857bdd1e037f08f8
BLAKE2b-256 checksum
How to use checksums
d8c1e8ab18a4d4c7cc318c6a4bb1e9ae652d25ab11c3dc73be111da9169c1167
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 23, 2026.

Transparency log

Release files / llmshim-0.12.1-py3-none-macosx_10_12_x86_64.whl

Download URL llmshim-0.12.1-py3-none-macosx_10_12_x86_64.whl
Size 6.8 MB
Tags Python 3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
f5a3943ab85fde14f133767715353e85341f6e26f4f5664bd8744805dfddfce0
BLAKE2b-256 checksum
How to use checksums
df4466845dc23ade8a67fb106a4e19b69879f92643e9db5a899c5cf6ccbf8d12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 23, 2026.

Transparency log

Release history Release notifications | RSS feed

0.28.0

6 release files

0.27.2

6 release files

0.27.1

6 release files

0.18.0

6 release files

0.17.0

6 release files

0.16.0

6 release files

0.15.0

6 release files

0.14.0

6 release files

0.13.0

6 release files

This release

0.12.1 This release

6 release files

0.11.0

6 release files

0.10.1

6 release files

0.10.0

6 release files

0.9.0

6 release files

0.8.1

6 release files

0.8.0

6 release files

0.7.2

6 release files

0.7.1

6 release files

0.7.0

6 release files

0.6.0

6 release files

0.5.0

6 release files

0.4.0

6 release files

0.3.7

6 release files

0.3.6

6 release files

0.3.5

6 release files

0.3.4

6 release files

0.3.3

6 release files

0.3.2

6 release files

0.3.1

6 release files

0.3.0

6 release files

0.2.3

6 release files

0.2.2

6 release files

0.2.1

6 release files

0.2.0

6 release files

0.1.26

6 release files

0.1.25

6 release files

0.1.21

6 release files

0.1.20

6 release files

0.1.16

6 release files

0.1.15

6 release files

0.1.14

6 release files

0.1.13

6 release files

0.1.12

6 release files

0.1.11

6 release files

0.1.10

6 release files

0.1.9

6 release files

0.1.8

6 release files

0.1.7

6 release files

0.1.6

6 release files

0.1.4

6 release files

0.1.3

6 release files

0.1.2

6 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page