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.7.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.7.1
File Size Uploaded
llmshim-0.7.1.tar.gz 1.7 MB Details

Built distributions (wheels)

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

Total release size: 31.9 MB

Release files / llmshim-0.7.1.tar.gz

Download URL llmshim-0.7.1.tar.gz
Size 1.7 MB
Tags Source
SHA-256 checksum
How to use checksums
c5538d114fabc128eb0e271bb10415d29f69d35372bb6a8f4996e2f00b79112d
BLAKE2b-256 checksum
How to use checksums
37ae453359dad5a868ee47ef737138c5d05dc79f73b1c8bf274d79a0589d0c98
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 20, 2026.

Transparency log

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

Download URL llmshim-0.7.1-py3-none-win_amd64.whl
Size 6.2 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
9220c07d22d43d4a34363329472e17e57f0812d0508a8434dbe8f0c17583ee80
BLAKE2b-256 checksum
How to use checksums
440fb0b779caedd9fb3dbdf1903916c62e622a0cbdc235df7c2bb29aa7f10b07
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 20, 2026.

Transparency log

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

Download URL llmshim-0.7.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 6.2 MB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
795076b99ca06417e0dff0fe03c06a0b31dd787a0310c6f6dba03712dadd9fa1
BLAKE2b-256 checksum
How to use checksums
117a5bdbcf09c48dd131fd8c22bd760e808bfabbe504b6d900ccd106306d30dd
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 20, 2026.

Transparency log

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

Download URL llmshim-0.7.1-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
8818482fe1ff6a63f662df2433d118abd2be64e900548998ab041609dac01755
BLAKE2b-256 checksum
How to use checksums
39db0a55760fb9442f61d622d2efa0673d6a43934c9f32a15117d7dbed37e55d
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 20, 2026.

Transparency log

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

Download URL llmshim-0.7.1-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
4c64bfca0535ca4a7607a94abf5088c2e9ab2ef9ca8cbf6b6df9e9d57acd82db
BLAKE2b-256 checksum
How to use checksums
b12ecde6777cd236f209738c2c47826a816ddf6ad0b121c796de1d665e277463
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 20, 2026.

Transparency log

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

Download URL llmshim-0.7.1-py3-none-macosx_10_12_x86_64.whl
Size 6.0 MB
Tags Python 3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
7eeb8c9617a0efab3d93538ec75941aefdaa93361acb1713ac901830e8cd8553
BLAKE2b-256 checksum
How to use checksums
510023522294f8971743c723655347ee5bd6c40e724a719f30f2d638b3fb410d
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 20, 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

0.9.0

6 release files

0.8.1

6 release files

0.8.0

6 release files

0.7.2

6 release files

This release

0.7.1 This release

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