{"slug": "the-base-url-is-the-whole-integration-langchain-through-a-gateway", "title": "The base_url Is the Whole Integration: LangChain Through a Gateway", "summary": "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.", "body_md": "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.\n\nChatOpenAI 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.\n\n``` python\nfrom langchain_openai import ChatOpenAI\n\nllm = ChatOpenAI(\n    model=\"nrouter/auto\",\n    api_key=\"sk-nrouter-...\",\n    base_url=\"https://api.nrouter.ai/v1\",\n    temperature=0.2,\n    timeout=60,\n    max_retries=2,\n    model_kwargs={\"nrouter_cache\": True},\n)\n```\n\nPrompt templates: unchanged. Memory: unchanged. The LCEL pipe you composed — `prompt | llm | StrOutputParser()` — unchanged, streaming included. JavaScript is the same shape with @langchain/openai.\n\nSo what does the endpoint buy you, if the code doesn't change?\n\n**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.\n\n**Routing as a per-call decision.** With `nrouter/auto` in the model slot, model_kwargs carries the policy per call:\n\n`nrouter_optimize`: `quality`, `balanced` or `cost`\n`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\nA triage chain and a drafting chain can share a codebase and still make different trade-offs, because the policy travels with the request.\n\n**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.\n\n**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.\n\nThe 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)\n\nThe chain doesn't change. The endpoint does.", "url": "https://wpnews.pro/news/the-base-url-is-the-whole-integration-langchain-through-a-gateway", "canonical_source": "https://dev.to/nrouterai/the-baseurl-is-the-whole-integration-langchain-through-a-gateway-2gja", "published_at": "2026-11-26 10:09:39+00:00", "updated_at": "2026-11-26 10:14:05.678542+00:00", "lang": "en", "topics": ["ai-agents", "large-language-models", "ai-tools", "developer-tools"], "entities": ["LangChain", "nRouter", "ChatOpenAI", "OpenAIEmbeddings", "@langchain/openai"], "also_reported_by": [], "takeaway": "Swapping only the base_url and API key on LangChain's ChatOpenAI to a gateway endpoint adds server-side PII redaction, prompt-injection filtering and per-call routing policies without touching prompts, memory or LCEL chains.", "recommendation": {"advice": "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.", "playbook": "llm-cost", "playbook_url": "https://wpnews.pro/playbooks/llm-cost"}, "alternates": {"html": "https://wpnews.pro/news/the-base-url-is-the-whole-integration-langchain-through-a-gateway", "markdown": "https://wpnews.pro/news/the-base-url-is-the-whole-integration-langchain-through-a-gateway.md", "text": "https://wpnews.pro/news/the-base-url-is-the-whole-integration-langchain-through-a-gateway.txt", "jsonld": "https://wpnews.pro/news/the-base-url-is-the-whole-integration-langchain-through-a-gateway.jsonld"}}