0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

nrouter-sdk for Python: one client, every LLM, cost on every response

0
Posted at

If your Python app calls more than one LLM provider, you've felt it: a different client library per provider, a different auth flow per provider, and cost tracking you have to build yourself. nRouter's official Python SDK (nrouter-sdk on PyPI) replaces all of that with one typed client against a managed gateway.

This isn't a wrapper around five provider SDKs. It's a single client speaking to one OpenAI-compatible endpoint (https://api.nrouter.ai/v1), with routing, fallbacks, guardrails, and cost tracking handled server-side. The SDK documentation is the hub for all of nRouter's SDKs — Python and TypeScript are published, with Go, Rust, Java, Kotlin, Swift, Dart/Flutter, and R in preview.

Install and first call

pip install nrouter-sdk
from nroutersdk import nRouter

client = nRouter()  # reads NROUTER_API_KEY from the environment

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

print(response.choices[0].message.content)

Three things to note:

  1. The key lives in the environment. nRouter() reads NROUTER_API_KEY — never hardcode it.
  2. The model string is just a string. Swap it for another catalog model and you're calling a different provider. No new package, no new client class.
  3. The call shape is the OpenAI chat completions shape, documented in the Chat Completions API reference. If you've used the OpenAI Python client, this is familiar on purpose.

Cost on every response — no extra instrumentation

This is the part that sold me. Every response carries its exact cost, parsed and surfaced on the client:

if client.last_response.cost:
    print(f"Cost: ${client.last_response.cost}")
else:
    print("Cost: unpriced")

nRouter bills at exact provider list prices with 0% token markup, and the cost behavior is honest: when a call can't be priced, the cost is absent (reported as unpriced) rather than misleadingly shown as $0. Your logging pipeline should treat a missing cost as "unknown," not "free." The x-nr-cost-status header carries the same signal at the HTTP layer.

For a Python service, this means per-request spend tracking without a separate telemetry pipeline — read it off the client right after the call.

Per-request controls

Beyond the basic call, nRouter exposes per-request control fields — nrouter_fallbacks (route around provider outages automatically), nrouter_guardrails, and nrouter_cache — that attach to individual requests. The per-request options guide documents the full set. Fallback policy becomes a request option instead of retry logic you maintain per provider.

Where the SDKs stand

nrouter-sdk (Python) and @nrouter_ai/sdk (TypeScript) are the published SDKs today. If you work in another language, the SDK documentation also covers the preview SDKs and the drop-in path — pointing the stock OpenAI client at the same base URL. Same gateway, same models, same cost headers, whichever client you use.

Takeaway

pip install nrouter-sdk, one environment variable, one client. The model becomes configuration, the cost becomes a property on the response, and fallbacks become request options. That's the whole integration.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?