Expand description
nRouter SDK for Rust — one API key for models across six provider clouds.
Two entry points, deliberately:
clientreturns anasync_openaiclient pointed at the gateway, so every OpenAI-shaped call you already write keeps working unmodified.http::Clientis a native client that hands back thex-nr-*metadata — per-request cost, tokens, cache outcome — which theasync-openaisurface cannot expose because it discards the raw response.
use async_openai::types::chat::{
ChatCompletionRequestUserMessageArgs, CreateChatCompletionRequestArgs,
};
let client = nrouter::client()?; // reads NROUTER_API_KEY
let request = CreateChatCompletionRequestArgs::default()
.model("claude-sonnet-4-5")
.messages(vec![ChatCompletionRequestUserMessageArgs::default()
.content("Hello!")
.build()?
.into()])
.build()?;
let response = client.chat().create(request).await?;
println!("{}", response.choices[0].message.content.as_ref().unwrap());Reading the cost of a call:
use serde_json::json;
let client = nrouter::http::Client::from_env()?;
let out = client
.chat_completions(&json!({
"model": "claude-sonnet-4-5",
"messages": [{"role": "user", "content": "Hello!"}]
}))
.await?;
match out.meta.cost {
Some(usd) => println!("cost ${usd}"),
// Unpriced is not free — it is unknown. Never render it as 0.
None => println!("unpriced ({:?})", out.meta.cost_status),
}Re-exports§
pub use errors::compute_jittered_backoff;pub use errors::format_error;pub use errors::parse_gateway_error_envelope;pub use errors::parse_retry_after;pub use errors::parse_retry_after_at;pub use errors::redact_keys;pub use errors::ErrorBody;pub use errors::ErrorEnvelope;pub use errors::NRouterError;pub use errors::MAX_RETRY_AFTER_SECONDS;pub use media::validate_audio_format;pub use media::VALID_AUDIO_FORMATS;pub use memory::sliding_window;pub use memory::ArrayStore;pub use memory::ChatMessage;pub use memory::Memory;pub use memory::MemoryStore;pub use meta::extract_trace_headers;pub use meta::with_trace_context;pub use meta::BudgetWarningInfo;pub use meta::ResponseMeta;pub use meta::HEADER_NAMES;pub use models::MODELS;pub use prompts::render_prompt;pub use prompts::RenderPromptOptions;
Modules§
- errors
- Typed errors, derived from
spec/nrouter-sdk-spec.json. - http
- A native client that can SEE the
x-nr-*headers. - media
- Media helpers for audio format validation and video polling.
- memory
- Client-side conversation memory.
- meta
- Per-request metadata carried on nRouter response headers.
- models
- Model constants generated from
spec/model-catalog-snapshot.json(snapshot version: 2026-11-14, schema_version: 1, catalog_revision: 1). - options
- nRouter request option helpers.
- prompts
- Managed prompt helpers for the nRouter request fields.
- sampling
- Sampling-parameter policy shared with the JavaScript SDK.
Constants§
- DEFAULT_
BASE_ URL - The gateway’s customer surface. A dynamic value: override it for stage.
- ENV_KEY
- The one environment variable this SDK reads.
- KEY_
PREFIX - Every customer key carries this prefix.
Functions§
- client
- An
async-openaiclient pointed at nRouter, keyed fromNROUTER_API_KEY. - client_
with_ key - An
async-openaiclient pointed at nRouter with an explicit key. - client_
with_ key_ and_ base_ url - An
async-openaiclient pointed at an explicit nRouter gateway. - client_
with_ key_ base_ url_ and_ http_ client - The public
async-openaifacade over a caller-supplied HTTP transport. - resolve_
api_ key - Resolve and validate a key: explicit argument first, then the environment.
- uses_
messages_ wire - True when a model family is served on /v1/messages rather than /v1/chat/completions.