kmail.at
← learning

langchain · difficulty ◆◆

LANGSMITH_API_KEY Fallback

One key, two names — resolved automatically.

Stop keeping two API keys in sync by hand.

2026-08-01 · 5 min read

$ export LANGSMITH_API_KEY=ls__...

What it does

In langchain-core 1.5.3 the gateway now automatically falls back to the LANGSMITH_API_KEY environment variable when LANGCHAIN_API_KEY is not set but a LangChain Gateway URL is configured. Previously, if LANGCHAIN_API_KEY was missing, the gateway client failed at request time even when LANGSMITH_API_KEY was present — a common situation when migrating between LangSmith and self-hosted gateway deployments.

Why it matters

This is a quiet quality-of-life fix for teams running LangChain behind a corporate firewall or using LangSmith for evaluation alongside a self-hosted gateway for production traces. Previously you had to keep both LANGCHAIN_API_KEY and LANGSMITH_API_KEY in sync or write your own os.getenv shim. Now LangChain reads the environment variable directly and does the right thing automatically — no code changes required.

Example

$ import os
os.environ.pop("LANGCHAIN_API_KEY", None)
os.environ["LANGSMITH_API_KEY"] = "ls__....mykey"
os.environ["LANGCHAIN_TRACING_V2"] = "true"
gateway = LangChainGateway(gateway_url="https://my-gateway.example.com", gateway_api_key=None)
resolved_key = gateway.api_key or os.environ.get("LANGSMITH_API_KEY")
print(f"Resolved API key starts with: {resolved_key[:8]}...")
Resolved API key starts with: ls__.......

The fallback picks up LANGSMITH_API_KEY when LANGCHAIN_API_KEY is omitted.

Common flags

LangChainGateway
Main gateway client; resolves auth from env automatically
LANGCHAIN_API_KEY
Primary env var LangChain has always looked for
LANGSMITH_API_KEY
Secondary env var; the new fallback target

History

PR-driven fallback

langchain-core 1.5.3 (2026-07-30) added the LANGSMITH_API_KEY fallback to the gateway client. The api_key property now tries LANGCHAIN_API_KEY first, then falls back to LANGSMITH_API_KEY when the first is None or empty.

Fun facts

Pros & cons

pros

  • + No manual key sync
  • + Clean gateway-only setups
  • + Zero code changes

cons

  • − Requires 1.5.3+
  • − Quiet QoL improvement

Takeaways

  1. 1Gateway falls back to LANGSMITH_API_KEY automatically.
  2. 2Upgrade to langchain-core 1.5.3.
  3. 3Drop your custom os.getenv shim.

Related commands

← all learning