Skip to main content

Routra Python SDK - OpenAI-compatible client with intelligent multi-provider routing

Project description

Routra Python SDK

Thin wrapper over the OpenAI Python SDK. Adds typed routing metadata, policy helpers, and async support.

Installation

pip install routra
# or
uv add routra

Quick Start

from routra import Routra

client = Routra(api_key="rtr-...")
response = client.chat.completions.create(
    model="auto",
    messages=[{"role": "user", "content": "Hello"}],
)

# Typed routing metadata on every non-streaming response
if response.routra:
    print(response.routra.provider)    # "groq"
    print(response.routra.latency_ms)  # 245
    print(response.routra.score)       # 0.8642
    print(response.routra.cost_usd)    # 0.000089

Routing Policies

# Set a default policy for all requests
client = Routra(api_key="rtr-...", policy="cheapest")

# Per-request policy override
fast_client = client.with_policy("fastest")
response = fast_client.chat.completions.create(
    model="auto",
    messages=[{"role": "user", "content": "Urgent request"}],
)

Async Support

from routra import AsyncRoutra

client = AsyncRoutra(api_key="rtr-...")
response = await client.chat.completions.create(
    model="auto",
    messages=[{"role": "user", "content": "Hello"}],
)
print(response.routra.provider)

Environment Variables

Variable Description
ROUTRA_API_KEY Default API key (used when api_key parameter is not provided)
ROUTRA_BASE_URL Override API base URL (default: https://api.routra.dev/v1)
import os
os.environ["ROUTRA_API_KEY"] = "rtr-..."

# No need to pass api_key
client = Routra()

Routing Metadata

The response.routra field is a RoutingMetadata object with these fields:

Field Type Description
provider str Provider slug that served the request (e.g. "groq")
latency_ms int Total provider response time in milliseconds
score float Normalized routing score (0–1)
cost_usd float | None Estimated cost in USD
input_tokens int | None Input token count
output_tokens int | None Output token count
failover bool | None Whether the request was rerouted due to a provider failure
ttfb_ms int | None Time to first byte (streaming only)

Note: routra metadata is only available on non-streaming responses. For streaming, use the x-routra-provider response header.

OpenAI Compatibility

Since Routra extends openai.OpenAI, all OpenAI SDK features work transparently:

# Streaming (metadata via headers, not response object)
stream = client.chat.completions.create(
    model="auto",
    messages=[{"role": "user", "content": "Count to 10"}],
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content, end="")

# Embeddings
embedding = client.embeddings.create(
    model="text-embedding-3-small",
    input="Hello world",
)

License

MIT

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

routra-0.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

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

routra-0.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file routra-0.1.0.tar.gz.

File metadata

  • Download URL: routra-0.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for routra-0.1.0.tar.gz
Algorithm Hash digest
SHA256 092bf3b4b1d49920314c2f5f3382d394aa8163eb423f34ed636dd105ff9ee080
MD5 9877bc694119f9b0c45eb169b17828aa
BLAKE2b-256 69c5cdee120ed7a0bc4c9ada49973ea0ec30285c4fd9892fd4bca0b17e7baed3

See more details on using hashes here.

File details

Details for the file routra-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: routra-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for routra-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 85f586decbfd124dd1f65b75d17bf7a63e94b24e7bf21f45ce6ea3e9ba308aba
MD5 a64db3d665e6d208f7c593ae612424ae
BLAKE2b-256 ec8cd69f102a961d8aed551f2ec686bed294a2696217cf6c9a31d2635b05a2c1

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