Python SDK

Install

python -m pip install llmswap

Query one provider

from llmswap import LLMClient

client = LLMClient(provider="openai", model="gpt-5.6")
response = client.query("Summarize the trade-offs in this decision")

print(response.content)
print(response.provider, response.model, response.usage)

Omit provider to auto-detect the first configured provider. Explicit model IDs are passed to the chosen provider even when they are not in the curated Arena catalog.

Best Answer

from llmswap import LLMClient

client = LLMClient(provider="openai", model="gpt-5.6")
result = client.best_answer(
    "Which option is most robust, and what could make it fail?"
)

print(result.best_answer)
print(result.agreement_level)
print(result.agreement)
print(result.disagreements)
print(result.cautions)
print(result.total_usage)
print(result.latency)

The default uses three independent drafts and one judge request on the primary provider/model. Candidate calls run concurrently; judging happens afterward.

Select providers explicitly:

result = client.best_answer(
    "Compare these proposals",
    models=["openai:gpt-5.6", "sarvam:sarvam-105b"],
    judge="openai:gpt-5.6",
    allow_cross_provider_sharing=True,
)

Cross-provider mode sends candidate text to the judge provider. LLMSwap anonymizes candidate labels but does not automatically redact sensitive data.

Client controls

client = LLMClient(
    provider="openai",
    model="gpt-5.6",
    fallback=True,
    cache_enabled=False,
    cache_ttl=3600,
    analytics_enabled=False,
    workspace_enabled=False,
)
  • Caching and local analytics are opt-in.
  • Fallback does not guarantee provider availability.
  • Set fallback=False when a request must never move to another configured provider.

Switching providers

client = LLMClient(provider="openai")
client.set_provider("anthropic", model="claude-sonnet-5")
response = client.query("Review this recommendation")

Async client

import asyncio
from llmswap import AsyncLLMClient


async def main():
    client = AsyncLLMClient(provider="gemini")
    response = await client.query("Give me three ways to reduce queue latency")
    print(response.content)


asyncio.run(main())

Tool schemas

from llmswap import LLMClient, Tool

weather = Tool(
    name="get_weather",
    description="Get current weather for a city",
    parameters={"city": {"type": "string"}},
    required=["city"],
)

client = LLMClient(provider="anthropic")
response = client.chat("What is the weather in Tokyo?", tools=[weather])

Tool schemas are implemented for Anthropic, OpenAI, Gemini, Groq, and xAI. The application remains responsible for validating arguments, executing tools, and returning results.

MCP from Python

from llmswap import LLMClient

client = LLMClient(provider="openai")
client.add_mcp_server(
    "filesystem",
    command=[
        "npx",
        "-y",
        "@modelcontextprotocol/server-filesystem",
        "/tmp",
    ],
)

response = client.chat("List log files in /tmp", use_mcp=True)
print(response.content)
client.remove_mcp_server("filesystem")

Only connect trusted MCP servers and grant narrow permissions.

Errors and verification

from llmswap import LLMClient, LLMSwapError

try:
    response = LLMClient(provider="openai", fallback=False).query("Hello")
except LLMSwapError as error:
    print(error)

See the examples for complete runnable workflows.


Copyright © 2026 LLMSwap. Distributed under the MIT license.

This site uses Just the Docs, a documentation theme for Jekyll.