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. Starts the proxy on a random localhost port
  3. Routes your request through it
  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.9.0

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.9.0
File Size Uploaded
llmshim-0.9.0.tar.gz 1.8 MB Details

Built distributions (wheels)

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

Total release size: 32.0 MB

Release files / llmshim-0.9.0.tar.gz

Download URL llmshim-0.9.0.tar.gz
Size 1.8 MB
Tags Source
SHA-256 checksum
How to use checksums
955bc01b33993d3a4617469a3a0895638367610d3ef5dbcbc7e5c1441563c7df
BLAKE2b-256 checksum
How to use checksums
3fd9a63793d968ea5bec465d41d32f21358bf6c7fd419c93f3db625d576c85f4
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 22, 2026.

Transparency log

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

Download URL llmshim-0.9.0-py3-none-win_amd64.whl
Size 6.3 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
e175fc6f33f852ab763064ed82572d14c488516c261888bea13f942e849d9bb1
BLAKE2b-256 checksum
How to use checksums
0c4ce9952ff554376c175a541bb8eb48786c2b42543117995ecbf31f1b54c6b0
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 22, 2026.

Transparency log

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

Download URL llmshim-0.9.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 6.3 MB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
873af0f5ee1a985275efef63556d5fd8c6a1cc2ab6057beb4778aae67b5b433b
BLAKE2b-256 checksum
How to use checksums
9ff1f149154ad48f4b3dda9d4b2af8ff6b1ebc61b8e11b9edb9d8c960fa5a7ce
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 22, 2026.

Transparency log

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

Download URL llmshim-0.9.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 5.9 MB
Tags Linux glibc 2.17+ ARM64 Python 3
SHA-256 checksum
How to use checksums
b92c85bd94d402c5c7d3d580ccfa87e94ba3da451e02a7f9733e07480b8cc3ef
BLAKE2b-256 checksum
How to use checksums
4e222fdf5c07d1504d0388d8fc798b02a48a60f636ed5ab3b6a61b9776df94df
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 22, 2026.

Transparency log

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

Download URL llmshim-0.9.0-py3-none-macosx_11_0_arm64.whl
Size 5.8 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
bbafa27ec73bda1486e23c08107e618c32e5759490552ec14139b13026e951cc
BLAKE2b-256 checksum
How to use checksums
e1ead0600d9f4337d7f4a9570e7f3cc6994fc820fab60bc7be78716f37a53477
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 22, 2026.

Transparency log

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

Download URL llmshim-0.9.0-py3-none-macosx_10_12_x86_64.whl
Size 6.1 MB
Tags Python 3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
fe4c7b1b9f37324fd536c874e8a6c1f3585e4a285b70febb0d271aa7e0eef0cf
BLAKE2b-256 checksum
How to use checksums
dbcc8e736cfa3204cf5939670bb30e4b68dc6e38b84a13316e758653887fb512
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 22, 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

0.12.1

6 release files

0.11.0

6 release files

0.10.1

6 release files

0.10.0

6 release files

This release

0.9.0 This release

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