Skip to main content

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 errors — AuthenticationError, 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

Metadata

Release files for routeplex 1.0.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 routeplex 1.0.0
File Size Uploaded
routeplex-1.0.0.tar.gz 13.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for routeplex 1.0.0
File Interpreter ABI Platform
routeplex-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.7 kB

Release files / routeplex-1.0.0.tar.gz

Download URL routeplex-1.0.0.tar.gz
Size 13.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a8c5bf5876ab586b73aa110f4b07b1a6894ebcb569f48e9c0bec3316841ae097
BLAKE2b-256 checksum
How to use checksums
06abfe32cef52cd850ca214d0445101a1a818cb544618315eed3c6d82cb22829
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / routeplex-1.0.0-py3-none-any.whl

Download URL routeplex-1.0.0-py3-none-any.whl
Size 12.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d44ca54323ec9149efdb7372d644c2f4fba2674f96c3658482e1be52415ad3f7
BLAKE2b-256 checksum
How to use checksums
52ed62d652b92eb6fa9686d9fb5ef7b3f1b9cca64e096d29980cda71c449faeb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.1.0

2 release files

0.0.1

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