Use an OpenAI-compatible model API with Python

Connect the OpenAI Python SDK to Jevrouter, make a fixed-model chat request, inspect usage and handle request failures without duplicate generations.

By Jevrouter · Updated

Prepare a project and a scoped key

Sign in to Jevrouter, select a project and create an API key that allows the model you will call. The project needs available balance. Keep the key on your server and load it through an environment variable; browser application bundles must not contain it.

Install the OpenAI Python package in your application's environment. The SDK supplies the familiar chat request shape while base_url selects Jevrouter as the service. Compatibility here covers the documented chat contract, not every feature of OpenAI's APIs.

Shell
python -m pip install openai
export JEVROUTER_API_KEY='YOUR_PROJECT_API_KEY'

Make one fixed-model request

This example disables automatic SDK retries so a timeout does not silently become another paid generation. Use a new idempotency key for each intended call, and keep it with your application's request record.

The model field pins Gemini 2.5 Flash Lite. Changing the base URL alone does not translate an existing application's model names, provider-specific options or conversation state.

Python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.jevrouter.io/v1",
    api_key=os.environ["JEVROUTER_API_KEY"],
    max_retries=0,
    timeout=125.0,
)

import uuid

response = client.chat.completions.create(
    model="google/gemini-2.5-flash-lite",
    messages=[{"role": "user", "content": "Extract the order ID from: Please check order AB-204. Return only the ID."}],
    max_tokens=256,
    extra_headers={"Idempotency-Key": str(uuid.uuid4())},
)

print(response.choices[0].message.content)
print("Model:", response.model)
print("Finish reason:", response.choices[0].finish_reason)
print("Usage:", response.usage)

Interpret the response before using it

Check finish_reason, validate the output and inspect the request record. A successful HTTP response does not prove that an extraction is correct. A length-limited response can be incomplete; reasoning models may spend a small output budget before producing final text.

The public cost calculator estimates base-rate token spend. Actual model usage, provider pricing tiers and any reported reasoning tokens determine the receipt. Jevrouter releases the unused part of a reservation after settlement.

Handle errors deliberately

Keep the X-Jevrouter-Request-Id response header, including on errors where present. If a connection closes after execution may have started, inspect the request status before creating a new generation. Reusing an idempotency key returns a conflict/status response; it does not replay the original answer.

  • 401 or 403: check the key, model allowlist and project access.
  • 402: check available balance, monthly budgets and the per-request cost ceiling.
  • 400: check model limits and parameters; changing providers cannot repair an unsupported request.
  • 429: honor Retry-After. A transport failure with an unknown outcome needs status inspection before retrying.

Models mentioned in this guide

Try it in your project.

Choose a model, create a scoped API key and inspect the result in Requests.

Open console ↗