cd /news/ai-agents/the-base-url-is-the-whole-integratio… · home › topics › ai-agents › article
[ARTICLE · art-176701] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

The base_url Is the Whole Integration: LangChain Through a Gateway

A developer demonstrated that pointing LangChain's ChatOpenAI constructor at a gateway base_url (nRouter at api.nrouter.ai/v1) with a virtual key routes all chain traffic through the gateway without changing prompts, memory, or LCEL pipelines. The setup adds server-side guardrails (PII redaction, prompt-injection filtering, keyword blocks), per-call routing policies via nrouter_auto, nrouter_optimize, nrouter_cost_band and nrouter_fallbacks, and per-response cost and trace headers (x-nr-request-cost, x-nr-request-id, x-nr-model). OpenAIEmbeddings can use the same endpoint so indexed documents are screened before embedding.

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 playbook: Controlling LLM cost
by read2 min views6 publishedNov 26, 2026

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 — with a virtual key, and the chain's traffic runs through the gateway. Here's the part that matters: nothing else changes.

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

The chain doesn't change. The endpoint does.

── more in #ai-agents 4 stories · sorted by recency
── more on @langchain 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/the-base-url-is-the-…] indexed:0 read:2min 2026-11-26 · —