langchain · difficulty ◆
Gateway Key from Env Var
Set the gateway key once, forget it everywhere.
One export replaced a hundred repeated key arguments.
$ export LANGCHAIN_LANGCHAIN_GATEWAY_API_KEY=sk-...What it does
A new env var LANGCHAIN_LANGCHAIN_GATEWAY_API_KEY was introduced across langchain-anthropic, langchain-fireworks, and langchain-openai (PR #38742). Instead of passing a langchain_gateway_api_key argument explicitly when initializing a model client, you set it once as an env var and every LangChain integration talking to LangSmith Gateway picks it up automatically.
Why it matters
Production pipelines often initialize many model clients (OpenAI, Anthropic, Fireworks) sharing the same LangSmith Gateway endpoint and credentials. Before, you had to pass langchain_gateway_api_key="..." to every .bind() or .with_structured_output() call — error-prone and verbose. A single process-level export covers them all. This shines in cron jobs, Docker containers, and serverless functions where secrets are injected via environment variables.
Example
$ import os
os.environ["LANGCHAIN_LANGCHAIN_GATEWAY_API_KEY"] = "sk-..."
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o")
print(llm.invoke("What is LangSmith?").content)LangSmith is a platform for debugging, testing, and monitoring
LangChain applications. It provides tracing, evaluation, and
dataset management capabilities to help you improve your LLM
pipelines over time.No explicit langchain_gateway_api_key is passed — the env var handles it.
Common flags
- LANGCHAIN_LANGCHAIN_GATEWAY_API_KEY
- Env var supplying the gateway API key to all integrations
- langchain_gateway_api_key
- Explicit constructor arg now optional when the env var is set
- with_structured_output()
- Structured-output calls that inherit the gateway key
History
PR #38742
The env-var support landed across three partner packages and in langchain-core 1.5.1, alongside the tool_call_schema caching fix. It centralized gateway auth so deployments stop repeating credentials per call site.
Fun facts
Pros & cons
pros
- + Set it once for all clients
- + Cleaner production config
- + Ideal for containers and cron
cons
- − Only for gateway deployments
- − Requires the newest package versions
Takeaways
- 1One env var authenticates every model client.
- 2Get the key from LangSmith Settings → API Keys.
- 3Works across OpenAI, Anthropic, and Fireworks.