Browse documentation

LangChain

Use nRouter with LangChain in Python and JavaScript. Integrate smart routing, automatic retries, guardrails, and cost tracking into your LangChain pipelines.

Integrate nRouter with LangChain across Python and JavaScript to add resilient model routing, server-side guardrails, and exact cost tracking to chains and agents. Point standard ChatOpenAI and OpenAIEmbeddings components directly to https://api.nrouter.ai/v1 without modifying prompt templates, memory, or execution graphs.

For framework details, see the official LangChain documentation.

Installation & Setup

Install the required packages for your runtime and export your virtual API key:

# Python
pip install langchain-openai langchain-core nrouter-sdk
export NROUTER_API_KEY="sk-nrouter-xxxxxxxxxxxx"

# TypeScript / JavaScript
npm install @langchain/openai @langchain/core

Initialize ChatOpenAI pointing to nRouter:

import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="claude-sonnet-4-5-20250929",
    api_key=os.environ["NROUTER_API_KEY"],
    base_url="https://api.nrouter.ai/v1",
    temperature=0.2,
    timeout=60.0,
    max_retries=2,
    model_kwargs={"nrouter_cache": True},
)

LCEL & Streaming

Build composable, streaming chains with LangChain Expression Language (LCEL):

import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

llm = ChatOpenAI(
    model="gpt-5.4-mini",
    api_key=os.environ["NROUTER_API_KEY"],
    base_url="https://api.nrouter.ai/v1",
)

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are an expert systems engineer. Answer concisely."),
    ("user", "Explain {concept} in high-availability systems."),
])

chain = prompt | llm | StrOutputParser()
for chunk in chain.stream({"concept": "circuit breakers"}):
    print(chunk, end="", flush=True)
print()

Routing & Observability

Configure model selection, RAG embeddings, and error handling:

  • Routing & fallbacks: Target specific models or use nrouter/auto (see Router Settings). For per-call fallbacks, pass nrouter_fallbacks in model_kwargs (see Per-Request Options).
  • FinOps telemetry: Headers include x-nr-request-id, x-nr-model, x-nr-cost-status, and x-nr-request-cost. Access usage via response.response_metadata["token_usage"].
  • Embeddings: Point OpenAIEmbeddings to https://api.nrouter.ai/v1 for server-side guardrails on retrieval documents.
  • Error handling: Gateway errors map to HTTP status codes (see Errors & Rate Limits):
import openai

try:
    response = llm.invoke("Execute pipeline task.")
except openai.BadRequestError as e:
    print(f"Blocked by guardrail or invalid parameters: {e}")
except openai.RateLimitError:
    print("Rate limit reached. Backing off before retry.")

Next Steps

Was this page helpful?