Skip to main content

RoutePlex Python SDK - Multi-model AI gateway. Route requests across OpenAI, Anthropic, and Google Gemini through a unified API.

Project description

RoutePlex Python SDK

PyPI License: MIT Python 3.8+

The official Python SDK for RoutePlex, the multi-model AI gateway. Route requests across OpenAI, Anthropic, and Google through a single API.

Install

pip install routeplex

Quick Start

from routeplex import RoutePlex

client = RoutePlex(api_key="rp_live_YOUR_KEY")

# Auto-routing — analyzes your prompt, picks the best model
response = client.chat("Explain quantum computing")
print(response.output)
print(f"Model: {response.model_used}")
print(f"Cost: ${response.usage.cost_usd:.6f}")

# Or override with a strategy
response = client.chat("Write a Python sorting function", strategy="quality")

Features

  • One-liner chat — pass a string, get a response
  • Streaming — real-time SSE with chat_stream() in buffered (~100ms) or realtime (~10ms) mode
  • Prompt-based auto-routing — RoutePlex analyzes your prompt and picks the best model automatically
  • Strategy routing — override with strategy (cost, speed, quality, balanced, auto)
  • Manual mode — pick a specific model with model="gpt-4o-mini"
  • Prompt enhancement — auto-improve prompts before sending to the model
  • Test mode — safe development and CI testing with default-tier models only
  • Cost estimation — estimate costs before sending (free, no API key needed)
  • Model catalog — list all 30+ models with pricing, capabilities, and health status
  • Typed errorsAuthenticationError, RateLimitError, ValidationError, ProviderError, ContentPolicyError
  • Zero dependencies — uses only Python stdlib (urllib, json)

Also see: Streaming · More Examples · OpenAI SDK Compatible

Routing Modes

RoutePlex supports three ways to route your requests:

1. Auto-routing (default) — analyzes your prompt

When you don't specify a model or strategy, RoutePlex analyzes your prompt to determine the best model. A simple question gets a fast, cheap model. A complex reasoning task gets a capable one.

# RoutePlex reads your prompt and picks the optimal model
response = client.chat("What is Python?")           # → fast, cheap model
response = client.chat("Prove the Riemann hypothesis approach")  # → powerful model

2. Strategy routing — you choose the priority

When you specify a strategy, RoutePlex picks the best model for that priority — regardless of prompt content.

response = client.chat("Write a haiku", strategy="speed")      # fastest model
response = client.chat("Analyze this data", strategy="quality") # most capable model
response = client.chat("Summarize this article", strategy="cost") # cheapest model
response = client.chat("General task", strategy="balanced")     # cost/speed/quality tradeoff

3. Manual mode — you pick the model

response = client.chat("Explain recursion", model="gpt-4o-mini")

Prompt Enhancement

Auto-improve your prompt before it reaches the model. Stateless, free, adds no latency overhead.

# Per-request enhancement
response = client.chat("fix my code", enhance_prompt=True)

# Standalone — preview the enhanced prompt (free, no API key)
result = client.enhance("tell me about kubernetes")
if result.changed:
    print(f"Enhanced: {result.enhanced_prompt}")

Test Mode

Use test_mode during development and CI to keep routing on default-tier models only — no premium charges, predictable costs.

# Safe for CI pipelines — will never route to premium models
response = client.chat("Write a unit test for this function.", test_mode=True)

test_mode only affects auto-routing. In manual mode you pick the model explicitly, so it has no effect.

Streaming

Stream responses in real time using chat_stream(). Supports two modes: "buffered" (default, ~100ms chunks for smooth output) and "realtime" (~10ms chunks for minimal latency).

# Buffered streaming (default)
for event in client.chat_stream("Explain how streaming works"):
    if event.type == "delta":
        print(event.content, end="", flush=True)
    elif event.type == "done":
        print(f"\nModel: {event.model_used} | Cost: ${event.usage['cost_usd']:.6f}")

# Realtime streaming — lowest latency
for event in client.chat_stream("Quick answer", stream_mode="realtime"):
    if event.type == "delta":
        print(event.content, end="", flush=True)

Events: delta (content chunk), done (final stats), error (failure).

More Examples

Multi-turn conversations

response = client.chat([
    {"role": "system", "content": "You are a helpful tutor."},
    {"role": "user", "content": "What is recursion?"},
    {"role": "assistant", "content": "Recursion is when a function calls itself..."},
    {"role": "user", "content": "Can you give me a Python example?"},
])

Cost estimation (free)

estimate = client.estimate("Write a blog post about AI")
print(f"Model: {estimate.model}")
print(f"Estimated cost: ${estimate.estimated_cost_usd:.6f}")

List models

models = client.list_models()
for m in models:
    print(f"{m.id} ({m.provider}) — {m.tier}")

Error handling

from routeplex import RoutePlex, RateLimitError, AuthenticationError

try:
    response = client.chat("Hello!")
except AuthenticationError:
    print("Invalid API key")
except RateLimitError as e:
    print(f"Rate limited: {e.message}")

Also Works with OpenAI SDK

You can also use RoutePlex with the OpenAI SDK — just change the base_url:

from openai import OpenAI

client = OpenAI(
    api_key="rp_live_YOUR_KEY",
    base_url="https://api.routeplex.com/v1",
)

response = client.chat.completions.create(
    model="routeplex-ai",
    messages=[{"role": "user", "content": "Hello!"}],
)

Ecosystem

Package Platform Install
routeplex PyPI pip install routeplex
@routeplex/node npm npm install @routeplex/node

Links

License

MIT — see LICENSE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

routeplex-1.0.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

routeplex-1.0.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file routeplex-1.0.0.tar.gz.

File metadata

  • Download URL: routeplex-1.0.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for routeplex-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a8c5bf5876ab586b73aa110f4b07b1a6894ebcb569f48e9c0bec3316841ae097
MD5 9d16b16f14eb5b5d9a4d86006416af80
BLAKE2b-256 06abfe32cef52cd850ca214d0445101a1a818cb544618315eed3c6d82cb22829

See more details on using hashes here.

File details

Details for the file routeplex-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: routeplex-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for routeplex-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d44ca54323ec9149efdb7372d644c2f4fba2674f96c3658482e1be52415ad3f7
MD5 82a343ce48b0d1a14c21caf96f642d4a
BLAKE2b-256 52ed62d652b92eb6fa9686d9fb5ef7b3f1b9cca64e096d29980cda71c449faeb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page