# The base_url Is the Whole Integration: LangChain Through a Gateway

> Source: <https://dev.to/nrouterai/the-baseurl-is-the-whole-integration-langchain-through-a-gateway-2gja>
> Published: 2026-11-26 10:09:39+00:00

**Recommendation:** Route LangChain calls through a gateway base_url and set nrouter_optimize and nrouter_cost_band per call so triage and drafting chains can trade cost against quality without code changes.
More in the playbook "Controlling LLM cost": <https://wpnews.pro/playbooks/llm-cost>

Every LangChain app has a connection point, and it's smaller than you think. It's the ChatOpenAI constructor. Model name, key, done — that object decides which provider takes every call your chain makes, what a retry looks like, and what evidence exists afterwards.

ChatOpenAI takes a base_url. Point it at a gateway — here, nRouter at [https://api.nrouter.ai/v1](https://api.nrouter.ai/v1) — with a virtual key, and the chain's traffic runs through the gateway. Here's the part that matters: nothing else changes.

``` python
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="nrouter/auto",
    api_key="sk-nrouter-...",
    base_url="https://api.nrouter.ai/v1",
    temperature=0.2,
    timeout=60,
    max_retries=2,
    model_kwargs={"nrouter_cache": True},
)
```

Prompt templates: unchanged. Memory: unchanged. The LCEL pipe you composed — `prompt | llm | StrOutputParser()` — unchanged, streaming included. JavaScript is the same shape with @langchain/openai.

So what does the endpoint buy you, if the code doesn't change?

**Guardrails that live outside your repos.** Prompts get screened server-side before a provider sees them: PII detection and redaction, prompt-injection filtering, keyword blocks. A blocked call comes back as a plain `openai.BadRequestError`, so the error handling you already wrote catches it. Rate limits surface as `RateLimitError`. No new exception taxonomy to learn.

**Routing as a per-call decision.** With `nrouter/auto` in the model slot, model_kwargs carries the policy per call:

`nrouter_optimize`: `quality`, `balanced` or `cost`
`nrouter_cost_band`: `low`, `mid` or `high` (under cost optimisation)`nrouter_fallbacks`: the models to try when the first choice can't take the call
A triage chain and a drafting chain can share a codebase and still make different trade-offs, because the policy travels with the request.

**A receipt on every response.** Priced responses return the exact list rate in the `x-nr-request-cost` header — flat list price, zero token markup — alongside `x-nr-request-id` for tracing and `x-nr-model` for what actually served the call. Token usage is in `response_metadata` as usual, so the two reconcile without a billing export.

**Embeddings on the same endpoint.** `OpenAIEmbeddings` takes the same base_url. The documents you index are untrusted text by definition; screening them before embedding is the half of the integration most teams forget.

The framework is the composition layer. The endpoint is the control layer. Keeping those jobs separate is the whole trick — full setup for Python and JavaScript is in the guide: [https://nrouter.ai/docs/frameworks/langchain](https://nrouter.ai/docs/frameworks/langchain)

The chain doesn't change. The endpoint does.
